4

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!

Niklas Berglund
  • 3,563
  • 4
  • 32
  • 32
n.evermind
  • 11,944
  • 19
  • 78
  • 122

2 Answers2

11

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

if (CGRectContainsPoint(aFrame, currentPosition))
   // Do something
Vladimir
  • 170,431
  • 36
  • 387
  • 313
2

All you need is CGGeomery reference especially the CGRectContainsPoint function.

Vincent Guerci
  • 14,379
  • 4
  • 50
  • 56