2009-09-08 148 views
1

我使用SnowFall應用程序,其中圖像從屏幕頂部下降到底部。這個應用程序使用動畫來實現這一任務。在iPhone中動畫時獲取圖像的幀座標

//把小片放在我們的主視圖中 [self.view addSubview:flakeView];

[UIView beginAnimations:nil context:flakeView]; 
// set up how fast the flake will fall 
[UIView setAnimationDuration:4 * speed]; 

// set the postion where flake will move to 
//flakeView.frame = CGRectMake(endX, 500.0, 25.0 * scale, 25.0 * scale); 
flakeView.frame = CGRectMake(endX, 500.0,16, 16); 

// set a stop callback so we can cleanup the flake when it reaches the 
// end of its animation 
[UIView setAnimationDidStopSelector:@selector(onAnimationComplete:finished:context:)]; 
[UIView setAnimationDelegate:self]; 
[UIView commitAnimations]; 

我的問題是我需要捕獲他們移動的圖像幀。

有什麼辦法可以讓我找到屏幕上圖像的當前位置。

感謝

編輯:我試圖使用方法:CGRectIntersectsRect(幀1,幀2),其中幀1是該動畫和幀2是一個物體圖像。如果圖像與物體交叉,我必須做些什麼。

回答

6

當您應用這樣的動畫時,視圖的框架(以及視圖的CALayer框架)立即反映最終位置。

要在動畫期間獲取當前位置的框架,您需要視圖的圖層presentationLayer

CALayer *layer = flakeView.layer.presentationLayer; 
CGRect layerFrame = layer.frame; 
BOOL intersects = CGRectIntersectsRect(layerFrame, frame2); 
+0

非常感謝! 但是在I行顯示錯誤:「訪問屬性的未知presentationLayer組件」 在這裏, flakeView是UIImageView, UIImageView * flakeView = [[UIImageView alloc] initWithImage:flakeImage]; And Frame to是另一個UIImageView作爲UIImageView * frame2; 我是否需要在開始動畫之前或在setAnimationDidStopSelector方法中編寫代碼。 請幫我 – iPhoneDev 2009-09-08 10:59:51

+0

動畫發生在[UIView commitAnimations];和setAnimationDidStopSelector。如果要在動畫製作過程中檢查圖層的位置,則需要創建一個線程或計時器或在動畫過程中檢查圖層位置的內容。 – iKenndac 2009-09-08 11:35:41

+0

再次感謝:)您的解決方案工作。無論我的問題還在那裏。我在定時器中生成圖像,所以CGRectIntersectsRect(layerFrame,frame2);弄糊塗要檢查的圖像。但是非常感謝你。 – iPhoneDev 2009-09-08 15:28:00