Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to animate zoomScale on a UIScrollView

I need to set zoomScale back to 1 on a UIScrollView, but I need it to be animated. I thought I could use CABasicAnimation for this, but NSValue doesn't seem to have a "valueForFloat" or "valueForNSNumber", so I'm not sure what to use for animation.fromValue and animation.toValue for the CABasicAnimation. Is there a better way to go about this?

EDIT: What do I need to add for this to work?

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"zoomScale"];
        animation.duration = 2.3;
        animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];        
        animation.fromValue = [NSNumber numberWithFloat:myScrollview.zoomScale];
        animation.toValue = [NSNumber numberWithFloat:1.0];

        [myScrollview.layer addAnimation:animation forKey:@"zoomScale"];
like image 773
soleil Avatar asked Jun 11 '13 21:06

soleil


1 Answers

You don’t need to resort to CAAnimation for this simple thing, just use -[UIScrollView setZoomScale:animated:] passing YES as the second parameter.

Anyway, if you wanted to do modify some aspect of the animation you may resort using “UIView animations” instead, since zoomScale is not directly a property animatable by CAAnimation.

Have you tried something like the following?

[UIView animateWithDuration:2.0
                      delay:0
                    options:UIViewAnimationOptionBeginFromCurrentState
                 animations:^{ [myScrollView setZoomScale:1.0f animated:NO]; }
                 completion:nil];
like image 104
yonosoytu Avatar answered Oct 13 '22 18:10

yonosoytu