本文介绍了CATransaction旋转图层360度奇怪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想执行无限360度旋转动画,所以我编码为:

I want to perform an infinite 360 degrees rotation animation, so I coded:

- (void)rotate
{
    __weak typeof(self) weakSelf = self;

    [CATransaction begin];
    [CATransaction setDisableActions:NO];
    [CATransaction setCompletionBlock:^{
        [weakSelf rotate];
    }];
    [CATransaction setAnimationDuration:1.0];
    [CATransaction setAnimationTimingFunction:
     [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    self.squareLayer.transform = CATransform3DRotate(self.squareLayer.transform, M_PI, 0, 0, 1);
    [CATransaction commit];
}

M_PI表示180度,因此我相信该图层可以从<$旋转c $ c> 0 到 M_PI * 1 M_PI * 2 ...

M_PI means 180 degrees, so I believe the layer can be rotated from 0 to M_PI * 1, M_PI * 2 ...

但实际上是从 0 M_PI ,然后 M_PI 0

But it turns out to be an rotation from 0 to M_PI, then M_PI to 0.

I可以通过 CABasicAnimation

CABasicAnimation *animatin = [CABasicAnimation animationWithKeyPath:@"transform"];
animatin.duration = 1.0;
animatin.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(0, 0, 0, 1)];
animatin.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0, 0, 1)];
animatin.repeatCount = HUGE_VALF;
animatin.fillMode = kCAFillModeForwards;
[self.squareLayer addAnimation:animatin forKey:@"a"];

上面的代码将图层完美旋转360度。

Codes above rotate the layer 360 degrees perfectly.

但是我对第一个实现感到困惑,为什么它旋转得如此奇怪?

But I am really confused about the first implementation, why it rotates so strange?

推荐答案

这是一个极端的例子。使用隐式动画,您将无法说顺时针旋转此量。您只是在说将转换设置为此值,并为该效果设置动画。好吧,应该如何改变这种变化?运行时必须做出答案,如何做到这一点并不明显。通过M_PI进行的旋转变换在两个方向上都是相同的距离,因此它得出的答案是任意的。如果提供的值 M_PI + 0.1 M_PI-0.1 ;您实际上得到了相反的结果:一个沿顺时针方向旋转,另一个沿逆时针方向旋转。

It's an edge case. With implicit animation, you are not able to say "rotate clockwise this amount". You are merely saying "set the transform to this value, and animate that effect." Well, how should that change be animated? The runtime has to make up an answer, and it isn't obvious how to do that. A transform of rotation by M_PI is the same "distance" in both directions, so the answer it makes up is arbitrary. You can see the problem even more clearly if you supply a value of M_PI+0.1 vs. a value of M_PI-0.1; you actually get reverse results: one keeps turning clockwise, the other keeps turning counterclockwise.

另一方面,使用CABasicAnimation,您有一个from值(隐式或显式) )和取值,因此您可以表达增加的概念(即正差表示顺时针旋转)。如果仅通过设置 byValue 设置动画,则更加清晰。

With CABasicAnimation, on the other hand, you have a from-value (implicit or explicit) and a to-value, so you are able to express the notion "increase" (i.e. a positive difference means turn clockwise). It's even clearer if you animate by setting just the byValue.

这篇关于CATransaction旋转图层360度奇怪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 08:06