Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 点不在Rect中,但CGRECT CONTAINSPOINT表示是_Objective C_Iphone_Cocoa Touch_Uiimageview_Core Graphics - Fatal编程技术网

Objective c 点不在Rect中,但CGRECT CONTAINSPOINT表示是

Objective c 点不在Rect中,但CGRECT CONTAINSPOINT表示是,objective-c,iphone,cocoa-touch,uiimageview,core-graphics,Objective C,Iphone,Cocoa Touch,Uiimageview,Core Graphics,如果我有一个UIImageView,并且想知道用户是否点击了该图像。在TouchesStart中,我执行以下操作,但总是以第一个条件结束。窗口处于纵向模式,图像位于底部。我可以点击窗口右上角,但仍然进入第一个条件,这似乎非常不正确 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint location = [touch loc

如果我有一个UIImageView,并且想知道用户是否点击了该图像。在TouchesStart中,我执行以下操作,但总是以第一个条件结束。窗口处于纵向模式,图像位于底部。我可以点击窗口右上角,但仍然进入第一个条件,这似乎非常不正确

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:touch.view];

if(CGRectContainsPoint(myimage.frame, location) == 0){
//always end up here
}
else
{ //user didn't tap inside image}
其值为:

location: x=303,y=102
frame: origin=(x=210,y=394) size=(width=90, height=15)

有什么建议吗?

你的逻辑完全颠倒了。
方法返回bool,即“yes”为true。True不等于0。

首先,您可以接触到:

UITouch *touch = [[event allTouches] anyObject];
接下来,您要检查相对于图像视图的locationInView

CGPoint location = [touch locationInView:self]; // or possibly myimage instead of self.
接下来,CGRectContainsPoint返回一个布尔值,因此将其与0进行比较非常奇怪。应该是:

if ( CGRectContainsPoint( myimage.frame, location ) ) {
   // inside
} else {
   // outside
}

但是,如果self不是myimage,那么myimage视图可能会代替您受到影响-从您的问题中不清楚self是什么对象,它不是所讨论的UIImageView的子类。

0在C中总是错误的。在任何计算上下文中几乎都不正确(我想不出任何使用0的语言是真的,尽管可能有一种)。@Chuck:当然,但在有些情况下,0确实表示“某种程度上的成功”(比如strcmp()),这就是我这样写的原因。不是根据文件:developer.apple.com/documentation/graphicsimaging/…-“如果指定的点位于指定的矩形内,则返回1;否则,返回0。”将上述代码与self.view一起使用,因为self.view是UIViewController。谢谢。4thSpace:完全正确。它返回0(因此,相等为真)因为该点位于矩形外。如果该点位于矩形内,CGRectContainsPoint将返回true(1),等式将返回false。我认为值得一提的是,如果图像未填满屏幕,此处显示的逻辑将不起作用。
[touch locationInView:self]
将为您提供一个相对于视图x、y坐标平面的点,而myimage.frame是相对于其superview的点。我认为,假设myimage是self的子视图,因此如果locationInView:self和myimage.frame位于同一坐标系中。使用locatingInView:my image和my image.bounds可能会这是另一种可能性。