在Objective C中很简单:更新main.m文件并更改UIApplicationMain()参数就足够了
return UIApplicationMain(argc, argv, NSStringFromClass([CustomUIApplication class]), NSStringFromClass([AppDelegate class]));
Run Code Online (Sandbox Code Playgroud)
但是在swift中没有main.m文件,因为指南说
"在全局范围编写的代码用作程序的入口点,因此您不需要主函数."
那么,如何在swift中继承UIApplication?有什么建议吗?
我正在尝试为我正在使用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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
在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] > …Run Code Online (Sandbox Code Playgroud)