CATransformLayer 不支持隐式动画?
这是我的动画代码:
CGFloat zDistance = 850;
CGFloat scaleFactor = BACK_COVER_WIDTH / self.transformLayer.bounds.size.width;
CATransform3D rotation = CATransform3DMakeRotation(M_PI, 0.0, 1.0, 0.0);
CATransform3D scale = CATransform3DMakeScale(scaleFactor, scaleFactor, 0.0);
CATransform3D transform = CATransform3DConcat(rotation, scale);
transform.m34 = 1.0 / -zDistance;
CGPoint location = CGPointMake(CGRectGetMidX(self.layer.frame), CGRectGetMidY(self.layer.frame));
[CATransaction begin];
[CATransaction setAnimationDuration:1.0];
self.transformLayer.transform = transform;
self.transformLayer.position = location;
[CATransaction commit];
self.transformLayer 是一个 CATransformLayer ,有两个子层,一个用于前面,一个用于后面(我正在创建一个“翻转”效果)。然而,这段代码只是设置位置和变换,没有动画。所以我想也许 transform
不支持隐式动画,所以我把它拿出来,只是尝试设置位置,但这也没有动画(而且我确信 position
支持隐式动画)。
我做错了什么或者 CATransformLayer 不支持隐式动画吗?该文档没有说任何关于它不支持它的内容,所以我假设它支持它。
编辑:这适用于 Mac OS X,不适用于 iOS
Here's my animation code:
CGFloat zDistance = 850;
CGFloat scaleFactor = BACK_COVER_WIDTH / self.transformLayer.bounds.size.width;
CATransform3D rotation = CATransform3DMakeRotation(M_PI, 0.0, 1.0, 0.0);
CATransform3D scale = CATransform3DMakeScale(scaleFactor, scaleFactor, 0.0);
CATransform3D transform = CATransform3DConcat(rotation, scale);
transform.m34 = 1.0 / -zDistance;
CGPoint location = CGPointMake(CGRectGetMidX(self.layer.frame), CGRectGetMidY(self.layer.frame));
[CATransaction begin];
[CATransaction setAnimationDuration:1.0];
self.transformLayer.transform = transform;
self.transformLayer.position = location;
[CATransaction commit];
self.transformLayer
is a CATransformLayer
with two sublayers, one for the front and one for the back (I'm creating a "flip" effect). However, this code just sets the position and transform without animation. So I thought that maybe transform
doesn't support implicit animation, so I took that out and just tried setting the position, but that didn't animate either (and I know for sure that position
supports implicit animation).
Am I doing something wrong or does CATransformLayer
just not support implicit animation? The documentation does not say anything about it not supporting it, so I'm assuming it does.
EDIT: This is for Mac OS X, not iOS
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我最终只使用了显式动画,但 David Duncan 的回答 here 似乎是朝着正确方向迈出的一步。
I ended up just using explicit animation, but David Duncan's answer here seems like a step in the right direction for anyone else who comes across this issue.
与 UIView 关联的所有图层都禁用隐式动画。我有更多详细回复 如果您对细节感到好奇,请回答另一个问题。但简而言之,如果您希望在与视图关联的图层上添加动画,则需要显式执行它们(即使用
-addAnimation:forKey:
附加CAAnimation
对象)。编辑:事实证明这个问题是针对OS X的,无论如何CATransformLayer实际上是一个子层。请忽略这个答案(尽管链接的答案对于 iOS 仍然有用)。
Implicit animations are disabled for all layers associated with
UIView
s. I have a much more detailed response on another question if you're curious as to the details. But in short, if you want animations on a layer associated with a view, you need to do them explicitly (i.e. attachCAAnimation
objects using-addAnimation:forKey:
).Edit: It turns out this question is for OS X, and the CATransformLayer is actually a sublayer anyway. Please disregard this answer (though the linked answer is still useful for iOS).
仅渲染 CATransformLayer 的子图层。所以我的猜测是它的
transform
属性永远不会被应用。您是否尝试过设置sublayerTransform
属性?无论如何,这似乎就是你想要的。Only the sublayers of a
CATransformLayer
are rendered. So my guess is that itstransform
property is never applied. Have you tried setting thesublayerTransform
property instead? It seems like that's what you're going for anyway.如果您添加一个图层并尝试在同一运行循环中对其进行动画处理,则动画不会发生 - 它会立即发生。您需要将图层添加到 CATransaction 中。之后,您可以隐式或显式设置动画。例如,这应该有效:
If you add a layer and try to animate it in the same run loop, the animation won't happen - it'll occur instantly. You need to wrap adding the layer in a CATransaction. Afterwards, you can either implicitly or explicitly animate. For example, this should work: