如何使用CABasicAnimation为图层的框架设置动画?

问题描述:

我想我必须将CGRect转换为对象才能将其传递给fromValue?

I guess I have to convert the CGRect into an object to pass it to fromValue?

这是我尝试的方法,但它不起作用:

This is how I try it, but it doesn't work:

CABasicAnimation *frameAnimation = [CABasicAnimation animationWithKeyPath:@"frame"];
frameAnimation.duration = 2.5;
frameAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
frameAnimation.fromValue = [NSValue valueWithCGRect:myLayer.frame];
frameAnimation.toValue = [NSValue valueWithCGRect:theNewFrameRect];
[myLayer addAnimation:frameAnimation forKey:@"MLC"];


我想您需要更改最后一行它的工作原理:

I guess you need to change your last line to make it work:

[myLayer addAnimation:frameAnimation forKey:@"frame"];

您还可以在图层上设置一个动作,以使所有帧更改都用动画进行动画处理:

You may also set an action to the layer to make all frame changes animated with your animation:

CABasicAnimation *frameAnimation = [CABasicAnimation animation];
frameAnimation.duration = 2.5;
frameAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

myLayer.actions = [NSDictionary dictionaryWithObjectsAndKeys:frameAnimation, @"frame", nil];

在CALayer的 actionForKey:方法引用中,您可以查找图层如何查找动画其属性的动作。

In CALayer's actionForKey: method reference you can find how layer looks up for the actions to animated its properties.