n.evermind
n.evermind

Reputation: 12004

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!

Upvotes: 4

Views: 2343

Answers (2)

Vincent Guerci
Vincent Guerci

Reputation: 14429

All you need is CGGeomery reference especially the CGRectContainsPoint function.

Upvotes: 2

Vladimir
Vladimir

Reputation: 170849

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

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

Upvotes: 11

Related Questions