开发者

UIScrollView odd behavior: zoom in, pan, zoom out leaves content view uncentered

I have a custom UIViewController subclass controlling a UISCrollView containing an UIImageView, set up like so:

UIViewController
+ UIScrollView (delegate: UIViewController, viewForZoomingInView: UIImageView)
  + UIImageView

I can use the UIScrollView to zoom in on my image, and pan around -- but if I do so such that the center of the view is not the center of the image, and then zoom out fully, my image is off center; what's more, since I am at the minimum zoom scale at that point, I cannot pan around to re-center my image.

My expectation was that when zooming out, UIScrollView would re-center the contentSize's area under its bounds so that you never see beyond the edge of the content view (or rather, the content view's size plus contentInset). Am I wrong in this assumption?

If so, what should I do to provide this behavior?

Here's my code:

- (void)viewDidLoad
{
    [super viewDidLoad];

    imageView.image = image;
    [imageView sizeToFit];

    float minimumScale = [imageScrollView frame].size.width  / [imageView frame].size.width;

    imageScrollView.contentSize = [imageView frame].size;    
    imageScrollView.maximumZoomScale = 2.0;
    imageScrollView.minimumZoomScale = minimumSca开发者_如何转开发le;

    imageScrollView.zoomScale = imageScrollView.minimumZoomScale;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return imageView;
}

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{
    // nothing
}

EDIT: Solution!

You have to set clipsToBounds = YES on the UIScrollView to get the desired behavior. I didn't find this initially because it is a property of UIView, not of UIScrollView directly.


I saw same on my UIImageView with pinch in and out, in IB I checked on "Clip subviews" and the Image view started behaving better.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜