iphone - 为什么CABasicAnimation会将图层的 View 发送到后面和前面?

标签 iphone objective-c core-animation

有两个大小相似的 UIView。 UIView 一 (A) 最初位于 UIView 二 (B) 之上。当我尝试在 A 层上执行 CABasicAnimation transform.rotation.y 时:

CABasicAnimation *rotateAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
CGFloat startValue = 0.0;
CGFloat endValue = M_PI;
rotateAnimation.fromValue = [NSNumber numberWithDouble:startValue];
rotateAnimation.toValue = [NSNumber numberWithDouble:endValue];
rotateAnimation.duration = 5.0;
[CATransaction begin];
[imageA.layer addAnimation:rotateAnimation forKey:@"rotate"];
[CATransaction commit];

在动画期间,动画层的 UIView (A) 将是:

  1. 发回(A 突然落后于 B)
  2. 旋转...动画后半部分已过
  3. 发送到前面(A 现在再次位于 B 之上)

有没有办法让 A 在整个动画期间都保持在 B 之上?谢谢!

更新:附加项目源:FlipLayer.zip

最佳答案

问题是,在真正的 3D 合成系统中,绕 Y 轴旋转将使 A 层与 B 层相交,这样 A 层的一半应该可见,一半应该在 B 层后面。CoreAnimation 不提供真正的 3D 合成系统。相反,它尝试确定哪个层位于哪个其他层的前面,并完全渲染它而不相交。在您的例子中,在旋转的前半段期间,CoreAnimation 已确定 B 层位于 3D 空间中 A 层的顶部。最简单的解决方案可能是在动画持续时间内将 A 层的 zPosition 设置为layer.bounds.size.width(默认为 0)(或者永久设置,如果它应该始终位于顶部) .

如果您只想在动画持续时间内设置它,您可以为 transform.translation.z 键路径添加第二个动画。这是修改后的 -flip 方法以使用此方法:

- (IBAction)flip:(id)sender {
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
    CGFloat startValue = 0.0;
    CGFloat endValue = M_PI;
    animation.fromValue = [NSNumber numberWithDouble:startValue];
    animation.toValue = [NSNumber numberWithDouble:endValue];
    animation.duration = 5.0;
    [imageOne.layer addAnimation:animation forKey:@"rotate"];
    animation.keyPath = @"transform.translation.z";
    animation.fromValue = [NSNumber numberWithFloat:imageOne.layer.bounds.size.width];
    animation.toValue = animation.fromValue;
    [imageOne.layer addAnimation:animation forKey:@"zPosition"];
}

关于iphone - 为什么CABasicAnimation会将图层的 View 发送到后面和前面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4631993/

相关文章:

ios - UIProgressView不显示进度

objective-c - 在动画完成之前不执行动画之前的代码?

iphone - 引脚移动后 MKMapView 刷新

iPhone UITableViewCell : repositioning the textLabel

iphone - iPhone 文件下载插件 - Phonegap

iphone - CAAnimation 委托(delegate)方法未在委托(delegate)上调用

iphone - 核心动画缩放速度异常缓慢?

Iphone:将 UIImage 通过 segue 传输到另一个 Controller

iphone - 在 uiimagePickerController 完成后呈现一个带有模态的 viewController

ios - UIDatePicker 重新定位到 View 底部