如何获得正确的z-ordering CATransform3D

时间:2014-07-10 16:15:44

标签: ios uiview catransform3d catransform3drotate

当我将围绕y轴的CATransform3DRotate应用于两个UIView时,它们的z排序似乎是相反的。 UIView中已旋转回z轴的部分出现在已朝屏幕旋转的部分的前面。这个动画显示了我的意思:

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

1 个答案:

答案 0 :(得分:1)

转换中的透视图被反转。通常,您将对变换的m34(第3列,变换矩阵中的第4行)组件使用负值。