暂离需求
当用户在一个子界面一段时间(3分钟)没有进行动作时,则自动返回到初始界面
分析
- 已知
点击事件
在iOS里分为两种。
-
target action
模式 -
UITapGestureRecognizer
手势
已知以上两种模式在点击时候必然调用
UIApplication
的touchesBegan:withEvent:
已知使用
倒计时->取消倒计时
原理,可以用来解决延时请求
以及暂离计数
问题已知可以发送通知到各个界面,用来处理延时之后进行的操作
结论
在touch开始时,取消延时请求
,在touch结束后,开始延时请求
,发送通知
实现
继承UIApplication
,重写sendEvent:
extern NSString *const kTSAFKNotification;
interface TSAplication : UIApplication
@(原创整理)end
.m
@implementation TSAplication
NSString *const kTSAFKNotification = @"TSAFKNotification";
- (void)sendEvent:(UIEvent *)event
{
NSSet *touches = [event allTouches];
for (UITouch *touch in touches) {
switch ([touch phase]) {
case UITouchPhaseBegan:
NSLog(@"UITouchPhaseBegan:%s",__PRETTY_FUNCTION__);
[[self class] cancelPreviousPerformRequestsWithTarget:self];
break;
case UITouchPhaseMoved:
NSLog(@"%s",__PRETTY_FUNCTION__);
break;
case UITouchPhaseEnded:
case UITouchPhaseCancelled:
NSLog(@"%s %tu",__PRETTY_FUNCTION__,[touch phase]);
[self performSelector:@selector(delayEvent) withObject:nil afterDelay:5];
break;
default:
break;
}
}
[super sendEvent:event];
}
- (void)delayEvent
{
[[NSNotificationCenter defaultCenter] postNotificationName:kTSAFKNotification object:nil];
}
在Main
函数里引入
int main(int argc, char * argv[]) {
@autoreleasepool {
return UIApplicationMain(argc, argv, @"TSAplication", NSStringFromClass([AppDelegate class]));
}
}
测试
用2个Controller来模拟
AFK
功能,ViewController
使用UIButton
以及UILabel
的Gesture
事件来控制跳入SubController
,在收到AFK
消息之后,SubController
消失
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(90, 100, 200, 40)];
[btn addTarget:self action:@selector(_openSubController) forControlEvents:UIControlEventTouchUpInside];
[btn setTitle:@"切换到下个界面(button)" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[self.view addSubview:btn];
[self.view setBackgroundColor:[UIColor whiteColor]];
UILabel *gestureLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 180, CGRectGetWidth(self.view.bounds), 100)];
gestureLabel.textAlignment = NSTextAlignmentCenter;
gestureLabel.text = @"切换到下个界面(gesture)";
gestureLabel.textColor = [UIColor blueColor];
gestureLabel.userInteractionEnabled = YES;
[self.view addSubview:gestureLabel];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(_tap:)];
[gestureLabel addGestureRecognizer:tap];
}
测试target action
- (void)_openSubController
{
SubController *sc = [[SubController alloc] init];
[self presentViewController:sc animated:YES completion:^{
}];
}
测试UITapGestureRecognizer
- (void)_tap:(UITapGestureRecognizer*)aTap
{
[self _openSubController];
}
Demo
预览
- ViewController
- SubController