【问题标题】:UITapGestureRecognizer only detects a control tapped if tapped in the top half of the control如果在控件的上半部分点击,UITapGestureRecognizer 仅检测点击的控件
【发布时间】:2012-04-13 05:44:18
【问题描述】:

我正在尝试在我的 iOS 应用上实现一种“帮助模式”。当用户点击帮助按钮时,UITapGestureRecognizer 会拦截对屏幕的触摸,如果它在控件上,则会出现一个带有帮助信息的小弹出窗口。

然而,我的问题是,如果我点击控件的上半部分,应用程序只会检测到我点击了控件。我使用 hitTest:withEvent: 来确定触摸了哪个视图:

CGPoint touchPoint = [(UITapGestureRecognizer*)sender locationInView:nil];
UIView* touchedView = [self.view hitTest:touchPoint withEvent:nil];

UILabel 尤其难以成功命中。

视图在顶部包含一个导航栏,但相当标准。有没有人有类似的问题?有谁知道解决办法吗?

【问题讨论】:

    标签: ios hittest uitapgesturerecognizer


    【解决方案1】:

    您需要在视图中获取触摸的位置,而不是在窗口中(当您将 nil 指定为视图时,您会得到)。

    CGPoint touchPoint = [(UITapGestureRecognizer*)sender locationInView:self.view];
    UIView* touchedView = [self.view hitTest:touchPoint withEvent:nil];
    

    【讨论】:

    • 谢谢!你能解释一下为什么当我在窗口中触摸时它会部分工作吗?
    • 在这种情况下,点的 y 值会出现一些偏移——可能与状态栏和/或导航栏的高度有关。有时你很幸运,即使在错误的点上也能找到“正确”的观点。
    • 我有同样的问题,我的 UIImageView 与 UITapRecognizer 只从其高度的后半部分报告 TouchEvents,'虽然我使用 CGPoint location = [(UITapGestureRecognizer*)sender locationInView:sender.view]; CGPoint touchPoint = [(UITapGestureRecognizer*)sender locationInView:sender];
    猜你喜欢
    • 2016-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多