[UIView animateWithDuration:2.0f animations:^{ self.imageView.myLayer.myProperty = 1; }];
在-actionForKey:方法我需要返回动画,负责插值.当然,我必须以某种方式告诉动画如何检索动画的其他参数(即持续时间和计时功能).
- (id<CAAction>)actionForKey:(NSString *)event { if ([event isEqualToString:@"myProperty"]) { CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"myProperty"]; [anim setFromValue:@(self.myProperty)]; [anim setKeyPath:@"myProperty"]; return anim; } return [super actionForKey:event]; } }
有关如何实现这一点的任何想法?我尝试在图层属性中查找动画,但找不到任何有趣的内容.我也对图层动画有问题,因为actionForKey:在动画之外调用.
解决方法
Changing a view-owned layer is the same as changing the view itself,
and any animations you apply to the layer’s properties respect the
animation parameters of the current view-based animation block. The
same is not true for layers that you create yourself. Custom layer
objects ignore view-based animation block parameters and use the
default Core Animation parameters instead.If you want to customize the animation parameters for layers you
create,you must use Core Animation directly.
此外,文档声称UIView仅支持一组有限的可动画属性
哪个是:
>框架
>界限
>中心
>变换
>阿尔法
> backgroundColor
> contentStretch
Views support a basic set of animations that cover many common tasks.
For example,you can animate changes to properties of views or use
transition animations to replace one set of views with another.Table 4-1 lists the animatable properties—the properties that have
built-in animation support—of the UIView class.
你必须为此创建一个CABasicAnimation.
如果在actionForKey中返回CABasicAnimation,则可以使用CATransactions进行某种解决方法:就像那样
[UIView animateWithDuration:duration animations:^{ [CATransaction begin]; [CATransaction setAnimationDuration:duration]; customLayer.myProperty = 1000; //whatever your property takes [CATransaction commit]; }];
- (id<CAAction>)actionForKey:(NSString *)event { if ([event isEqualToString:@"myProperty"]) { return [CABasicAnimation animationWithKeyPath:event]; } return [super actionForKey:event]; }
Github有一些东西,如果你不想看看:https://github.com/iMartinKiss/UIView-AnimatedProperty