我正在尝试为自定义 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
属性值在每个时刻(动画过程中的中间值)。
在我看来,那可能就是您所追求的那种东西。如果是这样,您可以在此处找到一个可运行的可下载示例:
此处讨论(来 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/