Checking if a touch is within a UIButton's bounds.

Posted by Joshua on Stack Overflow See other posts from Stack Overflow or by Joshua
Published on 2010-04-18T12:19:50Z Indexed on 2010/04/18 12:43 UTC
Read the original article Hit count: 280

Filed under:
|

I am trying to make an if statement which will check whether the users touch is within a UIButton's bounds. I thought this would be an easy affair as UIButton is a subclass of UIView, however my code doesn't seem to work. This is the code I have been using.

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    NSArray *array = [touches allObjects];
    UITouch *specificTouch = [array objectAtIndex:0];
    currentTouch = [specificTouch locationInView:self.view];
    if (CGRectContainsPoint(but.bounds, currentTouch)) {
        //Do something is in bounds.
    }
    //Else do nothing.
}

© Stack Overflow or respective owner

Related posts about cocoa-touch

Related posts about uikit