core-animation - 从 CATransform3D 到 CGAffineTransform

标签 core-animation core-graphics

我正在使用以下函数将脉冲效果应用于 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/

    相关文章:

    ios4 - iOS 4.2 : Flip image using block animations

    ios - 立即调用 Swift UIView animateWithDuration 完成闭包

    iphone - iOS:使用 UIView 的 'drawRect:' 与其层的委托(delegate) 'drawLayer:inContext:'

    ios - 如何在 Swift 中将图像转换为渐进式 JPEG?

    ios - 使用 Core Graphics 在 UIImageView 上绘制一个矩形

    iPhone Core Graphics subview 的粗虚线

    cocoa - 手动绘制NSView的 subview

    ios6 - CATransform3DMakeTranslation 参数

    core-graphics - 在 ARC 上使用 __bridge 进行 CoreGraphics 渐变时应用程序崩溃

    ios - 如何制作类似于iphone删除动画的摆动动画