CGRectContainsPoint Returns Strange Results

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

  1. Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #1
    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.
     
  2. macrumors regular

    drf1229

    Joined:
    Jun 22, 2009
    #2
    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...
    
    }
     
  3. macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #3
    What exactly are you expecting [self rect] to return?

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

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #4
    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

    drf1229

    Joined:
    Jun 22, 2009
    #5
    Maybe it's used for something other than what you're trying to do.
     
  6. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #6
    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