UIScrollView moves the image to the upper left corner when scaling

Looking at this question: Preventing the movement of a UIScrollView in the upper left corner , I have an exact problem.

I'm using this tutorial: http://cocoadevblog.heroku.com/iphone-tutorial-uiimage-with-zooming-tapping-rotation

Back to a similar question, if I turn off the UIScrollViewPanGestureRecognizer, I can no longer pan the enlarged image.

I have a UIImageView in a UIScrollView, and I want to be able to scale and pan the image.

How can I turn off content moving to the upper left corner when scaling?

+1
source share
5 answers

, Autosizing Origin UiScrollView \. Paging Enabled .

+1

- , ( ), contentSize scrollView. , , .

+1

, ? SO. .

0

Subclass UIScrollView and add this method to it:

- (void)layoutSubviews {
    [super layoutSubviews];

    // center the image as it becomes smaller than the size of the screen
    CGSize boundsSize = self.bounds.size;

    //get the subView that is being zoomed
    UIView *subView = [self.delegate viewForZoomingInScrollView:self];

    if(subView)
    {
    CGRect frameToCenter = subView.frame;

    // center horizontally
    if (frameToCenter.size.width < boundsSize.width)
        frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
    else
        frameToCenter.origin.x = 0;

    // center vertically
    if (frameToCenter.size.height < boundsSize.height)
        frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
    else
        frameToCenter.origin.y = 0;

    subView.frame = frameToCenter;
    }

    else
        NSLog(@"No subView set for zooming in delegate");
}
0
source

If you understand correctly, you want to allow scrolling only when zoomed in ImageView, then scrollView.zoomScale > 1. For my application, I use this.

Add the UIScrollView delegation method as follows and verify.

- (void)scrollViewDidScroll:(UIScrollView *)aScrollView
{
    CGFloat offsetY = 0;
    if (aScrollView.zoomScale > 1)
        offsetY = aScrollView.contentOffset.y;

    [aScrollView setContentOffset: CGPointMake(aScrollView.contentOffset.x, offsetY)];
}
0
source

All Articles