CALayer带有旋转动画

12
我已经像这样遮挡了一张图片:
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;

这段代码工作得很好,但是当我想旋转我的iPad时,视图或者层的旋转动画(我不太确定哪个)就失效了。它没有旋转动画。请帮忙解决一下。


简单实现旋转动画 - 如何制作旋转动画 - beryllium
3个回答

17

旋转一个 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 的单位应该为弧度。使用负值表示逆时针旋转,正值表示顺时针旋转。


1
我可以为我的CALayer添加旋转动画,但主要问题是在我的iPad旋转期间,CALayer不会旋转。 - Alex
确实,这并没有回答问题,但它出现在搜索结果中并回答了我的问题,所以+1给"This answer is useful" :)) - Jerry Krinock

0
        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"];

-6

使用CA3Daffinetransform进行旋转


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接