繁体   English   中英

如何在使用隐式动画为CALayer设置动画时继承动画属性

[英]How to inherit animation properties while animating CALayer with implicit animation

我试图使用隐式动画在CALayer上设置自定义属性的动画:

[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:在动画之外被调用。

我估计你有一个自定义属性,你自定义属性“myProperty”,你添加到UIView的支持层 - 根据文档UIView动画块不支持自定义图层属性的动画,并声明需要使用CoreAnimation:

更改视图拥有的图层与更改视图本身相同,并且应用于图层属性的任何动画都会遵循当前基于视图的动画块的动画参数。 对于您自己创建的图层,情况也是如此。 自定义图层对象忽略基于视图的动画块参数,而是使用默认的Core Animation参数。

如果要为您创建的图层自定义动画参数,则必须直接使用Core Animation。

此外,文档声称UIView仅支持一组有限的可动画属性:

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

视图支持一组涵盖许多常见任务的基本动画。 例如,您可以对视图属性进行动画处理或使用过渡动画将一组视图替换为另一组视图。

表4-1列出了可动画的属性 - 具有内置动画支持的属性 - UIView类。

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

你必须为此创建一个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:UIView的实现仅返回CAAnimation对象。 否则返回null以关闭动画。 在您的实现中,您始终返回CAAnimation,因此将始终对更改为该属性进行动画处理。

你应该用这个:

[CATransaction begin];
[CATransaction setAnimationDuration:duration];
[CATransaction setAnimationTimingFunction:timingFunction];

customLayer.myProperty = 1000; //whatever your property takes

[CATransaction commit];

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

最简单的方法可能是 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 ... 

所以在这种情况下持续时间= 2.0f

你可以使用CATransactionvalueForKey CATransaction应返回上下文的特定值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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