将UITouch位置与UIImageView矩形进行比较

sky*_*erl 6 iphone cocoa-touch objective-c uikit uitouch

我有以下代码来确定触摸是否在我的表格单元格的图像视图中.但是,它不起作用.我将这两者与CGRectContainsPoint进行了比较,但它不起作用.这是代码:

-(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");
     }    
}
Run Code Online (Sandbox Code Playgroud)

谢谢您的帮助!

Pet*_*sey 26

但是,它不起作用.

请更具体一点.

 UITouch *touch = [touches anyObject];
Run Code Online (Sandbox Code Playgroud)

为什么不检查每一次触摸,而不是简单地随机选择它们?

*文件anyObject说您无法保证会给您哪一个.你甚至不能保证它是随机的; 它每次都可能是同一个对象.墨菲定律说,无论是否是随机的,它都是错误的.

 CGPoint touchLocation = [touch locationInView:self];
 if (CGRectContainsPoint(myImageView.frame, touchLocation))
Run Code Online (Sandbox Code Playgroud)

你frame在superview的协调系统中; [touch locationInView:self]返回触摸点的坐标系.你想bounds在你的坐标系统中进行测试.文档解释了差异.