1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

CGRectContainsPoint Returns Strange Results

Discussion in 'iPhone/iPad Programming' started by Darkroom, Dec 9, 2009.

  1. Guest


    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.
  2. macrumors regular


    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...
  3. macrumors 6502

    What exactly are you expecting [self rect] to return?

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


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

    sorry, that should have been [self frame], was a typo, same problem happens.
  5. macrumors regular


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


    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.

Share This Page