我正在尝试为我正在使用Swift 2开发的应用程序创建一个超时函数,但在Swift 2中,您可以将此代码放入应用程序委托中并且它可以工作,但它无法检测到任何键盘按键、按钮按下、文本字段按下等:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesBegan(touches, withEvent: event);
let allTouches = event!.allTouches();
if(allTouches?.count > 0) {
let phase = (allTouches!.first as UITouch!).phase;
if(phase == UITouchPhase.Began || phase == UITouchPhase.Ended) {
//Stuff
timeoutModel.actionPerformed();
}
}
}
在 Swift 2 之前,我可以将 AppDelegate 子类化为 UIApplication 并像这样覆盖 sendEvent:
-(void)sendEvent:(UIEvent *)event
{
[super sendEvent:event];
// Only want to reset the timer on a Began touch or an Ended touch, to reduce the number of timer resets.
NSSet *allTouches = [event allTouches];
if ([allTouches count] > 0) {
// allTouches count only ever seems to be 1, so anyObject works here.
UITouchPhase phase = ((UITouch *)[allTouches anyObject]).phase;
if (phase == UITouchPhaseBegan || phase == UITouchPhaseEnded)
[[InactivityModel instance] actionPerformed];
}
}
上述代码适用于每一个触摸事件,但是Swift的等效代码只在该UIWindow层次结构中不存在视图时才起作用?
有人知道一种检测应用程序中每一个触摸事件的方法吗?
UIWindow
中有类似的解决方案,但它也停止工作了。可能是因为新的iOS 9分割应用视图等功能。因此,他们对其进行了重新设计,以支持这些新功能-两个打开的应用程序、键盘等。不是100%确定,只是随口想想... - zrzka