开发者

Getting a more accurate CGRectContainsPoint result

I'm new to posting on this forum but can't tell you how many times reading it has helped me out. Currently I'm working on a game for the iPhone it has a grid of UIImageViews that are contained within a subview of my gameboardViewController. I build the grid on ViewDidLoad. Ok so far. Now I am using touchesBegan to figure out which one of the UIImageViews was touched, which kind-of works. I say kind-of because CGRectContainsPoint seems to give a false result, meaning the top row of this grid is thought by that function to be outside of my subview rect.

-(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 *)tou开发者_StackOverflow社区ch.view;
            [self gridItemTouched:theTile];
        }
    }

}

For some reason it isn't accurate enough to see that the top row of 50 x 50 UIImageViews are within the subview.

Any suggestions?


You convert the tap location to the gridView’s coordinate system. But then you check if this coodinate it is contained in the gridView’s frame, which is in its superviews coordinate system.

You have two choices to fix this:

Either use gridView’s coodinate system:

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

Or use the superview’s coodinate system:

CGRect gridRect = self.gridView.bounds;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜