2009-08-04 132 views
0

我有一個電話輪。在發言中,它以動畫的形式出現在他的位置上。用CGAffineTransformIdentity逆時針轉爲順時針方向

直到角度小於180°,它會順時針返回。沒問題,用此代碼:

 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
[UIView setAnimationBeginsFromCurrentState:YES]; 
[UIView beginAnimations:nil context:NULL]; 
[UIViewsetAnimationDuration:0.5]; 
wheel.transform = CGAffineTransformIdentity; 
[UIView commitAnimations]; 
}

但它出錯後繼續旋轉完成轉。

我試圖做出動畫像這樣:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
[UIView setAnimationBeginsFromCurrentState:YES]; 
[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.2]; 
wheel.transform = CGAffineTransformRotate(wheel.transform, degreesToRadians(-130)); 
[UIView commitAnimations]; 
[self performSelector:@selector(animatewheelViewToCenter) withObject:nil afterDelay:0.3]; 
} 

- (void)animatewheelViewToCenter{ 
[UIView setAnimationBeginsFromCurrentState:YES]; 
[UIView beginAnimations:nil context:NULL]; 
[UIViewsetAnimationDuration:0.3]; 
wheel.transform = CGAffineTransformIdentity; 
[UIView commitAnimations]; 
}

它的工作原理,但動畫不是流體;該變化是可見的。

回答

1

我不確定touchesEnded(根據旋轉角度)是什麼狀態,而我正在琢磨你選擇degreesToRadians(-130)來嘗試部分地完成它,並期望下一個方法完成剩下的工作。這應該是一個更好的解決方案,希望能產生你期待的結果。我不確定什麼是卡特蘭,爲什麼你在旋轉,所以我只是旋轉輪子。

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.2]; 
    wheel.transform = CGAffineTransformMakeRotation(degreesToRadians(-130)); 
    // I would recommend that the degrees you rotate be half of whatever your rotation is. 
    // this would make the wheel rotate to the home position more evenly 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDidStopSelector:@selector(animatewheelViewToCenter:finished:context:)]; 
    [UIView commitAnimations]; 
} 

- (void)animatewheelViewToCenter:(NSString *)animationID finished:(NSNumber *)finished context:(id)context { 
    [UIView setAnimationBeginsFromCurrentState:YES]; // you might need to make this NO 
    [UIView beginAnimations:nil context:NULL]; 
    [UIViewsetAnimationDuration:0.2]; 
    wheel.transform = CGAffineTransformIdentity; 
    [UIView commitAnimations]; 
} 

編輯:其實,我很可能會令旋轉(在本例-130度)比一半會略多,因爲CGAffineTransformIdentity是要去走最短路徑回到正規,所以如果你完全按照1/2的方式走,它可能不會沿着你想要的正確方向(順時針或逆時針)。

+0

這是正確的,操作系統爲此執行操作的方式是錯誤的,動畫將最短路徑旋轉回縮進,這就是爲什麼您將問題解決的問題 – Daniel 2009-08-04 23:44:51

相關問題