文章分享至个人我的技术博客: https://cainluo.github.io/14974988224036.htmlhtml
在以前咱们就讲过苹果爸爸在iOS 10
推出了一个新的UserNotification
框架, 但苹果爸爸的野心不小, 不仅仅推出框架那么简单, 并且连Extension
都给你搞了一个, 哼哼, 我看还有谁~git
这里所演示的项目是和以前同样的, 若是找不到的盆友能够到这里去看看.github
回到咱们的项目, 拷贝一份新的, 而后添加Notification Extension
:微信
添加完Extension
以后, 咱们须要来配置一下Info.plist
文件, 这里咱们要添加点东西:app
AppDelegate.m
文件所添加的Category
保持一致, 我这里是reminder
修改完成后的结果:框架
来通知的效果:ide
上面的东西都搞定了以后, 那么接下来就是要自定义一个小方法:动画
- (void)addShakeAnimation {
CAKeyframeAnimation *frameAnimation = [CAKeyframeAnimation animation];
frameAnimation.keyPath = @"transform.translation.x";
frameAnimation.duration = 1;
frameAnimation.repeatCount = MAXFLOAT;
frameAnimation.values = @[@-20.0, @20.0, @-20.0, @20.0, @-10.0, @10.0, @-5.0, @5.0, @0.0];
frameAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
[self.view.layer addAnimation:frameAnimation
forKey:@"shake"];
}
- (void)removeShakeAnimation {
[self.view.layer removeAnimationForKey:@"shake"];
}
复制代码
addShakeAnimation添加摇摆动画的方法ui
removeShakeAnimation删除摇摆动画的方法spa
效果:
在Notification Extension
里, 有这么一个方法叫作: - didReceiveNotificationResponse:completionHandler:
, 这个方法能够获取通知里的Action
事件, 我这里是这么写的:
- (void)didReceiveNotificationResponse:(UNNotificationResponse *)response
completionHandler:(void (^)(UNNotificationContentExtensionResponseOption))completion {
if ([response.actionIdentifier isEqualToString:@"cancel"]) {
UNNotificationRequest *request = response.notification.request;
NSArray *identifiers = @[request.identifier];
// 根据标识符删除等待通知
[[UNUserNotificationCenter currentNotificationCenter] removePendingNotificationRequestsWithIdentifiers:identifiers];
// 根据标识符删除发送通知
[[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:identifiers];
self.label.text = @"点击了取消按钮";
// 删除动画效果
[self removeShakeAnimation];
// 不隐藏通知页面
completion(UNNotificationContentExtensionResponseOptionDoNotDismiss);
} else {
// 隐藏通知页面
completion(UNNotificationContentExtensionResponseOptionDismiss);
}
}
复制代码
若是不懂这个动画应用的话, 我在度娘里看到了一篇文章, 能够来看看IOS 核心动画之CAKeyframeAnimation
总体的运行效果:
文章里只是简单的讲解, 若是还想了解更多的话, 能够自行去查看WWDC 2016的视频讲解.
项目地址: https://github.com/CainRun/iOS-10-Characteristic/tree/master/6.Notification%20Content%20Extension