0
votes

I have a UIImageView inside a UIScrollview, and when I try to pinch and zoom, the image jumps down and right (I think to coordinates 0,0 instead of centered) but stays the same size, and in the same place, until I stop the pinch, and then it pings back to its former centered self.

I got NSLog to print out the zoomScale while zooming, and just kept printing 0.5 until I let go and then it prints 1.0 once.

I'm really at a loss here, all the tutorials on this seem so basic and easy, I don't know where I'm going wrong.

Notes: Both the scrollView and the ImageView are in storyboard, connected to outlets. The viewController that they are in is a UIScrollView delegate that implements viewForZoomingInScrollView:. minimumScale is 1.0, maximumScale is 4.0, though these numbers don't seem to have any effect.

1
Thanks iOS10, that documentation looks really straight forward and easy, but how come it doesn't work for me? I set the delegate, and implement viewForZoomingInScrollView:, what else is there to do?Mirror318

1 Answers

1
votes

Put this code in viewDidLoad

yourScroll.bouncesZoom = YES;
yourScroll.delegate = self;
yourScroll.clipsToBounds = YES;

UITapGestureRecognizer *twoFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTwoFingerTap:)];

[twoFingerTap setNumberOfTouchesRequired:2];

[yourImageView addGestureRecognizer:twoFingerTap];

float minimumScale = 1.0;//This is the minimum scale, set it to whatever you want. 1.0 = default

yourScroll.maximumZoomScale = 4.0;
yourScroll.minimumZoomScale = minimumScale;
yourScroll.zoomScale = minimumScale;
[yourScroll setContentMode:UIViewContentModeScaleAspectFit];
[yourScroll sizeToFit];
[yourScroll setContentSize:CGSizeMake(yourImageView.frame.size.width, yourImageView.frame.size.height)];

Add delegate methods of Scrollview and Gesture

#pragma mark UIScrollViewDelegate methods

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {

    return yourImageView;
}

#pragma mark TapDetectingImageViewDelegate methods

- (void)scrollViewDidZoom:(UIScrollView *)aScrollView {
    CGFloat offsetX = (yourScroll.bounds.size.width > yourScroll.contentSize.width)?
    (yourScroll.bounds.size.width - yourScroll.contentSize.width) * 0.5 : 0.0;
    CGFloat offsetY = (yourScroll.bounds.size.height > yourScroll.contentSize.height)?
    (yourScroll.bounds.size.height - yourScroll.contentSize.height) * 0.5 : 0.0;
    yourImageView.center = CGPointMake(yourScroll.contentSize.width * 0.5 + offsetX,
                                       yourScroll.contentSize.height * 0.5 + offsetY);
}


- (void)handleTwoFingerTap:(UIGestureRecognizer *)gestureRecognizer {
    // two-finger tap zooms out
    float newScale = [previewScroll zoomScale] / ZOOM_STEP;
    CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];
    [yourScroll zoomToRect:zoomRect animated:YES];
}

#pragma mark Utility methods

- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center {

    CGRect zoomRect;

    // the zoom rect is in the content view's coordinates.
    //    At a zoom scale of 1.0, it would be the size of the imageScrollView's bounds.
    //    As the zoom scale decreases, so more content is visible, the size of the rect grows.
    zoomRect.size.height = [previewScroll frame].size.height / scale;
    zoomRect.size.width  = [previewScroll frame].size.width  / scale;

    // choose an origin so as to get the right center.
    zoomRect.origin.x    = center.x - (zoomRect.size.width  / 2.0);
    zoomRect.origin.y    = center.y - (zoomRect.size.height / 2.0);

    return zoomRect;
}