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?
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");
}
}
}
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. :(
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.
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;
}
© 2022 - 2024 — McMap. All rights reserved.
if !scrollView.zooming && scrollView.zoomScale < 0.6 {do something}
– Lute