开发中遇到这样的需求,需要实现在后台接受到某一事件时无论当前用户处在哪个视图都需要弹出一个Alert.
参考了一些网上的资料,加上自己的思考,总结出两种方案:
1)对UIWindow进行扩展
UIWindow+.h
#define UIEventSubtypeMotionShakeNotification @"UIEventSubtypeMotionShakeNotification"
#import <UIKit/UIKit.h>
@interface UIWindow (Motion)
// @override
- (BOOL)canBecomeFirstResponder;
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event;
@end
UIWindow+.m
//
// UIWindow.m
// Demo
//
// Created by simon on 2018/8/18.
// Copyright © 2018年 simon. All rights reserved.
//
#import "UIWindow+.h"
@implementation UIWindow (Motion)
- (BOOL)canBecomeFirstResponder { NSLog(@"csm canBecomeFirstResponder");
return YES;
}
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *