无法让CALayer更新其drawLayer:DURING一个边界动画

zak*_*ces 5 animation core-animation core-graphics objective-c ios

我正在尝试为自定义UIView的边界设置动画,同时保持其图层与其父视图的大小相同.为此,我试图在其父视图旁边设置图层边界的动画.我需要图层来调用drawLayer:withContext AS它的动画,所以我的自定义绘图将正确地改变大小和边界.

drawLayer被正确调用并在我开始动画之前正确绘制.但我不能让图层在边界动画的每一步调用它的drawLayer方法.相反,它只是将其称为ONCE,立即跳转到动画最后一帧的"结束边界".

// self.bg is a property pointing to my custom UIView
self.bg.layer.needsDisplayOnBoundsChange = YES;
self.bg.layer.mask.needsDisplayOnBoundsChange = YES;

[UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionCurveEaseOut|UIViewAnimationOptionAutoreverse|UIViewAnimationOptionRepeat animations:^{

    [CATransaction begin];
    self.bg.layer.bounds = bounds;
    self.bg.layer.mask.bounds = bounds;
    [CATransaction commit];

    self.bg.bounds = bounds;
} completion:nil];
Run Code Online (Sandbox Code Playgroud)

为什么边界不报告其动画(不仅仅是最终帧)的变化?我究竟做错了什么?

mat*_*att 3

这可能有帮助,也可能没有帮助......

许多人没有意识到 Core Animation 有一个超酷的功能,它允许您以可以动画的方式定义自己的图层属性。我使用的一个示例是为 CALayer 子类提供一个thickness属性。当我用 Core Animation 为其制作动画时......

CABasicAnimation* ba = [CABasicAnimation animationWithKeyPath:@"thickness"];
ba.toValue = @10.0f;
ba.autoreverses = YES;
[lay addAnimation:ba forKey:nil];
Run Code Online (Sandbox Code Playgroud)

...效果是在整个动画中重复调用drawInContext:or drawLayer:...,允许我根据每个时刻的当前 thickness属性值(动画过程中的中间值)重复更改图层的绘制方式。

在我看来,这可能就是你所追求的东西。如果是这样,您可以在这里找到一个有效的可下载示例:

https://github.com/mattneub/Programming-iOS-Book-Examples/tree/master/ch17p498customAnimatableProperty

此处的讨论(来自我的书):

http://www.apeth.com/iOSBook/ch17.html#_making_a_property_animatable