UIScrollView detect pinch zoom end
Asked Answered
R

4

7

I'm trying to get notified when UIScrollView is pinch zoomed out beyond its minimum zoom limit and is about to animate back, but I'm finding it very difficult. Is there a way I can do this with delegate methods alone or do I need to override UIScrollView's touch handling?

Ralline answered 26/1, 2011 at 20:7 Comment(0)
F
11

Use scrollViewDidZoom: and check if scrollView.zoomBouncing == YES. Then use zoomScale to determine which direction the view is bouncing.

- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
    if (scrollView.zoomBouncing) {
        if (scrollView.zoomScale == scrollView.maximumZoomScale) {
            NSLog(@"Bouncing back from maximum zoom");
        }
        else
        if (scrollView.zoomScale == scrollView.minimumZoomScale) {
            NSLog(@"Bouncing back from minimum zoom");
        }
    }
}
Fonzie answered 16/12, 2013 at 13:45 Comment(0)
A
2

You can use UIScrollView's scrollViewDidZoom delegate method to detect the moment it's about to animate back. You'll see scrollView.zoomScale drop below scrollView.minimumZoomScale while the view is being pinched. Then, as soon as the user releases their fingers, scrollViewDidZoom will be called once again with scrollView.zoomScale == scrollView.minimumZoomScale, but scrollView.zooming == NO.

Capturing this moment is fine and all, but attempting to do anything to preempt the bounce-back-to-minimumZoomScale animation seems to have really odd side effects for me. :(

Amory answered 4/3, 2011 at 4:17 Comment(1)
what if I want to capture a zoomScale other than minimumZoomScale, like if !scrollView.zooming && scrollView.zoomScale < 0.6 {do something} Lute
P
2

In Swift 4.0:

func scrollViewDidZoom(_ scrollView: UIScrollView) {
    if scrollView.zoomScale == scrollView.minimumZoomScale
    {
        print("zoomed out")
    }
}

This will be called exactly when the user has finished zooming and the zoomScale is at its minimum possible value, i.e. when the scroll view is zoomed out completely.

Piperpiperaceous answered 12/1, 2019 at 22:48 Comment(5)
How can I detect the user pinching to zoom out, if they don't zoom out all the way?Rhaetia
@Daniel Springer that zoomScale attribute has all the info you need. It will be decreasing as they zoom out, so check to see if zoomScale has decreased without going all the way out (i.e. all the way to minimumZoomScale). If you still have issues ask the question and link it here, I can share an example of thatPiperpiperaceous
In order to check if it decreased, need I save the previous value to a variable of my own?Rhaetia
Yes exactly. There is nothing built into the scrollView that does this for you, so you would want to track it with your own variable.Piperpiperaceous
Got it. Thanks!Rhaetia
A
0

I did it with UIPinchGestureRecognizer.

-(void)viewDidLoad{
    UIPinchGestureRecognizer *gestureRecognizer = 
     [[[UIPinchGestureRecognizer alloc] initWithTarget:self 
                                                action:@selector(pinched:)] 
                                                               autorelease];
    gestureRecognizer.delegate=self;
    [self.scrollView addGestureRecognizer:gestureRecognizer];
    //your code
}
-(void)pinched:(UIPinchGestureRecognizer*)gestureRecognizer{
    if(gestureRecognizer.state==UIGestureRecognizerStateEnded){
        //pinch ended
        NSLog(@"scale: %f",scrollView.zoomScale);
    }
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
        shouldRecognizeSimultaneouslyWithGestureRecognizer:
                          (UIGestureRecognizer *)otherGestureRecognizer{
    return YES;
}
Aspia answered 12/1, 2013 at 14:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.