当我将围绕 y 轴的 CATransform3DRotate 应用于两个 UIView 时,它们的 z 顺序似乎颠倒了。已旋转回到 z 轴的 UIView 部分出现在已旋转到屏幕的部分前面。这个动画展示了我的意思:
我如何获得它以便 z 排序按预期运行?
我用来生成该动画的代码如下(双 [UIView animateWithDuration]
只需要正确地进行完整旋转):
@interface ViewController ()
@property (strong, nonatomic) UIView *blueSquare;
@property (strong, nonatomic) UIView *redSquare;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.blueSquare = [[UIView alloc] initWithFrame:(CGRect){.origin = {160,234}, .size = {400,300}}];
self.blueSquare.backgroundColor = [UIColor blueColor];
[self.view addSubview:self.blueSquare];
self.redSquare = [[UIView alloc] initWithFrame:(CGRect){.origin = {464,234}, .size = {400,300}}];
self.redSquare.backgroundColor = [UIColor redColor];
[self.view addSubview:self.redSquare];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[UIView animateWithDuration:5 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
CATransform3D transform = CATransform3DIdentity;
transform.m34 = 1.0/900.0; // for perspective
transform = CATransform3DRotate(transform, M_PI, 0, 1, 0);
self.blueSquare.layer.transform = transform;
self.redSquare.layer.transform = transform;
} completion:^(BOOL finished) {
[UIView animateWithDuration:5 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
CATransform3D transform = CATransform3DIdentity;
transform.m34 = 1.0/900.0; // for perspective
transform = CATransform3DRotate(transform, 2*M_PI-0.001, 0, 1, 0);
self.blueSquare.layer.transform = transform;
self.redSquare.layer.transform = transform;
} completion:nil];
}];
}
@end
最佳答案
转换中的透视图是反转的。通常,您会为转换的 m34
(转换矩阵中的第 3 列、第 4 行)组件使用负值。
关于ios - 如何获得正确的 z 顺序 CATransform3D,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24681537/