Joshua
Joshua

Reputation: 15500

Checking if a touch is within a UIButton's bounds

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.
}

Upvotes: 0

Views: 749

Answers (1)

Ben Gottlieb
Ben Gottlieb

Reputation: 85522

It's not clear what this is a method on, but self is not clear from your code. You might want to do:

currentTouch = [specificTouch locationInView: but];

Upvotes: 1

Related Questions