简体   繁体   中英

How can I add implicit animation for the 'position' property of a CALayer?

**Note: This is for iOS, not OS X, so implicit animations aren't automatically set up.

I am trying to add implicit animation to a CALayer for the position property.

My (incorrect) code is this:

NSMutableDictionary * actions = [self.view.layer.actions mutableCopy ] ;
if ( !actions ) { actions = [ NSMutableDictionary dictionary ] ; }

CABasicAnimation * anim = [ CABasicAnimation animationForKeyPath:@"position" ] ;

[ actions setValue:anim forKey:@"position" ] ;
self.view.layer.actions = actions ;

My question is, what sort of animation (the anim property, above) ie CABasicAnimation , CATransition should I use for this scenario and how should I configure it?

Grazie

Implicit animations, are triggered by just setting the position property (ie self.view.layer.position = <new position>; ) and Quartz will take care of the rest using your current CATransaction setting.

If you want to use explicit animation (which you seem to be trying to do), I would use CABasicAnimation and do something like this.

    self.view.layer.position = <new position>;
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
    animation.duration = <duration>;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    animation.fromValue = <current position>;
    animation.toValue = <new position>;
    [line addAnimation:animation forKey:@"position"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM