3DTouch那點事

公司之前在新的版本中添加了3DTouch,今天就總結(jié)出來方面大家參考和查閱

一、3D Touch 的Quick Action實現(xiàn)

Quick Action有兩種方式:靜態(tài)和動態(tài),靜態(tài)是在plist文件中申明,動態(tài)則是在代碼中注冊,系統(tǒng)支持兩者同時存在。但是系統(tǒng)限制每個app最多顯示4個快捷圖標(biāo),包括靜態(tài)和動態(tài)。

第一種方式動態(tài)添加入口標(biāo)簽

-(BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

方法中實現(xiàn)添加和分享的入口,代碼如下 :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // 創(chuàng)建標(biāo)簽的ICON圖標(biāo)。
    UIApplicationShortcutIcon *firstItemIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeAdd];
    // 創(chuàng)建一個標(biāo)簽,并配置相關(guān)屬性。
    UIMutableApplicationShortcutItem *firstItem = [[UIMutableApplicationShortcutItem alloc]initWithType:@"First" localizedTitle:@"添加" localizedSubtitle:nil icon:firstItemIcon userInfo:nil];
    UIApplicationShortcutIcon *secondItemIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeShare];
    UIMutableApplicationShortcutItem *secondItem = [[UIMutableApplicationShortcutItem alloc]initWithType:@"Second" localizedTitle:@"分享" localizedSubtitle:nil icon:secondItemIcon userInfo:nil];
    
    // 自定義創(chuàng)建標(biāo)簽的ICON圖標(biāo)。
    UIApplicationShortcutIcon *thirdItemIcon = [UIApplicationShortcutIcon iconWithTemplateImageName:@""];
    UIMutableApplicationShortcutItem *thirdItem = [[UIMutableApplicationShortcutItem alloc]initWithType:@"Third" localizedTitle:@"自定義" localizedSubtitle:nil icon:thirdItemIcon userInfo:nil];
    application.shortcutItems = @[firstItem,secondItem,thirdItem];
    return YES;
}

系統(tǒng)提供的圖標(biāo)樣式

typedef NS_ENUM(NSInteger, UIApplicationShortcutIconType) { UIApplicationShortcutIconTypeCompose, 
UIApplicationShortcutIconTypePlay, 
UIApplicationShortcutIconTypePause, 
UIApplicationShortcutIconTypeAdd, 
UIApplicationShortcutIconTypeLocation, 
UIApplicationShortcutIconTypeSearch, 
UIApplicationShortcutIconTypeShare, 
UIApplicationShortcutIconTypeProhibit NS_ENUM_AVAILABLE_IOS(9_1), 
UIApplicationShortcutIconTypeContact NS_ENUM_AVAILABLE_IOS(9_1), 
UIApplicationShortcutIconTypeHome NS_ENUM_AVAILABLE_IOS(9_1), 
UIApplicationShortcutIconTypeMarkLocation NS_ENUM_AVAILABLE_IOS(9_1), 
UIApplicationShortcutIconTypeFavorite NS_ENUM_AVAILABLE_IOS(9_1), 
UIApplicationShortcutIconTypeLove NS_ENUM_AVAILABLE_IOS(9_1), 
UIApplicationShortcutIconTypeCloud NS_ENUM_AVAILABLE_IOS(9_1), 
UIApplicationShortcutIconTypeInvitation NS_ENUM_AVAILABLE_IOS(9_1), 
UIApplicationShortcutIconTypeConfirmation NS_ENUM_AVAILABLE_IOS(9_1), 
UIApplicationShortcutIconTypeMail NS_ENUM_AVAILABLE_IOS(9_1), 
UIApplicationShortcutIconTypeMessage NS_ENUM_AVAILABLE_IOS(9_1), 
UIApplicationShortcutIconTypeDate NS_ENUM_AVAILABLE_IOS(9_1), 
UIApplicationShortcutIconTypeTime NS_ENUM_AVAILABLE_IOS(9_1), 
UIApplicationShortcutIconTypeCapturePhoto NS_ENUM_AVAILABLE_IOS(9_1), 
UIApplicationShortcutIconTypeCaptureVideo NS_ENUM_AVAILABLE_IOS(9_1), 
UIApplicationShortcutIconTypeTask NS_ENUM_AVAILABLE_IOS(9_1), 
UIApplicationShortcutIconTypeTaskCompleted NS_ENUM_AVAILABLE_IOS(9_1), 
UIApplicationShortcutIconTypeAlarm NS_ENUM_AVAILABLE_IOS(9_1), 
UIApplicationShortcutIconTypeBookmark NS_ENUM_AVAILABLE_IOS(9_1), 
UIApplicationShortcutIconTypeShuffle NS_ENUM_AVAILABLE_IOS(9_1), 
UIApplicationShortcutIconTypeAudio NS_ENUM_AVAILABLE_IOS(9_1), 
UIApplicationShortcutIconTypeUpdate NS_ENUM_AVAILABLE_IOS(9_1)
} 
NS_ENUM_AVAILABLE_IOS(9_0) __TVOS_PROHIBITED;

2.實現(xiàn)這個方法

-(void)application:(UIApplication *)application performActionForShortcutItem:
(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler

在這個方法中處理添加和分享的事件,代碼如下:

-(void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
{ 
   if ([shortcutItem.type isEqual:@"add"])
       { NSLog(@"執(zhí)行添加事件"); }
   else if([shortcutItem.type isEqual:@"share"])
       { NSLog(@"執(zhí)行分享的操作 ");}
}


第二種方式靜態(tài)添加入口標(biāo)簽

靜態(tài)添加入口標(biāo)簽不需要寫代碼,只需要在info.plist文件中添加相關(guān)功能設(shè)置即可。
靜態(tài)的方法加入:
即在plist文件中加入UIApplicationShortcutItems標(biāo)簽,標(biāo)簽類型為NSArray,根據(jù)需要可以添加四個不同的子標(biāo)簽。類型為NSDictionary。然后加如所需鍵值對:
必填項(下面兩個鍵值是必須設(shè)置的):

UIApplicationShortcutItemType 這個鍵值設(shè)置一個快捷通道類型的字符串
UIApplicationShortcutItemTitle 這個鍵值設(shè)置標(biāo)簽的標(biāo)題

選填項(下面這些鍵值不是必須設(shè)置的):

UIApplicationShortcutItemSubtitle 設(shè)置標(biāo)簽的副標(biāo)題
UIApplicationShortcutItemIconType 設(shè)置標(biāo)簽的圖標(biāo)樣式,系統(tǒng)提供了29中樣式的圖標(biāo)
UIApplicationShortcutItemIconFile 設(shè)置自定義標(biāo)簽圖片文件的路徑
UIApplicationShortcutItemUserInfo 設(shè)置用戶信息,是一個字典類型,可以用來傳值

UIApplicationShortcutItems
 <key>UIApplicationShortcutItems</key>
 <array>
  <dict>
           <key>UIApplicationShortcutItemType</key>
           <string>First</string>
           <key>UIApplicationShortcutItemTitle</key>
           <string>添加</string>
           <key>UIApplicationShortcutItemIconType</key>
           <string>UIApplicationShortcutIconTypeFavorite</string>

   <string>UIApplicationShortcutIconTypeShare</string>
  </dict>
   <dict>
            <key>UIApplicationShortcutItemType</key>
            <string>Second</string>
            <key>UIApplicationShortcutItemTitle</key>
            <string>帥哥</string>
            <key>UIApplicationShortcutItemIconType</key>
            <string>UIApplicationShortcutIconTypeFavorite</string>
   </dict>
 </array>
IMG_0046.PNG

二、3D Touch 的Peek和Pop實現(xiàn)

3D Touch 的Peek和Pop實現(xiàn)

使用步驟:

  1. 給Cell注冊3DTouch
  2. 遵守協(xié)議<UIViewControllerPreviewingDelegate>
  3. 實現(xiàn)協(xié)議方法

第1步:給Cell注冊3DTouch

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"zhan" forIndexPath:indexPath];
    //判斷是否支持3DTouch
    if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
        //注冊Cell支持3DTouch,并設(shè)置代理
        [self registerForPreviewingWithDelegate:self sourceView:cell];
    }
    
    return cell;
    
}

第2步:遵守協(xié)議<UIViewControllerPreviewingDelegate>

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,UIViewControllerPreviewingDelegate>
@property (weak, nonatomic) IBOutlet UITableView *myTableView;

第3步:實現(xiàn)協(xié)議方法

-(UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
{
    //獲取sourceView
    MyTableViewCell *cell = (MyTableViewCell *)[previewingContext sourceView];
    //設(shè)置彈出預(yù)覽的位置(peek是從哪個位置彈出)
    [previewingContext setSourceRect:cell.bounds];
    //設(shè)置彈框的View.
    UIStoryboard *sb=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    
    DetailViewController *detailVC = [sb instantiateViewControllerWithIdentifier:@"DetailViewController"];
    //設(shè)置彈出peek的高度(設(shè)置寬度是沒有效果的)
    detailVC.preferredContentSize = CGSizeMake(0, self.view.bounds.size.height*0.6);
    //取出Cell的模型傳遞給詳情控制器.
    
    return detailVC;
    //設(shè)置標(biāo)題
    //detailVC.title = @"帥";
    //在這里想彈一個帶有導(dǎo)航條的控制器,控制器里面包裝一個導(dǎo)航條.直接返回導(dǎo)航控制器.那么就會peek出一個導(dǎo)航控制器.

    //return [[UINavigationController alloc] initWithRootViewController:detailVC];
}

QQ20160613-1.png

彈框出現(xiàn)后,繼續(xù)重按時調(diào)用

//彈框出現(xiàn)后,繼續(xù)重按時調(diào)用 //viewControllerToCommit:就是上面return的控制器. 
 - (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UINavigationController *)viewControllerToCommit{ 


   //*******如果上面返回的是導(dǎo)航控制器*********
    
    //獲取導(dǎo)航控制器的根控制器.因為當(dāng)前已經(jīng)是一個導(dǎo)航控制器了,不能再繼續(xù)push一個導(dǎo)航控制器,所以要先獲取peek的導(dǎo)航控制器里面的根控制器.
    //然后再拿當(dāng)前的控制器把獲取的控制器push進去.
    
    //DetailViewController *detailVC = (DetailViewController*)viewControllerToCommit.childViewControllers.lastObject;
    
    //*******如果上面返回的是導(dǎo)航控制器*********
    
    DetailViewController *detailVC = (DetailViewController*)viewControllerToCommit;//.childViewControllers.lastObject;
    [self.navigationController pushViewController:detailVC animated:YES];
    
    //使用show和push是一樣的效果
    //[self showViewController:viewControllerToCommit sender:self];

彈窗出現(xiàn)后,向上滑動,會出現(xiàn)類似ActionSheet的控件.
實現(xiàn)該效果必須得要是在previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location return回去那個控制器當(dāng)中實現(xiàn)以下方法,(我們這里peek出來的是DetailViewController,所以必須得要在DetailViewController中實現(xiàn)該方法)

-(NSArray<id<UIPreviewActionItem>> *)previewActionItems
{
    //彈出的第一個按鈕
    UIPreviewAction *action0 = [UIPreviewAction actionWithTitle:@"收藏" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"收藏");
        
    }];
    
    UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"點贊" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"點贊");
    }];
    
    UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"試試" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"試試");
    }];
    UIPreviewAction *action3 = [UIPreviewAction actionWithTitle:@"星星" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"星星");
    }];
    
    //該按鈕可以是一個組,點擊該組時,跳到組里面的按鈕.
    UIPreviewActionGroup *actionGroup = [UIPreviewActionGroup actionGroupWithTitle:@"組" style:UIPreviewActionStyleSelected actions:@[action2, action3]];
    //直接返回數(shù)組.
    return  @[action0,action1,actionGroup];
}
IMG_0050.PNG

三、通過模擬器進行調(diào)試

使用SBShortcutMenuSimulator來配置模擬器,使模擬器支持3D Touch
在終端中按順序輸入以下命令:

1.git clone https://github.com/DeskConnect/SBShortcutMenuSimulator.git

2.cd SBShortcutMenuSimulator

3.make

然后打開剛才寫好的程序 運行一下打開模擬器,再去終端中按順序輸入一下命令:

1.xcrun simctl spawn booted launchctl debug system/com.apple.SpringBoard --environment DYLD_INSERT_LIBRARIES=$PWD/SBShortcutMenuSimulator.dylib

2.xcrun simctl spawn booted launchctl stop com.apple.SpringBoard

3.echo 'com.apple.mobilecal' | nc 127.0.0.1 8000
注意: 'com.apple.mobilecal' ''里邊寫的是自己項目的Bundle identifier. 這行命令就是要讓模擬器顯示出3D Touch,每次想要顯示快速入口只要重復(fù)操作即可

echo 'com.zhanming.text.-DTouch-OC
' | nc 127.0.0.1 8000

具體代碼放在了GitHub上大家可以下載。
如果感覺這篇文章對您有所幫助,順手點個喜歡,謝謝啦

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,158評論 4 61
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,534評論 19 139
  • 一夜風(fēng)吹雨打 葉用她美麗的絲絹 為秋繡好了厚厚的錦被 枝條還沉浸在 風(fēng)雨的夢鄉(xiāng) 輕輕地啍著對秋的戀歌 根無瑕欣賞秋...
    六月天氣閱讀 429評論 33 40
  • 青石電影(ifmovie)原創(chuàng),轉(zhuǎn)載請微信公號聯(lián)系授權(quán)! 前戲 一直以來青石始終認為韓劇是有毒的,看完火遍全球的《...
    青石電影閱讀 531評論 0 0
  • 舉拳立誓獻國防,柴米油鹽愁斷腸。 凌云壯志終不悔,裙裳鞋襪總是傷。 縱然螺釘一小枚,紅心映出半邊天。
    小菜一碟中花生米閱讀 313評論 1 1

友情鏈接更多精彩內(nèi)容