2013-04-08 53 views
0

我必須做一個小遊戲,其中2張圖像在同一行上水平移動。我想知道它們何時疊加,以便顯示新圖像而不是2.如何知道UIImageView移動的當前位置?

要做到這一點,最好的方法是什麼?

辦法,我想這樣做:

我需要我的UIImageViews的動畫過程中有所瞭解的位置和我會定時嘗試刷新ImageView的時候我發現,2個imageViews接近。

我有這個功能,到目前爲止,移動圖像:

- (void)moveImage:(UIImageView *)image duration:(NSTimeInterval)duration 
     curve:(int)curve x:(CGFloat)x y:(CGFloat)y 
{ 
    // Setup the animation 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:duration]; 
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 

    // The transform matrix 
    CGAffineTransform transform = CGAffineTransformMakeTranslation(x, y); 
    image.transform = transform; 

    // Commit the changes 
    [UIView commitAnimations]; 

} 

謝謝您的幫助!

回答

1

我認爲每秒多次檢查一次職位並不是最好的辦法。如果可以的話,當視圖重疊時,您應該預先計算該時間點!

以任何方式,您不應該使用affinetransform來移動imageview,而只是通過更改視圖的框架或中心屬性來移動imageview的框架!

如果你不能預先計算,這裏有關於如何實現定時器和檢查一些提示:

爲了實現定時器可以使用:

 [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(checkPosition:) userInfo:nil repeats:YES]; 

這將調用每0.1秒功能。

- (void)checkPosition:(NSTimer *)timer 

在這種方法中,你可以訪問imageViews UILayer找出這個觀點的實際位置調用

CGFrame currentFrame = [imageView.layer presentationLayer].frame; 

爲了能夠做到這一點,你必須導入QuartzCore框架!

+0

感謝您的評論尼爾斯! – 2013-04-09 19:50:38

+0

我訪問的位置感謝這篇文章:http://stackoverflow.com/questions/3467911/how-do-we-get-the-coordinates-of-an-uiimageview-programmatically – 2013-04-09 20:00:48

相關問題