
点击switch-->跳转到设置页 -->改变通知状态-->返回app刷新switch状态
用户点击开关,switch的状态切换动画会与跳转到设置页的转场动画同时进行,而且会看到switch的状态切换,体验很不好。
解决方案:
在switch上放一个透明button,由button来处理用户事件,而switch只用于展示通知开闭状态
// 在switch上add一个透明button
UIButton *button = [[UIButton alloc] initWithFrame:_notifSwitch.bounds];
[_notifSwitch addSubview:button];
[button addTarget:self action:@selector(switchButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshSwitch) name:UIApplicationWillEnterForegroundNotification object:nil];
- (void)switchButtonClicked {
// 跳转到系统设置
NSURL *settingURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if (@available(iOS 10.0, *)) {
[[UIApplication sharedApplication] openURL:settingURL options:[NSDictionary dictionary] completionHandler:nil];
} else {//iOS10之前
[[UIApplication sharedApplication] openURL:settingURL];
}
}
- (void) refreshSwitch {
UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
self.notifSwitch.on = (setting.types != UIUserNotificationTypeNone);
}
网友评论