2011-07-14 14 views
11

Mam zamaskowany obraz tak:CALayer z animacją obrotu

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; 

To działa dobrze, ale głównym problemem jest to, czy chcę, aby obrócić mój iPad, animację obrotu widoku lub warstwy (I nie jestem bardzo pewien) nie działa. Obraca się bez animacji. Czy możesz pomóc?

+0

Wystarczy wdrożyć animacja rotacji - [Jak zrobić animację obracania] (http://stackoverflow.com/questions/6075696/how-to-make-a-rotate-animation) – beryllium

Odpowiedz

16

Aby obrócić 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 powinny być w radianach. Użyj wartości ujemnych dla obrotu w lewo i wartości dodatnich dla ruchu wskazówek zegara.

+1

mogę dodać do Obróć animację do mojego CALayera, ale głównym problemem jest to, że CALayer nie obraca się podczas obracania mojego iPada. – Alex

+0

Rzeczywiście, to nie odpowiada na pytanie, ale pojawił się w wynikach wyszukiwania i odpowiedział * moje * pytanie, więc +1 do "Ta odpowiedź jest przydatna" :)) –

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