如何使用CATransform3DMakeRotation从左向右旋转视图

时间:2013-02-07 08:15:29

标签: iphone ios calayer catransform3d

我知道如何从右向左旋转视图

CATransform3D transform = CATransform3DIdentity;
transform = CATransform3DMakeRotation(M_PI, 0.0f, 1.0f, 0.0f);
transform.m34 = 0.0015;
view.layer.transform = transform;

但是我无法从左向右旋转它。 请帮帮我。

4 个答案:

答案 0 :(得分:3)

我不确定你的意思是“从左到右”和“从右到左”。无论如何,正如您所发现的那样,您无法通过设置变换矩阵来控制动画的方向,因为-M_PI和+ M_PI都具有相同的最终效果,因此Core Animation无法可靠地知道您的哪种方式希望它旋转。

相反,you can animate the transform.rotation.y key path of the layer。这样做有点复杂,特别是因为您想要为视图的图层设置动画而不是独立图层(不受视图控制)。

无论如何,这是我测试的代码:

- (IBAction)rotateButtonWasTapped:(id)sender {
    // Establish the perspective to be used during the animation.
    CATransform3D transform = CATransform3DIdentity;
    transform.m34 = 0.0015;
    self.imageView.layer.transform = transform;

    static CGFloat kAngle = -M_PI;

    [CATransaction begin]; {
        // After the animation completes, make the transform “permanent”.  Otherwise it
        // will be undone when the animation is removed from the layer.
        [CATransaction setCompletionBlock:^{
            self.imageView.layer.transform = CATransform3DRotate(transform, kAngle, 0, 1, 0);
        }];

        // Animate the rotation using Core Animation's extension to Key Value Coding.
        // The sign of kAngle determines the direction of rotation.
        CABasicAnimation *animation = [CABasicAnimation
            animationWithKeyPath:@"transform.rotation.y"];
        animation.fromValue = @0;
        animation.toValue = @(kAngle);
        animation.duration = 1;
        [self.imageView.layer addAnimation:animation forKey:animation.keyPath];
    } [CATransaction commit];
}

这使视图旋转,使得视图的左边缘在旋转期间“更接近”移动到用户。如果这不是“从左到右”的含义,请将kAngle的定义更改为M_PI,它将向另一个方向旋转。我已经测试了两种方式。您甚至可以在一个动画中多次旋转图层。例如,尝试将kAngle定义为-3 * M_PI

答案 1 :(得分:0)

我在功能文档中没有找到任何内容,但我认为你可以将旋转角度设置为负向旋转到另一个方向。

答案 2 :(得分:0)

如果你想旋转视图,回到初始位置......只需用:

transform = CATransform3DMakeRotation(0, 0.0f, 1.0f, 0.0f)

或者你可以“结合”更多..

CATransform3D transform = CATransform3DIdentity;
transform = CATransform3DMakeRotation(-M_PI_2, 0.0f, 1.0f, 0.0f);
transform = CATransform3DMakeRotation(0, 0.0f, 1.0f, 0.0f);
transform.m34 = 0.0015;
view.layer.transform = transform;

您可以尝试...使用CATransform3D transform = starLayer.transform;代替CATransform3D transform = CATransform3DIdentity;

答案 3 :(得分:0)

- (无效)updateTimer {     如果(更新125)     更新=更新+ 1;

NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate];
NSTimeInterval interval = now - start;
int inter = (int)(interval*update);
inter*=10;

milliSec = (inter) % 1000;
seconds = (inter/1000) % 60;
minutes = (inter/60000) % 60;

timeValue = oldTimeValue+inter;

// Set Digital clock

// Set Analog clock hands

secHand.layer.transform = CATransform3DMakeRotation (Degrees2Radians(-(timeValue%1000)*360/1000), 0, 0, 1);

}