【问题标题】:How to get proper z-ordering CATransform3D如何获得正确的 z 排序 CATransform3D
【发布时间】:2014-07-10 16:15:44
【问题描述】:

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

如何获得它以使 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

【问题讨论】:

  • 你的变换中的视角是不通的。将其更改为负值。
  • @DavidRönnqvist 就是这样,谢谢!我知道我错过了一些简单的事情。如果您将此作为自己的答案,我会将其标记为已接受。

标签: ios uiview catransform3d catransform3drotate


【解决方案1】:

变换中的视角是相反的。通常,您会对变换的m34(变换矩阵中的第 3 列,第 4 行)分量使用负值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-31
    • 2017-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多