gpt4 book ai didi

core-animation - 从 CATransform3D 到 CGAffineTransform

转载 作者:行者123 更新时间:2023-12-04 13:53:15 26 4
gpt4 key购买 nike

我正在使用以下函数将脉冲效果应用于 View

- (void)pulse {

CATransform3D trasform = CATransform3DScale(self.layer.transform, 1.15, 1.15, 1);
trasform = CATransform3DRotate(trasform, angle, 0, 0, 0);

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
animation.toValue = [NSValue valueWithCATransform3D:trasform];
animation.autoreverses = YES;
animation.duration = 0.3;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.repeatCount = 2;
[self.layer addAnimation:animation forKey:@"pulseAnimation"];

}

我想使用 CGAffineTransform self.transform 而不是 CATransform3D self.layer.transform 获得相同的结果。 这可能吗?

最佳答案

可以转换 CATransform3DCGAffineTransform ,但你会失去一些能力。我发现将层及其祖先的聚合转换转换为 CGAffineTransform 很有用。所以我可以用 Core Graphics 渲染它。约束是:

  • 您的输入将在 XY 平面上被视为平面
  • 您的输出在 XY 平面上也将被视为平坦
  • .m34 的透视/缩短将被中和

  • 如果这对于您的目的来说听起来不错:
        // m13, m23, m33, m43 are not important since the destination is a flat XY plane.
    // m31, m32 are not important since they would multiply with z = 0.
    // m34 is zeroed here, so that neutralizes foreshortening. We can't avoid that.
    // m44 is implicitly 1 as CGAffineTransform's m33.
    CATransform3D fullTransform = <your 3D transform>
    CGAffineTransform affine = CGAffineTransformMake(fullTransform.m11, fullTransform.m12, fullTransform.m21, fullTransform.m22, fullTransform.m41, fullTransform.m42);

    您将希望首先在 3D 转换中完成所有工作,例如从您的超层连接,然后最后转换聚合 CATransform3DCGAffineTransform .鉴于图层一开始是平坦的并渲染到平坦的目标上,我发现这非常合适,因为我的 3D 旋转变成了 2D 剪切。我还发现牺牲透视是可以接受的。没有办法解决这个问题,因为仿射变换必须保留平行线。

    例如,要使用 Core Graphics 渲染 3D 变换层,您可以连接变换(考虑 anchor !),然后转换为仿射,最后:
        CGContextSaveGState(context);
    CGContextConcatCTM(context, affine);
    [layer renderInContext:context];
    CGContextRestoreGState(context);

    关于core-animation - 从 CATransform3D 到 CGAffineTransform,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10497397/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com