iOS 3D touch 使用技巧

第一個 在桌面中3d Touch 打開菜單  數組

因爲本人純屬代碼黨,本次實現方法也只使用代碼實現微信

到達到這個效果並不難,只須要在appdelegate中實現如下代碼便可 ,固然也有缺點,就是這個app沒運行過的話是用不了3dtouch呼出菜單app

 1 - (void)setting3DTouchModule{
 2     // 判斷系統版本大於9.0再設置 (若不判斷 在低版本系統中會崩潰)
 3     if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0){
 4         
 5         // 自定義圖標
 6         UIApplicationShortcutIcon *icon1 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"圖片名稱"];
 7         
 8         UIApplicationShortcutItem *shortItem1 = [[UIApplicationShortcutItem alloc] initWithType:@"item類型1" localizedTitle:@"item標題1" localizedSubtitle:@"子標題1" icon:icon1 userInfo:nil];
 9         
10         UIApplicationShortcutItem *shortItem2 = [[UIApplicationShortcutItem alloc] initWithType:@"item類型2" localizedTitle:@"item標題2" localizedSubtitle:@"子標題2" icon:[UIApplicationShortcutIcon iconWithType: UIApplicationShortcutIconTypeCompose] userInfo:nil];
11         
12         // item 數組
13         NSArray *shortItems = [[NSArray alloc] initWithObjects: shortItem1,shortItem2, nil];
14         
15         // 設置按鈕
16         [[UIApplication sharedApplication] setShortcutItems:shortItems];
17     }
18     
19 }
20 // 經過3dtouch菜單啓動 後回調
21 - (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler{
22     // 能夠經過標題 字符串判斷 來確認 是哪一個item
23     if ([shortcutItem.localizedTitle  isEqualToString: @"item標題1"]){
24 
25     }
26 }

didFinishLaunchingWithOptions方法中執行 spa

    [self setting3DTouchModule];

便可3d

 

第二種效果   觸發機制 參考 微信朋友圈 3dtouch打開圖片 代理

這個因爲涉及到tableview /collectionview中cell的重用機制(若是你只給某個view加入 3dtouch手勢的話能夠無視), 須要對cell作必定的自定義操做code

我這裏以collectionview爲例,在回調cell 的方法裏,將當前controller對象傳入cell中進行 3DTouch事件註冊orm

而正常添加3dtouch只須要下面這一句代碼對象

[self registerForPreviewingWithDelegate:self sourceView:cell.contentView];  //正常添加3DTouch事件

 

添加3DTouch的controller 要遵循UIViewControllerPreviewingDelegate協議blog

而後實現 兩個代理方法

- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit{
    // 打開詳情頁觸發
    if (viewControllerToCommit){
        [self showViewController:viewControllerToCommit sender:self];
    }
}
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{
     // 這裏要返回要打開的viewController
     return nil;  
}

具體實現 能夠參考個人demo

 

再者就是下面的按鈕

加入購物車 這個按鈕是須要咱們在 回調的那個controller中實現如下代碼

1 - (NSArray<id<UIPreviewActionItem>> *)previewActionItems{
2     UIPreviewAction *itemA = [UIPreviewAction actionWithTitle:@"加入購物車" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
3         NSLog(@"你要作的操做");
4     }];
5     return @[itemA];
6 }

具體請參考demo

http://files.cnblogs.com/files/n1ckyxu/IOS_3DTouch_Demo.zip

相關文章
相關標籤/搜索