如何使用CABasicAnimation为图层的框架设置动画? [英] How to animate the frame of an layer with CABasicAnimation?

查看:95
本文介绍了如何使用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.

这篇关于如何使用CABasicAnimation为图层的框架设置动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆