2010-02-02 32 views
1

我嘗試這樣做:有一個視圖已經旋轉到某個值,可以說15度。如何繼續使用CAKeyFrameAnimation旋轉視圖?

想象一下旋轉了15度的視圖。

接下來,我只知道,我希望這個視圖將從當前旋轉旋轉到40度。

想象一下如何將視圖從15度旋轉到40度。

實踐中,我想用CAKeyFrameAnimation來做到這一點。但事情是,我所能做的就是從哪裏到哪裏硬指定。沒有像UIView動畫那樣的方便的「拾取當前狀態」的東西,對吧?我會怎麼做呢?

此外,我想爲該旋轉始終指定3個關鍵幀,無論旋轉量需要多少。

想象一下這種看法在15至40度旋轉,使用關鍵幀3這樣做

但一個很大的問題:它可能會發生,那另一個動畫有不同的目標一命嗚呼可以說一個視圖目前是從20度旋轉到100度,並且在動畫之間的某個位置新的動畫被踢,並希望該視角達到15度。用UIView動畫很簡單。如何用CAKeyFrameAnimation做到這一點?

基本上我希望它表現得像UIView的動畫,拿起當前狀態;)

CALayer* theLayer = rotatedView.layer; 

CAKeyframeAnimation* animation; 
animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"]; 

animation.duration = 2; 
animation.repeatCount = 1; 
animation.removedOnCompletion = NO; 
animation.fillMode = kCAFillModeForwards; 

animation.values = [NSArray arrayWithObjects: 
        [NSNumber numberWithFloat:(10.0/180.0) * M_PI], 
        [NSNumber numberWithFloat:(20.0/180.0) * M_PI], 
        [NSNumber numberWithFloat:(30.0/180.0) * M_PI], nil]; 

[theLayer addAnimation:animation forKey:@"transform.rotation.z"]; 

回答

0

您可以獲取使用的起始角度設置您的動畫或者使用類似的代碼我在this answer提供:

CATransform3D rotationTransform = [theLayer transform]; 
float angle; 
if (rotationTransform.m11 < 0.0f) 
     angle = 180.0f - (asin(rotationTransform.m12) * 180.0f/M_PI); 
else 
     angle = asin(rotationTransform.m12) * 180.0f/M_PI; 

或使用簡單的助手的keyPath方法suggested here

float currentAngle = ([[theLayer valueForKeyPath:@"transform.rotation.z"] floatValue] * 180.0f/M_PI); 

如果您將此動畫添加到已在進行中的動畫中,您將需要查詢圖層的presentationLayer的當前角度,而不是直接查詢圖層。 CALayer的presentationLayer反映了圖層中間動畫的當前狀態。