如何在使用隐式动画为CALayer设置动画时继承动画属性 [英] How to inherit animation properties while animating CALayer with implicit animation

查看:127
本文介绍了如何在使用隐式动画为CALayer设置动画时继承动画属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to animate a custom property on a CALayer with an implicit animation:

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

在-actionForKey:方法我需要返回动画来处理插值。当然,我必须以某种方式告诉动画如何检索动画的其他参数(即持续时间计时功能)。

In -actionForKey: method I need to return the animation taking care of interpolating the values. Of course I have to tell somehow the animation how to retrieve the other parameters for the animation (i.e. the duration and the timing 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];
    }
}

有关如何实现这一目标的任何想法?我尝试在图层属性中查找动画,但找不到任何有趣的内容。我也有动画层动画的问题,因为actionForKey:在动画之外被调用。

Any idea on how to achieve that? I tried looking for the animation in the layer properties but could not find anything interesting. I also have a problem with the layer animating since actionForKey: is called outside of animations.

推荐答案

我认为你有一个自定义您添加到UIView的后备层的自定义属性myProperty的图层 - 根据文档UIView动画块不支持自定义图层属性的动画并声明需要使用CoreAnimation:

I reckon that you have a custom layer with you custom property "myProperty" that you added to the backing layer of UIView - according to the Documentation UIView animation blocks does not support the animation of custom layer properties and states the need to use CoreAnimation:


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

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.

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

If you want to customize the animation parameters for layers you create, you must use Core Animation directly.

此外,UIView仅支持有限集的文档说明动画属性
是:

Further the documentation sates that UIView supports just a limited set of animatable properties which are:


  • frame

  • bounds

  • center

  • 转换

  • alpha

  • backgroundColor

  • contentStretch

  • frame
  • bounds
  • center
  • transform
  • alpha
  • 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.

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

Table 4-1 lists the animatable properties—the properties that have built-in animation support—of the UIView class.

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

你必须为此创建一个CABasicAnimation。

You have to create a CABasicAnimation for that.

如果你返回一个CABasicAnimation,你可以对CATransactions进行一种解决方法actionForKey:喜欢那样

You can have sort of a workaround with CATransactions if you return a CABasicAnimation in actionForKey: like that

[UIView animateWithDuration:duration animations:^{
    [CATransaction begin];
    [CATransaction setAnimationDuration:duration];

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

    [CATransaction commit];
  }];

只需将actionForKey:方法更改为类似的内容

Just change your actionForKey: method to something like that

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

如果您不想看看Github中有什么东西: https://github.com/iMartinKiss/UIView-AnimatedProperty

There is something in Github in case you wan't to have a look: https://github.com/iMartinKiss/UIView-AnimatedProperty

这篇关于如何在使用隐式动画为CALayer设置动画时继承动画属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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