将UITouch位置与UIImageView矩形进行比较 [英] Comparing a UITouch location to UIImageView rectangle

查看:141
本文介绍了将UITouch位置与UIImageView矩形进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码来确定触摸是否在我的表单元格中的图像视图内。但是,它不工作。我比较两个与CGRectContainsPoint然而,它不工作。下面是代码:

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");
     }    
}

感谢您的帮助!

推荐答案


但它不起作用。

However, it doesn't work.

请更具体。


 UITouch *touch = [touches anyObject];


为什么不检查<而不是简单的随机*选择他们?

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

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

*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))


您的框架是在您的superview的坐标系统; [touch locationInView:self] 返回您的坐标系统中的接触点。你想在 bounds 中测试,这是在你的坐标系统中。 本文档介绍了区别

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.

这篇关于将UITouch位置与UIImageView矩形进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆