Rotating a CALayer 90 degrees?

前端 未结 1 543
醉话见心
醉话见心 2021-02-11 17:54

How do I rotate a CALayer 90 degrees? I need to rotate everything include sublayers and the coordinate system.

相关标签:
1条回答
  • 2021-02-11 18:32

    If I'm animating it I use something like this in my apps:

    - (NSObject *) defineZRotation {
        // Define rotation on z axis
        float degreesVariance = 90;
        // object will always take shortest path, so that
        // a rotation of less than 180 deg will move clockwise, and more than will move counterclockwise
        float radiansToRotate = DegreesToRadians( degreesVariance );
        CATransform3D zRotation;
        zRotation = CATransform3DMakeRotation(radiansToRotate, 0, 0, 1.0);  
        // create an animation to hold "zRotation" transform
        CABasicAnimation *animateZRotation;
        animateZRotation = [CABasicAnimation animationWithKeyPath:@"transform"];
        // Assign "zRotation" to animation
        animateZRotation.toValue = [NSValue valueWithCATransform3D:zRotation];
        // Duration, repeat count, etc
        animateZRotation.duration = 1.5;//change this depending on your animation needs
        // Here set cumulative, repeatCount, kCAFillMode, and others found in
        // the CABasicAnimation Class Reference.
        return animateZRotation;
    }
    

    Of course you can use it anywhere, don;t have to return it from a method if that doesn;t suit your needs.

    0 讨论(0)
提交回复
热议问题