有两个相似大小的UIView。 UIView one(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)将为:
在整个动画期间,有没有办法让A保持在B的顶部?谢谢!
更新:项目来源已附加:FlipLayer.zip
答案 0 :(得分:10)
问题是围绕Y轴的旋转将在真正的3D合成系统中使层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"];
}
答案 1 :(得分:0)
您是否尝试在动画序列中添加[self.view bringSubviewToFront:imageA];
?
答案 2 :(得分:0)
您可以尝试在z轴上进行平移并首先将A向前移动。执行旋转,然后将z设置回其起始值。
此外,除非在转换上设置.m34属性,否则您可能看不到任何关于旋转的视角。尝试这样的事情:
CATransform3D transform = CATransform3DIdentity;
transform.m34 = 1.0f / -300.0f;
transform = CATransform3DRotate(transform, 0.0f, M_PI, 0.0f);
CABasicAnimation *rotateAnimation = [CABasicAnimation
animationWithKeyPath:@"transform"];
rotateAnimation.fromValue = [NSValue
valueWithCATransform3D(CATransformIdentity)];
rotateAnimation.toValue = [NSValue valueWithCATransform3D(transform)];
rotateAnimation.duration = 5.0;
[imageA.layer addAnimation:rotateAnimation forKey:@"rotate"];
您不需要交易冻结。
最诚挚的问候。