[英]UIWindow subclass: Touches event not get fired
I want to implement session time feature in my current project. 我想在当前项目中实现会话时间功能。 So for that I am trying to subclass
UIWindow
and override the touchesBegan
and touchesEnded
methods. 因此,为此,我尝试将
UIWindow
子类化,并覆盖touchesBegan
和touchesEnded
方法。
class BaseWindow: UIWindow {
convenience init() {
self.init(frame: UIScreen.mainScreen().bounds)
}
private var sessionTimeOutTimer: NSTimer?
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
sessionTimeOutTimer?.invalidate()
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
sessionTimeOutTimer = NSTimer.scheduledTimerWithTimeInterval(60, target: CIAppManager.sharedManger(), selector: #selector(CIAppManager.sessionTimeOut), userInfo: nil, repeats: false)
}
}
In app delegate I did this 在应用程序委托中,我这样做
private let baseWindow = BaseWindow()
var window: UIWindow? {
get {
return baseWindow
}
set {}
}
But the problem is touchesBegan
and touchesEnded
are not get called. 但是问题是没有调用
touchesBegan
和touchesEnded
。 I am not able to figure out what's I am doing wrong. 我无法弄清楚我在做什么错。
The touchesBegan(_:with:)
API will not be called for your window, unless there is no one else to capture the touch event. 除非没有其他人捕获触摸事件,否则不会为您的窗口调用
touchesBegan(_:with:)
API。 This is normally never the case, as you want your UI elements to receive touches. 通常情况并非如此,因为您希望UI元素接收触摸。
What you want to implement in your window subclass is sendEvent(:_)
and put your logic there. 您想要在窗口子类中实现的是
sendEvent(:_)
并将您的逻辑放在这里。 Don't forget to call the super implementation to pass the events. 不要忘记调用超级实现来传递事件。
A small ceveat with this approach is you will hear some events which are not touch events, but that may not be a problem, as they are mostly user generated anyway. 使用此方法的一个小提示是,您会听到一些不是触摸事件的事件,但这可能不是问题,因为无论如何这些事件大多数都是用户生成的。
I have tried to subclass UIWindow and override the methods touchesBegan, touchesEnded these methods are getting called when window is having a rootViewController check the demo project. 我尝试子类化UIWindow并重写touchesBegan,touchesEnded方法,这些方法在窗口具有rootViewController检查演示项目时被调用。 Sample app TouchTest
示例应用程序TouchTest
Also check this method - (void)sendEvent:(UIEvent *)event 还要检查此方法-(void)sendEvent:(UIEvent *)event
As per documentation : you might call this method to dispatch a custom event to the window's responder chain. 根据文档:您可以调用此方法将自定义事件调度到窗口的响应者链。 Window objects dispatch touch events to the view in which the touch occurred, and dispatch other types of events to the most appropriate target object
窗口对象将触摸事件分配给发生触摸的视图,并将其他类型的事件分配给最合适的目标对象
A better way to track sessions would be to register for UIApplication
notifications, such as UIApplicationDidEnterBackgroundNotification
and UIApplicationDidFinishLaunchingNotification
. 跟踪会话的更好方法是注册
UIApplication
通知,例如UIApplicationDidEnterBackgroundNotification
和UIApplicationDidFinishLaunchingNotification
。
The full list of application notifications is available at the bottom of the UIApplication
class reference . 应用程序通知的完整列表位于
UIApplication
类参考的底部。 You can create a session tracker class that listens for these cleanly without interfering with other parts of your code. 您可以创建一个会话跟踪器类,以干净地侦听这些类,而不会干扰代码的其他部分。
Instead of subclassing UIWindow
. 而不是子类化
UIWindow
。 You can add UITapGestureRecognizer
in AppDelegate
's window. 您可以在
AppDelegate
的窗口中添加UITapGestureRecognizer
。
let gestureRecogniger = UITapGestureRecognizer(target: self, action: #selector(yourSelector)) // Implement your selector
gestureRecogniger.cancelsTouchesInView = false
window?.addGestureRecognizer(gestureRecogniger)
If you need to consider other touches other than taps use UIPanGestureRecognizer
and UITapGestureRecognizer
together. 如果您需要考虑点击以外的其他触摸,
UITapGestureRecognizer
同时使用UIPanGestureRecognizer
和UITapGestureRecognizer
。
If you have subviews that have their own gesture recognizers add this extension 如果您的子视图有自己的手势识别器,请添加此扩展名
extension AppDelegate: UIGestureRecognizerDelegate {
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
}
and do 并做
gestureRecogniger.delegate = self
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.