PDA

View Full Version : CGRectContainsPoint Returns Strange Results




Darkroom
Dec 9, 2009, 06:16 PM
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.


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
Dec 9, 2009, 07:02 PM
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:

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

//Do the code you want...

}

North Bronson
Dec 9, 2009, 07:09 PM
What exactly are you expecting [self rect] to return?

You might be mixing up the bounds and the frame of the view somewhere.

Darkroom
Dec 10, 2009, 12:46 AM
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:

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.

drf1229
Dec 10, 2009, 06:24 AM
sure, which works, but what's the point of CGRectContainsPoint if it's not going to work properly?


Maybe it's used for something other than what you're trying to do.

PhoneyDeveloper
Dec 10, 2009, 09:43 AM
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.