2011-07-14 161 views
11

我掩蓋的圖像是這樣的:的CALayer與旋轉動畫

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,視圖的旋轉動畫或層(I不太確定)不起作用。它沒有動畫旋轉。你能幫忙嗎?

+0

簡單地實現旋轉動畫 - [如何使一個旋轉的動畫(http://stackoverflow.com/questions/6075696/how-to-make-a-rotate-animation) – beryllium

回答

16

要旋轉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,但主要問題是CALayer不會在我的iPad旋轉期間旋轉 – Alex

+0

確實,這並不回答這個問題,但它出現在搜索結果中並回答了我的問題,所以+1 「這個答案很有用」:)) –

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