2009-11-10 75 views
1

我是新來張貼在這個論壇上,但不能告訴你讀了多少次幫助我。目前我正在爲iPhone的遊戲工作,它有一個包含在我的gameboardViewController的子視圖中的UIImageViews網格。我在ViewDidLoad上構建網格。好吧到目前爲止。現在我正在使用touchesBegan來找出哪一個UIImageView被觸及,哪種作品。我說那種 - 因爲CGRectContainsPoint似乎給出了一個錯誤的結果,這意味着這個網格的最上面一行被認爲是該函數在我的子視圖矩形之外。獲得更準確的CGRectContainsPoint結果

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *) event 
{ 

    UITouch *touch = [touches anyObject]; 
    CGPoint currentLocation = [[touches anyObject] locationInView:self.gridView]; 

    CGRect gridRect = self.gridView.frame; 
    // Since I have an overlay I want to ignore touches when this subview is visible. 
    if(self.readyScreen.hidden) 
    { 
     /* restrict the touches to only the subview gridView so any stray touches should be ignored.*/ 
     if(CGRectContainsPoint(gridRect,currentLocation)) 
     { 
      UIImageView *theTile = (UIImageView *)touch.view; 
      [self gridItemTouched:theTile]; 
     } 
    } 

}

出於某種原因,這是不夠準確的看到,50×50 UIImageViews的最上面一行是子視圖中。

有什麼建議嗎?

回答

2

將水龍頭位置轉換爲gridView的座標系。但是接下來檢查它是否包含在gridView的框架中,該框架位於其超級視圖座標系中。

你有兩個選擇來解決這個問題:

要麼使用gridView的coodinate系統:

CGPoint currentLocation = [[touches anyObject] locationInView:self.gridView.superview]; 

或者使用上海華盈的coodinate系統:

CGRect gridRect = self.gridView.bounds; 
+0

我明白了感謝幫幫我。我現在按預期工作。請繼續關注我的下一個問題,因爲我確信我會有更多的問題。 – brucemartin 2009-11-11 11:12:41