Question

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?

Was it helpful?

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]];

OTHER TIPS

How is touch being defined? I have been using the following (within the standard touchesBegan or touchesMoved), though self is a ViewController.

UITouch *touch = [[[event allTouches] allObjects] objectAtIndex:0];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top