iOS CGRectContainsPoint Returns Strange Results

Darkroom

Guest
Original poster
Dec 15, 2006
2,448
0
Montréal, Canada
i'm containing touchesMoved: output to the frame of a UIImageView. so if touchesMoved drags outside of the image, it no longer returns a value.

however, CGRectContainsPoint is showing strange results. when adding this UIImageView subclass to the main view, there seems to be some sort of offset of the image's frame position according to the main view, leading to inaccurate and undesirable results.

Code:
CGPoint touchPoint = [touch locationInView:self];

if (CGRectContainsPoint([self rect], touchPoint)
	{
	//this doesn't work.  inaccurate and undesirable results
	}
	
if ((touchPoint.x >= 0) && (touchPoint.x <= self.frame.size.width) && (touchPoint.y >= 0) && (touchPoint.y <= self.frame.size.height))
	{
	//this works and is precise.
	}
i would like to know why this offset is happening with CGRectContainsPoint.
 

drf1229

macrumors regular
Jun 22, 2009
237
0
While I can't say I've ever used CGRectContainsPoint, I know that is not the best way to tell if a touch is inside a view or not. What I would suggest is something along the lines of this:

Code:
if ([touch view]==[theviewyouwant])
{

//Do the code you want...

}
 

Darkroom

Guest
Original poster
Dec 15, 2006
2,448
0
Montréal, Canada
While I can't say I've ever used CGRectContainsPoint, I know that is not the best way to tell if a touch is inside a view or not. What I would suggest is something along the lines of this:

Code:
if ([touch view]==[theviewyouwant])
{

//Do the code you want...

}
sure, which works, but what's the point of CGRectContainsPoint if it's not going to work properly?

What exactly are you expecting [self rect] to return?
sorry, that should have been [self frame], was a typo, same problem happens.
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
I assure you that CGRectContainsPoint is implemented in the obvious way and that it works correctly.

Why do you think your frame starts at 0, 0?

At any rate. You need to print out some of the rects and points to find out why one method works and the other one doesn't.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.