3D Touch是一種立體觸控技術(shù)秋泳,被蘋果稱為新一代多點(diǎn)觸控技術(shù)晕粪,是在Apple Watch上采用的Force Touch颠蕴,屏幕可感應(yīng)不同的感壓力度觸控泡挺。3D Touch辈讶,有Peek Pop 兩種新手勢(shì)。
- 主頁(yè)的 quick action 可以讓你的app 直接進(jìn)入相關(guān)行為娄猫。
- Peek and Pop 允許你快速預(yù)覽內(nèi)容贱除,并導(dǎo)航到對(duì)應(yīng)界面。
- UIPreviewInteraction 讓你可以更精細(xì)地控制 3D Touch媳溺。
話不多說(shuō)月幌,馬上開(kāi)始
1.添加主屏幕快捷菜單 ( Home screen quick action)
(1)靜態(tài)添加快捷菜單
創(chuàng)建一個(gè)靜態(tài)的快捷菜單,只需要簡(jiǎn)單地在Info.plist中添加一個(gè) UIApplicationShortcutItems 的 Array 即可悬蔽。
關(guān)鍵字釋義:
UIApplicationShortcutItemType: 快捷可選項(xiàng)的特定字符串(必填)
UIApplicationShortcutItemTitle: 快捷可選項(xiàng)的標(biāo)題(必填)
UIApplicationShortcutItemSubtitle: 快捷可選項(xiàng)的子標(biāo)題(可選)
UIApplicationShortcutItemIconType: 快捷可選項(xiàng)的圖標(biāo)(可選)
UIApplicationShortcutItemIconFile: 快捷可選項(xiàng)的自定義圖標(biāo)(可選)
UIApplicationShortcutItemUserInfo: 快捷可選項(xiàng)的附加信息(可選)
(2)動(dòng)態(tài)添加快捷菜單
初始化并配置 UIApplicationShortcutItem, UIMutableApplicationShortcutItem 和 UIApplicationShortcutIcon 這三個(gè)類扯躺,并將其添加到 AppDelegate 中的 shortcutItems 屬性即可。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
UIApplicationShortcutIcon *icon1 = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeAdd];
UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeShare];
UIApplicationShortcutItem *item1 = [[UIApplicationShortcutItem alloc]initWithType:@"item1" localizedTitle:@"標(biāo)題1" localizedSubtitle:nil icon:icon1 userInfo:nil];
UIApplicationShortcutItem *item2 = [[UIApplicationShortcutItem alloc]initWithType:@"item2" localizedTitle:@"標(biāo)題2" localizedSubtitle:nil icon:icon2 userInfo:nil];
NSArray *array = @[item1,item2];
[UIApplication sharedApplication].shortcutItems = array;
return YES;
}
2.檢測(cè)設(shè)備是否支持3D Touch
在ViewController.m文件的viewDidLoad中判斷3D Touch是否可用蝎困,防止設(shè)備不支持3D Touch功能時(shí)崩潰
-(void)check3dtouch
{
if(self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable)
{
NSLog(@"3DTouch 可用");
}else{
NSLog(@"3DTouch 不可用");
}
}
3.實(shí)現(xiàn)快捷選項(xiàng)功能
-(void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
{
if ([shortcutItem.localizedTitle isEqualToString:@"標(biāo)題1"]) {
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"測(cè)試1" message:@"我只是測(cè)試" delegate:nil cancelButtonTitle:@"我知道是測(cè)試" otherButtonTitles:@"好", nil];
[alert show];
}
else if([shortcutItem.type isEqualToString:@"標(biāo)題2"]){
//...
}
else if (completionHandler) {
completionHandler(YES);
}
}
4.Peek&Pop
Peek and Pop 將傳統(tǒng)的 Push 操作分為了兩步录语,當(dāng)你的手指按壓某行列表,背景開(kāi)始模式模糊禾乘,然后出現(xiàn)一個(gè)預(yù)覽界面钦无,然后繼續(xù)增加壓力,伴隨著俏皮的彈性動(dòng)畫(huà)盖袭,下一個(gè)界面呈現(xiàn)在你眼前。在 API 中彼宠,這兩部分別被稱為 Preview 和 Commit鳄虱。
- 讓需要預(yù)覽的 ViewController 遵循 UIViewControllerPreviewingDelegate 協(xié)議
- 調(diào)用 registerForPreviewingWithDelegate: sourceView: 注冊(cè)該ViewController
[self registerForPreviewingWithDelegate:self sourceView:self.view];
- 在preview 代理方法中提供一個(gè)預(yù)覽的ViewController,并設(shè)置好 context 的 sourceRect.
- 在 commit 代理方法中凭峡,直接調(diào)用 showViewController(_:sender:) 即可拙已。
//BJBaseViewController
//UIViewControllerPreviewingDelegate
//添加peek功能
-(UIViewController *)previewingContext:(id)context viewControllerForLocation:(CGPoint) point
{
BJOneViewController *childVC = [[BJOneViewController alloc] initWithTitle:@"Peek&Pop"];
childVC.preferredContentSize = CGSizeMake(0.0f,300.f);
NSLog(@"%@",childVC);
return childVC;
}
//實(shí)現(xiàn)pop功能
-(void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
{
[self showViewController:viewControllerToCommit sender:self];
}
//BJOneViewController
//添加預(yù)覽界面的ActionSheet
-(NSArray<id<UIPreviewActionItem>> *)previewActionItems
{
UIPreviewAction * action1 = [UIPreviewAction actionWithTitle:@"選項(xiàng)1" style:0 handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"click1");
}];
UIPreviewAction * action2 = [UIPreviewAction actionWithTitle:@"選項(xiàng)2" style:1 handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"click2");
}];
UIPreviewAction * action3 = [UIPreviewAction actionWithTitle:@"選項(xiàng)3" style:2 handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"click3");
}];
NSArray * actions = @[action1,action2,action3];
return actions;
}