Search code examples
objective-ctrackinguitouchcgrect

CGRectContainsPoint() isn't returning TRUE even though the rect contains the point?


I have a UIView with a UIImageView inside it. When someone tries to drag the image view, the image view should follow the person's finger. Below is the code from the UIView's class.

- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {

CGPoint touchPoint = [touch locationInView:self];


    if ( CGRectContainsPoint(self.thumb.frame, touchPoint))

        self.thumb.center = CGPointMake(touchPoint.x, self.thumb.center.y);

return YES;

}

When I start tracking by placing my finger on the imageView and trying to move it, nothing happens. From NSLogging, I figured out that the if statement was never satisfied.

However, when I start tracking outside of the imageView and drag my finger to the imageView, the image view will then follow my finger.

Why doesn't CGRectContainsPoint() work when my finger starts on the image view?


Solution

  • CGPoint touchPoint = [touch locationInView:self.view];
    

    self is not a view (usually), it is a view controller.

    Note that also assumes self.view is the superview of self.thumb. If not, more generally, you can use:

    CGPoint touchPoint = [touch locationInView:[self.thumb superview]];