iOS全局响应事件

有些控件需要在任何界面都能够被吊起,包括一些辅助工具便于调试,这时候需要设置一个全局的方法调用。

自定义UIWindow,里面重写UIResponder的方法,如:

#if DEBUG
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    if(UIEventSubtypeMotionShake == motion) {
        //可以发送通知等
    }
}
# endif

在AppDelegate当中用自定义的UIWindow作为容器装载rootViewController
场景一,使用storyBoard:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *rootViewController = [storyBoard instantiateInitialViewController];
    self.window = [[CustomWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.window setRootViewController:rootViewController];
    [self.window makeKeyAndVisible];
    
    return YES;
}

场景二,使用代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    CustomViewController *rootViewController = [[CustomViewController alloc] init];
    
    self.window = [[GTWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.window setRootViewController:rootViewController];
    [self.window makeKeyAndVisible];
    
    return YES;
}

你可能感兴趣的:(iOS全局响应事件)