只要用户正在交互就显示菜单
我一直在寻找解决方案,但很不幸找到了。如果以前有人问过,也许有人可以指出,或者帮助我提出一个想法。
我有 UIViewController
托管两个自定义视图。第一个自定义视图是全屏的。第二个自定义视图,实际上是一个从屏幕底部滑入视图的菜单,类似于带有自定义动画的工具栏,然后应该滑出。
当前配置:菜单在延迟后滑入视图(视图出现后),并且 NSTimer 开始计数。它会在视图中保留几秒钟,直到 NSTimer 触发另一个方法,并且该方法将菜单收回。
一切正常。
我想要的:如果用户开始与菜单、切换开关、滑块等进行交互,我希望菜单保留在视图中,延迟 NSTimer 的触发,直到用户完成交互。然后收回菜单。
为了实现这一点,我在将菜单滑入视图的方法中使用了 NSTimer,以触发在 X 毫秒后收回菜单的另一个方法。
在收回菜单的方法中,我使计时器无效。
我重写了 tocuhesBegan 方法,以查看交互区域是否落在菜单的边界内,如果是,则 NSTimer 会重置 X 毫秒。
问题:如果用户触摸菜单的背景,这很好,但如果用户与菜单中的开关、按钮和幻灯片交互,menu.view
不会注册触摸,这是有道理的,因为它们是最前线并耗尽触摸事件,因此触摸事件不会向下传递。但另一方面,这也是我的问题!!
我无法覆盖 menu.view
中每个元素中的每个 touchesBegan
方法。我还能如何注册在 menu.view 区域中发生的那些触摸?
我尝试使用 locationInView:nil
来获取窗口内的触摸,但这也没有注册与按钮和滑块等的交互。
谢谢。
I have been looking for a solution, and haven't been lucky to find one. If it has been asked before, mayboe some one can point it out, or help me with an idea.
I have UIViewController
that hosts tow custom views. The first custom view is full screen. The second custom view, is actually is a menu that slides into the view from the bottom of the screen, similar to a toolbar with custom animation, and then it should slides out.
Current confirguation: the menu slides into the view after a delay (after view appears), and a NSTimer starts counting. It remains in the view for few seconds, until the NSTimer fires another method, and that method retracts the menu back.
Everything works fine.
What I want: If user starts interacting with the menu, toggling switches, slider, etc, I want the menu to stay in the view, delaying the firing of NSTimer, until user is done interacting. Then retract the menu.
To accomplish this, I used a NSTimer in the method that slides the menu into the view, to fire the other method that retracts the menu after X msec.
In the method that retracts the menu, I invalidate the timer.
I overrode the tocuhesBegan
method, to see if the area of interaction falls within the boundries of the menu, if so, the NSTimer is resetted for another X msec.
Problem: If user touches the background of the menu, it is fine, but if user interacts with switches, buttons and slides within the menu, the menu.view
does not register touches, which makes sense, because they are the front line and exhaust the touch event, so the touch event does not get passed down the hierarchy. But on the other hand, that is my problem!!
I can't override every touchesBegan
method in every element in the menu.view
. How else can I register for those touches that are happening in the area of the menu.view?
I tried locationInView:nil
to get touches within the window, but that dosen't register interactions with buttons and sliders, etc, either.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
捕获视图层次结构中发生的所有触摸事件的一种解决方案是提供自定义 UIWindow 实现并重写
sendEvent
方法。所有事件首先由UIApplication
接收,然后分派到UIWindow
,然后再分派到包含触摸点的UIView
。通过捕获 UIWindow 级别的事件,您可以确保可以拦截每个事件,而无需覆盖每个控件。创建一个自定义 UIWindow 类并实现
sendEvent
,如下所示:要使用自定义 UIWindow 而不是普通 UIWindow 取决于 UI 的实现方式。如果您使用的是 Storyboard,那么您需要在应用程序委托上实现一个
window
getter 方法,如下所示:否则,请在应用程序委托的
应用程序中创建自定义 UIWindow 的实例,而不是标准实例: didFinishLaunchingWithOptions:
方法。您有多种选择来接收触摸事件的通知。您可以使用 NSNotificationCenter 发送和订阅自定义通知,或者您可以在自定义 UIWindow 上实现委托属性并将 UIViewController 设置为接收触摸事件通知的委托。
One solution to capture all touch events wherever they occur in your view hierarchy is to provide a custom UIWindow implementation and override the
sendEvent
method. All events are received by theUIApplication
first then dispatched toUIWindow
before being dispatched to theUIView
containing the touch point. By catching the events at the UIWindow level you ensure that you can intercept every event without having to override each control.Create a custom UIWindow class and implement
sendEvent
like this:To use your custom UIWindow instead of the vanilla one depends on how your UI is implemented. If you are using Storyboards then you need to implement a
window
getter method on your app delegate like this:Otherwise create an instance of your custom UIWindow instead of the standard one in your app delegate's
application:didFinishLaunchingWithOptions:
method.You have several options for receiving the notification of touch events. You could use NSNotificationCenter to send and subscribe to a custom notification or you could implement a delegate property on your custom UIWindow and set your UIViewController as the delegate to receive notifications of touch events.