views:

41

answers:

2

Is there any way to specify a duration for the animation of [UIScrollView zoomToRect:zoomRect animated:YES]?

At the moment it's either fast (animated:YES) or instant (animated:NO).

I'd like to specify a duration, eg [UIScrollView setAnimationDuration:2]; or something similar.

Thanks in advance!

A: 

Use the UIView's animations.

It's a bit long to explain, so I hope this little example will clear things up a bit. Look at the documantation 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];

It's from a project I'm currently working on so it's a bit specific, but I hope you can see the basics.

tadej5553
A: 

I've had luck with this method in a UIScrollView subclass:

- (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)];
}

It's kinda cheating, but it seems to mostly work. Occasionally it does fail, but I don't know why. In this case it just reverts to the default animation speed.

Dave Batton