pointInView 始终返回 NO
我需要知道一个点是否位于给定视图集中的至少一个视图内。为此,我使用了 UIView 的 pointInView 方法,但它总是返回 NO。作为绝望的行为,我检查了视图的中心点是否在视图内部,它也返回了“否”。这是我使用的代码:
BOOL wasPointFound = NO;
NSArray *views = [view subviews];
for (UIView *curView in views)
{
if ([curView pointInside:curView.center withEvent:nil])
{
wasPointFound = YES;
break;
}
}
if (!wasPointFound)
NSLog(@"NO");
else
NSLog(@"YES");
任何人都可以告诉我我做错了什么吗?
谢谢,
I need to know if a point is inside at lease one of the views in a given set of views. For that, I used UIView's pointInView method, but it always returns NO. As an act of despair, I checked if the view's center point is inside the view and it also returned NO. This is the code I've used for that:
BOOL wasPointFound = NO;
NSArray *views = [view subviews];
for (UIView *curView in views)
{
if ([curView pointInside:curView.center withEvent:nil])
{
wasPointFound = YES;
break;
}
}
if (!wasPointFound)
NSLog(@"NO");
else
NSLog(@"YES");
Can anybody please tell me what I'm doing wrong?
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
PointInView 用于检查触摸事件是否在视图内部,这意味着它与窗口相关,而不是与视图相关。 curView.center 是相对于视图而言的,因此使用它很可能会返回 false。
尝试使用 CGPointMake(curView.frame.origin.x+curView.center.x,curView.frame.origin.y+curView.center.y)
这应该返回 YES
PointInView is used to check if touch event is inside a view, that means that it is related to the window and not to the view. Taking curView.center is relative to the view therefore there is a good chance that using it will return false.
Try using CGPointMake(curView.frame.origin.x+curView.center.x,curView.frame.origin.y+curView.center.y)
This should return YES