12

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. It rotates without animation. Could you help, please?

Reed Olsen
  • 9,099
  • 4
  • 37
  • 47
Alex
  • 6,957
  • 12
  • 42
  • 48
  • Simply implement rotation animation - [How to make a rotate animation](http://stackoverflow.com/questions/6075696/how-to-make-a-rotate-animation) – beryllium Jul 14 '11 at 09:07

3 Answers3

17

To rotate a 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. Use negative values for counter-clockwise rotation and positive values for clockwise.

Spiros
  • 709
  • 4
  • 9
  • 1
    I can to add a rotate animation to my CALayer but the main problem is the CALayer doesn't rotate during the rotation of my iPad – Alex Jul 15 '11 at 16:21
  • Indeed, this does not answer the question, but it showed up in search results and answered *my* question, so +1 to "This answer is useful" :)) – Jerry Krinock Aug 03 '17 at 19:31
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"];
Jagveer Singh
  • 2,258
  • 19
  • 34
-6

use CA3Daffinetransform to rotate

jothikenpachi
  • 656
  • 6
  • 12