pointInView 始终返回 NO

发布于 2024-09-16 12:48:02 字数 448 浏览 3 评论 0原文

我需要知道一个点是否位于给定视图集中的至少一个视图内。为此,我使用了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

も让我眼熟你 2024-09-23 12:48:02

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文