2011-11-10 16 views
1

我正在使用UIView動畫在屏幕上移動按鈕。雖然它是移動的,我有一個NSTimer運行方法使用UIView動畫時的UIButton.frame

checkForCollision 

看起來像這樣:

for(UIButton* b in squares) 
    if(CGRectIntersectsRect(playerSquare.frame, b.frame)) { 
     [self showEndMenu]; 
     break; 
    } 

具有0.05的間隔;我希望該方法在任何時候通知我按鈕移動到另一個路徑上。看起來的問題是,當它檢查UIButton b的幀時,它只會看到按鈕正在移動的幀。

我有按鈕被動畫這樣的:

[UIView beginAnimations:@"" context:nil]; 
[UIView setAnimationDuration:1.3]; 
[UIView setAnimationCurve:UIViewAnimationCurveLinear]; 
view.center = destPoint; 
[UIView commitAnimations]; 

所以,如果destPoint相交CGRectplayerSquare.frame[self showEndMenu]被調用。但是,如果按鈕所在的幀/位置(例如,在動畫的中途)與playerSquare相交,則[self showEndMenu]不會被調用。我不知道如何解決這個問題。如果需要的話,我可以提供更多的代碼。謝謝!

+0

[動畫中的UIView動畫確定中心]的可能重複(http://stackoverflow.com/questions/6545545/uiview-animation-determine-center-during-animation) – jrturton

回答

2

通過動畫化屬性來移動視圖立即將屬性設置爲最終值,就像您找到的那樣。

您可以使用核心動畫層獲取中間值(請參閱http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Animation_Types_Timing/Articles/PropertyAnimations.html#//apple_ref/doc/uid/TP40006672-SW1在手機上輸入對不起鏈接的歉意),但老實說我沒有任何經驗。如果有人這麼做,在這裏看到它會很高興。事實上,快速搜索已經變成了這個問題,你正在尋找確切的答案,我認爲:)

UIView animation determine center during animation

你的另一個選擇是來回移動的按鈕使用定時器,以同樣的方式使用cocos2d移動對象,所以當你每次檢查位置時,你的按鈕就是它們真正的位置。但是這樣你必須自己計算路徑。