6

I have the following code to determine if a touch is within an image view in my table cell. However, it doesn't work. I compared the two with CGRectContainsPoint however, it doesn't work. Here is the code:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event    
{
     // Declare the touch and get it's location

     UITouch *touch = [touches anyObject];

     CGPoint touchLocation = [touch locationInView:self];

     if (CGRectContainsPoint(myImageView.frame, touchLocation))
     {
        NSLog(@"Tapped image view");
     }    
}

Thanks for the help!

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
skylerl
  • 4,030
  • 12
  • 41
  • 60

4 Answers4

26

However, it doesn't work.

Please be more specific.

 UITouch *touch = [touches anyObject];

Why not examine every touch, and not simply a random* pick of them?

*The documentation for anyObject says that you are not guaranteed which one it will give you. You are not even guaranteed that it will be random; it could be the same object every time. Murphy's Law says that, whether it is random or not, it will be the wrong one.

 CGPoint touchLocation = [touch locationInView:self];
 if (CGRectContainsPoint(myImageView.frame, touchLocation))

Your frame is in your superview's co-ordinate system; [touch locationInView:self] returns the touch point in your co-ordinate system. You want to test within bounds, which is in your co-ordinate system. The documentation explains the difference.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
  • expanding: `CGPoint touchLocation = [touch locationInView:self]; if (CGRectContainsPoint(myImageView.frame, touchLocation))` only works if `myImageView` is a direct subview of `self` – João Portela Feb 01 '12 at 18:47
  • 1
    expanding on Joao's comment, you can get a subview's frame relative to the superview (or any view) with `CGRect frameRelativeToView = [myImageView convertRect:myImageView.bounds toView:self]` – chazzwozzer Nov 05 '13 at 21:55
1

The problem is that you need to be calling [touch locationInView:myImageView] to get the point in the image views coordinate system. Then do your check to see if it's within the frame.

bstahlhood
  • 2,006
  • 13
  • 10
1
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self];
if ([touch view]==view) {
view.center=location;
}

write it in the touch moved event. thanx

user986045
  • 11
  • 1
0

Remember that when you're asking a touch for locationInView:, you're getting out a point relative to that view's frame. So, assuming the code snippet you gave was contained in a subclass of UIViewController you should be asking for

CGPoint touchLocation = [touch locationInView:self.view];

Which will give you a point relative to your view. The reason you want a point relative to your current view is because the frame of your image view is also relative to its parent view - the same view. So now it should work.

 if (CGRectContainsPoint(myImageView.frame, touchLocation)) {
    NSLog(@"Tapped image view");
 }
Johnus
  • 722
  • 6
  • 12