简体   繁体   English

如何在UIScrollView下获取UIView来检测触摸?

[英]How to get a UIView under a UIScrollView to detect touches?

I have a UIScrollView ontop of my UIViewController recreating an effect like in the Gowalla iPhone app when you're on a spot's page. 我有一个UIScrollView ontop我的UIViewController在你现场的页面上重新创建像Gowalla iPhone应用程序中的效果。 Under my scroll view I have a button that I want to be able to perform it's action even when the scroll view's frame covers it up (where it's ontop of the button, the scroll view's clear). 在我的滚动视图下,我有一个按钮,我希望能够执行它的动作,即使滚动视图的框架覆盖它(按钮的顶部,滚动视图清晰)。 How would I do something like this? 我该怎么办? Is it possible? 可能吗? (it has to be, Gowalla [somehow] did it) (必须是,Gowalla [不知何故]做到了)

As for me, I will transfer touch event to another view by override following methods. 至于我,我将通过覆盖以下方法将触摸事件转移到另一个视图。

  • – touchesBegan:withEvent: - touchesBegan:withEvent:
  • – touchesMoved:withEvent: - touchesMoved:withEvent:
  • – touchesEnded:withEvent: - touchesEnded:withEvent:
  • – touchesCancelled:withEvent: - touchesCancelled:withEvent:

Like this way, 像这样,

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
     // pass touch event by default implement.
     [super touchesBegan:touches withEvent:event]; 

     // redirect the touch events. 
     [anotherView touchesBegain:touches withEvent:event];    
}

hitTest:withEvent: is used to decide which view should response touch event on the view hierarchy tree. hitTest:withEvent:用于决定哪个视图应该响应视图层次结构树上的触摸事件。 If the view doesn't want to response touch event, it will return nil in hitTest. 如果视图不想响应触摸事件,它将在hitTest中返回nil。 As result the above touch event methods won't be called. 结果,不会调用上述触摸事件方法。

I have no idea what this "Gowalla" app might do; 我不知道这个“Gowalla”应用程序可能做什么; hopefully, your description of "a button behind the scroll view, that you can touch 'through' the scroll view" is accurate. 希望您对“滚动视图后面的按钮,您可以通过滚动视图”进行触摸的描述是准确的。

If your button behind the scroll view can be sized to fill the entire contentSize area of the scroll view without screwing up your interface, the easiest solution would be to just put it inside the scroll view (under all the other views) and do just that. 如果滚动视图后面的按钮大小可以填充滚动视图的整个contentSize区域而不会搞砸你的界面,最简单的解决方案就是将它放在滚动视图中(在所有其他视图下)并执行此操作。

Otherwise, your best bet is probably to create a custom view with a clear background to be placed in the scroll view as above. 否则,您最好的选择可能是创建一个具有清晰背景的自定义视图,以放置在滚动视图中,如上所示。 The easy solution is to have the custom view (probably a UIControl) just do whatever touching the button does. 简单的解决方案是让自定义视图(可能是UIControl)只需触摸按钮即可。 If that's not possible for some reason, your best option would be to override hitTest:withEvent: on the custom view to return the underlying button. 如果由于某种原因这是不可能的,那么最好的选择是在自定义视图上覆盖hitTest:withEvent:以返回基础按钮。 I'd be wary of overriding hitTest:withEvent: on the scroll view itself, as that might interfere with scrolling. 我要警惕在滚动视图本身上覆盖hitTest:withEvent:因为这可能会干扰滚动。

Try adding the button on top of the scroll view. 尝试在滚动视图顶部添加按钮。 The only problem with that is if you hit the button, you will not be able to interact with the scrollView, but it will still be visible. 唯一的问题是,如果你点击按钮,你将无法与scrollView交互,但它仍然是可见的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM