ios - 如何获得正确的 z 顺序 CATransform3D

标签 ios uiview catransform3d catransform3drotate

当我将围绕 y 轴的 CATransform3DRotate 应用于两个 UIView 时,它们的 z 顺序似乎颠倒了。已旋转回到 z 轴的 UIView 部分出现在已旋转到屏幕的部分前面。这个动画展示了我的意思:

rotation animation

我如何获得它以便 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/

相关文章:

ios - Objective C 在长字符串中使用 UItextview/NSstring 占位符

ios - 选中单元格时隐藏圆形uiview

iphone - 使用 CATransform3D 透视的折纸过渡

swift - 不可点击的单元格,wen 动画打开

iphone - Xcode 4 跳过将应用程序复制到设备/模拟器

ios - 直接从 AFNetworking 获取 NSData,而不是让它转换为 UIImage

iOS - 需要了解哪些设备可与 CoreBluetooth 配合使用

swift - 加载 Controller 后如何以编程方式添加 View

ios - 无法满足约束时,如何识别 Debug 区域中的 View ?

ios - CATransform3D:计算透视变换后的高度