ios - 无法让 CALayer 更新其 drawLayer : DURING a bounds animation

标签 ios objective-c animation core-animation core-graphics

我正在尝试为自定义 UIView 的边界设置动画,同时保持其图层与其父 View 的大小相同。为此,我试图在其父 View 旁边为图层边界设置动画。我需要图层调用 drawLayer:withContext 作为其动画,这样我的自定义绘图将随着边界正确更改大小。

drawLayer 在我开始动画之前被正确调用并正确绘制。但是我无法让图层在边界动画的每个步骤上调用其 drawLayer 方法。相反,它只是调用一次,立即跳到动画最后一帧的“结束边界”。

// 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];

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

最佳答案

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

许多人不知道 Core Animation 有一个 super 酷的功能,它允许您定义自己的图层属性,使它们可以被动画化。我使用的一个例子是给 CALayer 子类一个 thickness 属性。当我使用 Core Animation 为它制作动画时...

CABasicAnimation* ba = [CABasicAnimation animationWithKeyPath:@"thickness"];
ba.toValue = @10.0f;
ba.autoreverses = YES;
[lay addAnimation:ba forKey:nil];

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

在我看来,那可能就是您所追求的那种东西。如果是这样,您可以在此处找到一个可运行的可下载示例:

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

此处讨论(来 self 的书):

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

关于ios - 无法让 CALayer 更新其 drawLayer : DURING a bounds animation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16000798/

相关文章:

python - 从命令提示符运行 Blender。 - 初学者

javascript - 如何在 Javascript 中使预加载动画在 3 秒内消失

ios - 重命名后项目崩溃

ios - 有没有办法从容器应用程序中打开 iMessage 扩展程序?

ios - 单击相应按钮后如何转到标签栏 Controller

objective-c - 在 NSOutlineView 中托管 NSView 的图层

iphone - 支持开发的 iOS 设备的性能是否受到影响?

objective-c - 为什么在处理子类的成员之前调用父类(super class)的viewDidUnload?

ios - 调配一个没有实例的类

ios - 如何在 iOS 中创建侧面板位于中心面板之上的滑出式导航面板?