可以通過動畫化層的transform.rotation
關鍵路徑的動畫,通過圍繞其Z軸的一個(或多個)完全旋轉的層,這樣的:
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
animation.duration = .25;
animation.fromValue = [NSNumber numberWithFloat:0];
animation.toValue = [NSNumber numberWithFloat:2 * M_PI];
[layer addAnimation:animation forKey:animation.keyPath];
可以繞X或Y軸動畫使用關鍵路徑transform.rotation.x
和transform.rotation.y
。 (關鍵路徑transform.rotation.z
與關鍵路徑transform.rotation
具有相同的效果。)可以同時在單獨的軸上應用多個旋轉。
另一種方式來做到這一點,如果你想繞離軸載體,它可能工作得更好,是使用關鍵幀動畫,就像這樣:
CALayer *layer = [sender layer];
CATransform3D transform = CATransform3DIdentity;
transform.m34 = 1.0/-50;
layer.transform = transform;
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
animation.values = [NSArray arrayWithObjects:
[NSValue valueWithCATransform3D:CATransform3DRotate(transform, 0 * M_PI/2, 100, 1, 100)],
[NSValue valueWithCATransform3D:CATransform3DRotate(transform, 1 * M_PI/2, 100, 1, 100)],
[NSValue valueWithCATransform3D:CATransform3DRotate(transform, 2 * M_PI/2, 100, 1, 100)],
[NSValue valueWithCATransform3D:CATransform3DRotate(transform, 3 * M_PI/2, 100, 1, 100)],
[NSValue valueWithCATransform3D:CATransform3DRotate(transform, 4 * M_PI/2, 100, 1, 100)],
nil];
animation.duration = 2;
[layer addAnimation:animation forKey:animation.keyPath];
你們是不是要旋轉180分或360度?因爲我認爲360度旋轉與完全不旋轉相同。 – htinlinn 2012-07-20 01:16:54
@htinlinn我認爲他正在嘗試通過完整的360度旋轉對他的圖層進行動畫製作。 – 2012-07-20 03:16:30
@robmayoff啊。好。 – htinlinn 2012-07-20 03:51:48