2009-11-10 76 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