如何在隐式animationanimationCALayer的同时inheritanceanimation属性

我正在尝试使用隐式animation在CALayer上设置一个自定义属性:

[UIView animateWithDuration:2.0f animations:^{ self.imageView.myLayer.myProperty = 1; }]; 

In -actionForKey:方法我需要返回animation来处理插值。 当然,我必须告诉animation如何检索animation的其他参数(即持续时间定时function )。

 - (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]; } } 

任何想法如何实现呢? 我试图在图层属性中寻找animation,但找不到任何有趣的东西。 由于actionForKey:在animation之外被调用,所以我还有一个animation层的问题。

我认为你有自定义属性“myProperty”,你添加到UIView的支持层的自定义层 – 根据文档UIViewanimation块不支持自定义图层属性的animation,并指出需要使用CoreAnimation:

更改视图拥有的层与更改视图本身相同,并且应用于图层属性的任何animation都尊重当前基于视图的animation块的animation参数。 对于自己创build的图层也是如此。 自定义图层对象会忽略基于视图的animation块参数,而是使用默认的“核心animation”参数。

如果要为所创build的图层自定义animation参数,则必须直接使用Core Animation。

进一步的文档指出,UIView只支持一组有限的animation属性:

  • 界限
  • 中央
  • 转变
  • α
  • 背景颜色
  • contentStretch

视图支持一组涵盖许多常见任务的基本animation。 例如,您可以对视图的属性进行animation更改,或使用过渡animation将一组视图replace为另一组视图。

表4-1列出了UIView类的animation属性 – 内置animation支持的属性。

https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/AnimatingViews/AnimatingViews.html#//apple_ref/doc/uid/TP40009503-CH6-SW12

你必须为此创build一个CABasicAnimation。

如果你在actionForKey中返回一个CABasicAnimation,你可以使用CATransactions的解决方法:

 [UIView animateWithDuration:duration animations:^{ [CATransaction begin]; [CATransaction setAnimationDuration:duration]; customLayer.myProperty = 1000; //whatever your property takes [CATransaction commit]; }]; 

只要改变你的actionForKey:方法就是这样的

 - (id<CAAction>)actionForKey:(NSString *)event { if ([event isEqualToString:@"myProperty"]) { return [CABasicAnimation animationWithKeyPath:event]; } return [super actionForKey:event]; } 

Github中有些东西是你不想看的: https : //github.com/iMartinKiss/UIView-AnimatedProperty

我不认为你可以访问的时间,如果你使用:

 [UIView animateWithDuration:duration animations:^{ }]; 

你的代码的其他问题是,UIView的actionForKey:的实现只会返回一个CAAnimation对象,如果在animation块中调用代码的话。 否则,返回null来closuresanimation。 在你的实现中,你总是返回一个CAAnimation,因此改变为该属性将永远是animation的。

你应该使用这个:

 [CATransaction begin]; [CATransaction setAnimationDuration:duration]; [CATransaction setAnimationTimingFunction:timingFunction]; customLayer.myProperty = 1000; //whatever your property takes [CATransaction commit]; 

然后在actionForKey:方法中,使用[CATransaction animationDuration][CATransaction animationTimingFunction]来检索当前的持续时间和计时function。

最简单的方法可以是 myProperty 之前设置自定义图层的其他属性。 喜欢:

 self.imageView.myLayer.myTimingFunction = kCAMediaTimingFunctionEaseInEaseOut; self.imageView.myLayer.myProperty = 1; 

 -(id<CAAction>)actionForKey:(NSString *)event { if ([event isEqualToString:@"myProperty"]) { CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"myProperty"]; [anim setFromValue:@(self.myProperty)]; [anim setKeyPath:@"myProperty"]; [anim setTimingFunction:myTimingFunction]; return anim; } return [super actionForKey:event]; } } 

如果你想获取参数,如持续时间 ,请设置

  [UIView animateWithDuration:2.0f ... 

所以在这种情况下, duration = 2.0f

您可以使用CATransactionvalueForKey 。 CATransaction应该返回上下文的具体值。