我想在我当前的项目中实现 session 时间功能。因此,我尝试子类化 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)
}
}
在应用程序委托(delegate)中我这样做了
private let baseWindow = BaseWindow()
var window: UIWindow? {
get {
return baseWindow
}
set {}
}
但问题是 touchesBegan
和 touchesEnded
没有被调用。我无法弄清楚我做错了什么。
最佳答案
除非没有其他人捕获触摸事件,否则不会为您的窗口调用 touchesBegan(_:with:)
API。通常情况并非如此,因为您希望 UI 元素接收触摸。
您想要在窗口子类中实现的是 sendEvent(:_)
并将您的逻辑放在那里。不要忘记调用 super 实现来传递事件。
此方法的一个小注意事项是您会听到一些不是触摸事件的事件,但这可能不是问题,因为它们大多是用户生成的。
关于ios - UIWindow子类: Touches event not get fired,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39254025/