文章分享至個人我的技術博客: 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