我有以下代码来确定触摸是否位于表格单元格的图像视图内。然而,这不起作用。我将两者与 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");
}
}
谢谢您的帮助!
然而,这不起作用。
请说得更具体一些。
UITouch *touch = [touches anyObject];
为什么不检查every触摸,而不是简单地随机*挑选它们?
*的文档anyObject http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSSet_Class/Reference/Reference.html#//apple_ref/occ/instm/NSSet/anyObject说你不能保证它会给你哪一个。你甚至不能保证它是随机的;每次它都可能是同一个对象。墨菲定律说,无论是否随机,它都将是错误的。
CGPoint touchLocation = [touch locationInView:self];
if (CGRectContainsPoint(myImageView.frame, touchLocation))
Your frame
位于您的超级视图的坐标系中;[touch locationInView:self]
返回触摸点your坐标系。您想在以下范围内进行测试bounds
,位于您的坐标系中。文档解释了其中的差异。 http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/WindowsandViews/WindowsandViews.html#//apple_ref/doc/uid/TP40007072-CH8-SW6
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)