3D Touch的應(yīng)用在App中主要分為三個(gè)模塊。
一灭贷、Quick Actions
通過按壓主屏幕的應(yīng)用圖片打開應(yīng)用氧腰。
-
定義功能菜單刨肃。功能菜單有兩種定義方式箩帚,靜態(tài)在.plist文件中保存,或者動(dòng)態(tài)通過方法創(chuàng)建盔然。
靜態(tài)創(chuàng)建ShortcutItem.png
// 動(dòng)態(tài)創(chuàng)建
- (void)creatShortcutItem {
//創(chuàng)建系統(tǒng)風(fēng)格的icon
UIApplicationShortcutIcon *icon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeShare];
UIApplicationShortcutItem *item = [[UIApplicationShortcutItem alloc] initWithType:@"UITouchText.share" localizedTitle:@"分享" localizedSubtitle:@"分享副標(biāo)題" icon:icon userInfo:nil];
// 添加到快捷選項(xiàng)數(shù)組
[UIApplication sharedApplication].shortcutItems = @[item];
}
其中愈案,有兩個(gè)必須設(shè)置的值~
UIApplicationShortcutItemType 字符串標(biāo)示鹅搪,區(qū)分用戶的點(diǎn)擊
UIApplicationShortcutItemTitle 字符串丽柿,用于屏幕顯示
其他的副標(biāo)題魂挂、圖標(biāo)之類的馁筐,看需要設(shè)置。
- 在AppDelegate文件中實(shí)現(xiàn)方法
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler { }
在方法中判斷shortcutItem的type的不同來執(zhí)行不同操作果正。
二盟迟、Peek and Pop
按壓預(yù)覽及進(jìn)入的需求~實(shí)現(xiàn)UIViewControllerPreviewingDelegate協(xié)議队萤,協(xié)議中一共有兩個(gè)方法:
NS_CLASS_AVAILABLE_IOS(9_0) @protocol UIViewControllerPreviewingDelegate <NSObject>
// If you return nil, a preview presentation will not be performed
- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location NS_AVAILABLE_IOS(9_0);
- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit NS_AVAILABLE_IOS(9_0);
@end
- 設(shè)置預(yù)覽的視圖
// 預(yù)覽,返回預(yù)覽視圖
- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location {
// 獲取按壓的cell所在行舍杜,[previewingContext sourceView]就是按壓的那個(gè)視圖
NSIndexPath *indexPath = [_tableView indexPathForCell: (UITableViewCell *)[previewingContext sourceView]];
// 調(diào)整不被虛化的范圍赵辕,按壓的那個(gè)cell不被虛化
CGRect rect = CGRectMake(0, 0, self.view.frame.size.width, 40);
previewingContext.sourceRect = rect;
// 設(shè)定預(yù)覽的界面还惠,preferredContentSize決定了預(yù)覽視圖的大小
...
previewingVC.preferredContentSize = CGSizeMake(0.0f, 500.0f);
return previewingVC;
}
值得注意的是:想要響應(yīng) 3D touch 的 view 需進(jìn)行注冊(cè),在我這里救欧,響應(yīng) 3D Touch 的 view 是 cell(感謝 旭丶Joy 的提醒):
// 判斷3DTouch是否可用
if ([[UIDevice currentDevice].systemVersion floatValue] >= 9.0 &&
self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
// 注冊(cè)響應(yīng) 3D Touch 的 View
[self registerForPreviewingWithDelegate:self sourceView:cell];
} else {
NSLog(@"3D Touch 不可用");
}
- 用力按壓進(jìn)入頁面
- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
[self showViewController:viewControllerToCommit sender:self];
}
預(yù)覽時(shí)上移頁面底部有對(duì)應(yīng)操作按鈕笆怠,這個(gè)在對(duì)應(yīng)的頁面.m文件里添加方法即可~
- (NSArray <id <UIPreviewActionItem>> *)previewActionItems {
UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"點(diǎn)我試試" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"action1");
}];
NSArray *actions = @[action1];
return actions;
}
三誊爹、Force Properties
UITouch新增force屬性,可通過檢測(cè)壓力不同并且用CGFloat類型表示办成。實(shí)現(xiàn)并不復(fù)雜搂漠。
這個(gè)我在這次的項(xiàng)目中似乎用不上??。
**好了說完了冷守。 **