如何在UIScrollView上为zoomScale设置动画

sol*_*eil 6 uiscrollview cabasicanimation ios nsvalue

我需要设置zoomScale回到1上一个UIScrollView,但我需要它来进行动画.我以为我可以使用CABasicAnimation这一点,但NSValue似乎并不具有"valueForFloat"或"valueForNSNumber",所以我不知道用什么animation.fromValue和animation.toValue为CABasicAnimation.有没有更好的方法来解决这个问题?

编辑:我需要添加什么才能工作?

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"];
Run Code Online (Sandbox Code Playgroud)

yon*_*ytu 13

你不需要求助于CAAnimation这个简单的事情,只需使用-[UIScrollView setZoomScale:animated:]pass YES作为第二个参数.

无论如何,如果你想修改动画的某些方面,你可以使用"UIView动画"代替,因为zoomScale它不是直接属于动画的属性CAAnimation.

你尝试过类似下面的东西吗?

[UIView animateWithDuration:2.0
                      delay:0
                    options:UIViewAnimationOptionBeginFromCurrentState
                 animations:^{ [myScrollView setZoomScale:1.0f animated:NO]; }
                 completion:nil];
Run Code Online (Sandbox Code Playgroud)