简体   繁体   English

CALayer 带旋转 animation

[英]CALayer with rotation animation

I have masked an image like this:我屏蔽了这样的图像:

UIView *maskImage; maskImage = [[UIView alloc] init];
maskImage.backgroundColor = UIColorFromRGB(FTRMaskColor);
maskImage.frame = newFrame;

CALayer *theLayer = [CALayer layer];
theLayer.contents = (id)[[UIImage imageNamed:@"image.png"] CGImage];
theLayer.frame = newFrame;

maskImage.layer.mask = theLayer;

It works fine but the main problem is if I want to rotate the my Ipad, the rotation animation of the view or the layer (I'm not very sure) doesn't work.它工作正常,但主要问题是如果我想旋转我的 Ipad,视图或图层的旋转 animation(我不太确定)不起作用。 It rotates without animation.它在没有 animation 的情况下旋转。 Could you help, please?你能帮忙吗?

To rotate a CALayer :旋转CALayer

NSNumber *rotationAtStart = [myLayer valueForKeyPath:@"transform.rotation"];
CATransform3D myRotationTransform = CATransform3DRotate(myLayer.transform, myRotationAngle, 0.0, 0.0, 1.0);
myLayer.transform = myRotationTransform;        
CABasicAnimation *myAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
myAnimation.duration = kMyAnimationDuration;
myAnimation.fromValue = rotationAtStart;
myAnimation.toValue = [NSNumber numberWithFloat:([rotationAtStart floatValue] + myRotationAngle)];
[myLayer addAnimation:myAnimation forKey:@"transform.rotation"];

myRotationAngle should be in radians. myRotationAngle应该是弧度。 Use negative values for counter-clockwise rotation and positive values for clockwise.逆时针旋转使用负值,顺时针旋转使用正值。

        CAKeyframeAnimation *rotation = [CAKeyframeAnimation animation];
        rotation.keyPath = @"transform.rotation";
        rotation.values = @[ @0.14, @0,@0.2 ,@0];
        rotation.timingFunctions = @[
                                     [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                     [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                     [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                     [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]
                                     ];
        rotation.fillMode            = kCAFillModeForwards;
        rotation.removedOnCompletion = NO;
        rotation.beginTime = AVCoreAnimationBeginTimeAtZero;
        [imageLayer addAnimation:rotation forKey:@"rotation"];

use CA3Daffinetransform to rotate使用 CA3Daffinetransform 进行旋转

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM