Any way to change zoomToRect duration for UIScrollView?

Can I specify the animation duration [UIScrollView zoomToRect:zoomRect animated:YES] ?

At the moment, it is either fast animated:YES or instant animated:NO .

I would like to specify a duration, for example [UIScrollView setAnimationDuration:2] ; or something similar.

Thanks in advance!

+6
source share
3 answers

Use UIView animation.

It’s a little long to explain, so I hope this little example fixes the situation a bit. Look at the documentation for further instructions.

 [UIView beginAnimations: nil context: NULL]; [UIView setAnimationDuration: 2]; [UIView setAnimationDelegate: self]; [UIView setAnimationDidStopSelector: @selector(revertToOriginalDidStop:finished:context:)]; expandedView.frame = prevFrame; [UIView commitAnimations]; 

This is from a project I'm working on right now, so this is a bit specific, but I hope you can see the basics.

+1
source

I got lucky with this method in a subclass of UIScrollView:

 - (void)zoomToRect:(CGRect)rect duration:(NSTimeInterval)duration { [self setZoomLimitsForSize:rect.size]; if (duration > 0.0f) { [UIView beginAnimations:nil context:NULL]; [UIView setAnimationBeginsFromCurrentState:YES]; [UIView setAnimationDuration:duration]; } [self zoomToRect:rect animated:NO]; if (duration > 0.0f) [UIView commitAnimations]; [self zoomToRect:rect animated:(duration > 0.0f)]; } 

This is kind of a hoax, but it seems to work mostly. Sometimes it fails, but I don't know why. In this case, it simply reverts to the default animation speed.

0
source

Actually these answers are really close to what I used, but I will post it separately, as it is different. Basically, zoomToRect does not work correctly if the target zoom scale matches the current one.

You can try to execute scrollToRect, but I had no luck with that.

Instead, just use contentOffset and set it to zoomRect.origin and the socket in the animation block.

 [UIView animateWithDuration:duration delay:0.f options:UIViewAnimationOptionCurveEaseInOut animations:^{ if (sameZoomScale) { CGFloat offsetX = zoomRect.origin.x * fitScale; CGFloat offsetY = zoomRect.origin.y * fitScale; [self.imageScrollView setContentOffset:CGPointMake(offsetX, offsetY)]; } else { [self.imageScrollView zoomToRect:zoomRect animated:NO]; } } completion:nil]; 
0
source

All Articles