Search code examples
iphoneobjective-ccocoa-touchuitouchcgpoint

Cocoa-Touch: How to find out if CGPoint is in a certain CGRect?


I was wondering if there is a simple way to find out if a certain point is in a certain CGRect?

I have this to get the position of where the user touched the screen:

UITouch *touch = [touches anyObject];    
CGPoint currentPosition = [touch locationInView:self.view];

No I would like to find out if this point is in the following rect:

CGRect aFrame = CGRectMake(0, 100, 320, 200);

The following does obviously not work:

if (currentPosition = aFrame) {//do something}

I'd be grateful for any help. Thanks a lot!


Solution

  • Use CGRectContainsPoint function to determine if point lies inside a rectangle:

    if (CGRectContainsPoint(aFrame, currentPosition))
       // Do something