views:

320

answers:

1

In my application I have large area (≈5000x5000pts) and I must allow user to see (+zoom, scroll) only its certain rectangular subregion with sides not necessarily parallel to area sides.

What is the best way to accomplish that?

What I am doing now:

  • Have a large UIView containing whole area (so its frame is 5000x5000) as a UIScrollView's subview.
  • Calculate and apply CGAffineTransform - rotate my view to make region sides parallel to the coordinate axis and position required area to the origin:

    contentView.transform = CGAffineTransformMake(cos(angle), sin(angle), -sin(angle), cos(angle), -requiredRect.origin.x, -requiredRect.origin.y);
    
  • Set scrollview's content size to required value.

    scrollView.contentSize = CGSizeMake(someWidth, someHeight);
    

It works in a way but there're some (not all listed in fact) problems with it:

  • When I zoom the scroll view its content size resets to the size of my large area view (5000x5000 multiplied by scale factor). I set it again in delegate's scrollViewDidEndZooming method but it looks weird anyway.
  • Zooming seems to be applied with incorrect anchor point and looks not nice (view "jumps" to another position after zooming is finished)

Can you propose another approach to the problem or point what can be done to improve my current one? It looks that UIScrollView behaves badly when custom transforms are applied to its content views...

A: 

Although I'm not completely satisfied with my solution, I came up with the following: in delegate's -scrollViewDidScroll method I check if current visible region is a subregion of my limited area. If it is not I adjust contents offset without animation to make visible region fit the limits.

- (void)scrollViewDidScroll:(UIScrollView *)aScrollView{
    if (!CGRectContainsRect(myLimitRect, [aScrollView visibleRect]){
         // calculate new contents offset to make visible region fit limits
         [aScrollView setContentOffset:newContentOffset animated:NO}; 
    }   
}

Doing this way I don't need to apply complex CGAffineTransforms to my contents view explicitly (no need to scale/translate the view) - all I need is rotate it and it seems UIScrollView can handle that quite well.

Vladimir