2013-02-10 83 views
0

我在CAKeyFrameAnimation上遇到了一個奇怪的問題。iOS CAKeyframeAnimation奇怪的圖像旋轉

下面的代碼:

//Inside the selector method of the button 
CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"position"]; 
[anim setDelegate:self]; 
anim.path = racetrack.path; 
anim.rotationMode = kCAAnimationRotateAuto; 
anim.repeatCount = 0; 
anim.duration = 10.0; 

[carBtn.layer addAnimation:anim forKey:@"race"]; 

動畫工作正常。 按下carBtn(帶汽車背景圖像的按鈕)時會出現問題。 按鈕的圖像無故地旋轉180°(至少沒有理由)。 我已經對自己與創建動畫

carBtn.transform = CGAffineTransformMakeRotation(M_PI); 

後,這行代碼旋轉按鈕,而是爲了一兩秒鐘(動畫開始之前)汽車是相反面對其初始位置。按鈕是這樣創建的:

UIImage* car = [UIImage imageNamed:@"car_image"]; 
carBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 
[carBtn setFrame:CGRectMake(startPoint.x-45, startPoint.y-33, macchina.size.width, macchina.size.height)]; 
[carBtn setBackgroundImage:car forState:UIControlStateNormal]; 
[carBtn setBackgroundImage:car forState:UIControlStateHighlighted]; 
[carBtn addTarget:self action:@selector(carPressed) forControlEvents:UIControlEventTouchUpInside]; 
[self.view addSubview:carBtn]; 

這怎麼可能? 謝謝

回答

0

這就是它的工作原理:視圖的X軸沿着路徑的切線對齊。

如果你明確申請的輪換適用於你,我會堅持。否則,你會發現一個完全解決方案here

+0

感謝您的幫助。不幸的是,在我的情況下,您鏈接的解決方案無效。我試圖使用Caleb的「animate」方法,但UIView animationWithDuration塊的持續時間不起作用。雖然我將持續時間設置爲10秒,但汽車在小於1秒的時間內繞着路徑移動。有關這個問題的任何建議? – nicopasso 2013-02-10 19:10:41

+0

我認爲這個問題是我沿着負x座標的路徑的方向。所以我創建了沿着正x座標的第一個運動的反向路徑。但問題依然存在。按鈕(或圖像)在動畫開始之前旋轉180°。令人沮喪! – nicopasso 2013-02-10 20:41:31