3D-Touch的使用

同步至CoderHannのGitHub博客

2015年9月10日,蘋果在新品發(fā)布會上宣布了3D-Touch功能。這是一種立體觸控技術(shù),屏幕可感應(yīng)不同的感壓力度觸控,很明顯這種技術(shù)給APP的交互又增添了樣式。Hann作為一名iOS開發(fā)者自然對這種新技術(shù)感到好奇,經(jīng)過一番的資料搜集與學(xué)習(xí),成功的將這種功能添加到正在開發(fā)的APP中。將3DTouch有關(guān)的知識點記錄下來以供自己查閱,也給有需求的開發(fā)人員提供一些參考。

3DTouch的使用

開發(fā)者應(yīng)該關(guān)注哪幾部分?

  • 外部啟動標(biāo)簽(Home Screen Quick Actions)
  • peek、pop以及action的使用

外部啟動標(biāo)簽(Home Screen Quick Actions)

外部啟動標(biāo)簽給快速使用APP的某些特定功能提供了便利,按壓具有3DTouch功能的APP圖標(biāo)可以激活標(biāo)簽。外部標(biāo)簽的添加上分為兩種:靜態(tài)添加標(biāo)簽、動態(tài)添加標(biāo)簽。靜態(tài)標(biāo)簽是直接在項目的info.plist配置,動態(tài)標(biāo)簽需要在AppDelegate啟動入口進(jìn)行添加。下面是兩種方式的添加,以及添加所涉及到的類、類型介紹。

靜態(tài)標(biāo)簽

上圖所示為靜態(tài)標(biāo)簽在info.plist中的配置內(nèi)容,即在plist中添加Array類型的UIApplicationShortcutItems以及字典類型的標(biāo)簽實體,下面對這些key和value進(jìn)行介紹:

// info.plist

UIApplicationShortcutItems[{

UIApplicationShortcutItemIconFile:圖標(biāo)名,(如果使用系統(tǒng)圖標(biāo)key為UIApplicationShortcutItemIconType)

UIApplicationShortcutItemTitle:標(biāo)簽名稱,(必須設(shè)置)

UIApplicationShortcutItemSubtitle:子標(biāo)簽名稱,

UIApplicationShortcutItemType:標(biāo)簽的ID,(必須設(shè)置)

{UIApplicationShortcutItemUserInfo[{key:value}...](附加信息)

}...]

動態(tài)標(biāo)簽

在添加動態(tài)標(biāo)簽之前我們先來了解幾個相關(guān)的類:

UIApplicationShortcutIcon標(biāo)簽圖標(biāo)類

@interface UIApplicationShortcutIcon : NSObject <NSCopying>

// 使用系統(tǒng)icon時用這個API創(chuàng)建UIApplicationShortcutIcon對象.
+ (instancetype)iconWithType:(UIApplicationShortcutIconType)type;

// 使用提供的圖片自定義UIApplicationShortcutIcon對象,templateImageName為圖片名.
+ (instancetype)iconWithTemplateImageName:(NSString *)templateImageName;
@end

UIApplicationShortcutItem標(biāo)簽類

@interface UIApplicationShortcutItem : NSObject <NSCopying, NSMutableCopying>
/**
  * type: 標(biāo)簽ID
  * localizedTitle: 標(biāo)簽名稱
  * localizedSubtitle: 子標(biāo)題名稱
  * icon: 標(biāo)簽圖標(biāo)對象
  * userInfo: 附加信息
  */
- (instancetype)initWithType:(NSString *)type localizedTitle:(NSString *)localizedTitle localizedSubtitle:(nullable NSString *)localizedSubtitle icon:(nullable UIApplicationShortcutIcon *)icon userInfo:(nullable NSDictionary *)userInfo;

// 圖標(biāo)對象
@property (nullable, nonatomic, copy, readonly) UIApplicationShortcutIcon *icon;

// 對應(yīng)靜態(tài)標(biāo)簽的UIApplicationShortcutItemUserInfo.
@property (nullable, nonatomic, copy, readonly) NSDictionary<NSString *, id <NSSecureCoding>> *userInfo;
@end

在了解這兩個類后我們來看看動態(tài)添加標(biāo)簽的代碼:

// AppDelegate.m
// - (BOOL)application:didFinishLaunchingWithOptions:
// 動態(tài)添加快捷啟動
    UIApplicationShortcutIcon *iconThree = [UIApplicationShortcutIcon iconWithTemplateImageName:@"showItemIconThree"];
    UIApplicationShortcutItem *itemThree = [[UIApplicationShortcutItem alloc] initWithType:@"shortcutTypeThree" localizedTitle:@"動態(tài)標(biāo)簽" localizedSubtitle:nil icon:iconThree userInfo:nil];
    [[UIApplication sharedApplication] setShortcutItems:@[itemThree]];

現(xiàn)在你可以使用靜態(tài)、動態(tài)以及混合方式創(chuàng)建快捷啟動標(biāo)簽,注意啟動標(biāo)簽最多可以有4個。下面就是我們創(chuàng)建的三個標(biāo)簽:


標(biāo)簽的點擊事件交互

// AppDelegate.m
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
    
    UINavigationController *nav = (UINavigationController *)self.window.rootViewController;
    BSDetailViewController *detailVC = [[BSDetailViewController alloc] init];
    
    if ([shortcutItem.type isEqualToString:@"shortcutTypeOne"]) {
        detailVC.navTitle = @"靜態(tài)標(biāo)簽一";
        
    } else if ([shortcutItem.type isEqualToString:@"shortcutTypeTwo"]) {
        detailVC.navTitle = @"靜態(tài)標(biāo)簽二";
        
    } else if ([shortcutItem.type isEqualToString:@"shortcutTypeThree"]) {
        detailVC.navTitle = @"動態(tài)標(biāo)簽";
        
    }
    
    [nav pushViewController:detailVC animated:YES];
}

到此外部啟動標(biāo)簽的創(chuàng)建以及處理事件都已經(jīng)介紹完了,如果看文章比較枯燥請配合demo源碼進(jìn)行查看.

peek、pop & action

在設(shè)置了具有peek、pop的頁面跟用戶交互的時候主要有三個階段:①輕按:提示用戶這個有預(yù)覽功能,即被選中的空間會凸顯出來周圍變得模糊。②加大力度:進(jìn)入peek預(yù)覽模式,如果設(shè)置了該預(yù)覽模式下的一些操作可以向上滑動顯示actions。③更大力度:激活pop,該階段一般是跳到指定的控制器。

輕按狀態(tài):


press
press

peek預(yù)覽:


peek
peek

actions:
action
action

pop激活:


pop
pop

要達(dá)到上述效果,在開發(fā)中都需要做什么呢?下面一步一步揭曉!

peek & pop

首先我們將能觸發(fā)peek、pop功能的控制器遵守UIViewControllerPreviewingDelegate協(xié)議

// BSTableViewController.m
@interface BSTableViewController()<UIViewControllerPreviewingDelegate,BSDetailViewControllerDelegate>
@end

然后注冊該控制器作為peek和pop預(yù)覽的代理以及提供預(yù)覽視圖容器

// BSTableViewController.m
// 重要
[self registerForPreviewingWithDelegate:self sourceView:self.view];

實現(xiàn)代理方法:

// BSTableViewController.m
// UIViewControllerPreviewingDelegate
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location {
    self.selectedCell = [self searchCellWithPoint:location];
    previewingContext.sourceRect = self.selectedCell.frame;
    
    NSLog(@"peek");
    BSDetailViewController *detailVC = [[BSDetailViewController alloc] init];
    detailVC.delegate = self;
    detailVC.navTitle = self.selectedCell.textLabel.text;
    return detailVC;
}

- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
    NSLog(@"pop");
    [self tableView:self.tableView didSelectRowAtIndexPath:[self.tableView indexPathForCell:self.selectedCell]];
}

actions

上面的一步操作使我們的主界面BSTableViewController有了輕按、peek以及pop的功能,還有個是action,這個action不屬于主界面而是詳情界面BSDetailViewController的事件,在demo里面,詳情將事件代理了主界面去處理事情。
BSDetailViewController生成事件方法:

// BSDetailViewController.m
- (NSArray<id<UIPreviewActionItem>> *)previewActionItems {
    //
    UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"刪除" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        if ([self.delegate respondsToSelector:@selector(detailViewController:DidSelectedDeleteItem:)]) {
            [self.delegate detailViewController:self DidSelectedDeleteItem:self.navTitle];
        }
    }];
    //
    UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"返回" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        if ([self.delegate respondsToSelector:@selector(detailViewControllerDidSelectedBackItem:)]) {
            [self.delegate detailViewControllerDidSelectedBackItem:self];
        }
    }];
    
    NSArray *actions = @[action1,action2];
    
    return actions;
}

好了,上面將peek、pop以及action也介紹完了,看到這你應(yīng)該對3DTouch的使用有了一定的認(rèn)識,還想更深入的學(xué)習(xí)有關(guān)3Dtouch請查看官方文檔,想看本博客demo源碼的點擊我CoderHann進(jìn)行查看。

最后編輯于
?著作權(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)容

  • 前言 關(guān)于3D touch蘋果官方文檔是這么開始介紹的: 大意如下:iOS9開始,所有新的手機都增加了一個三維的用...
    VV木公子閱讀 2,371評論 3 39
  • 前言 關(guān)于這篇文章 由于iPhone 6S發(fā)布不到一年的時間,很多新特性、新技術(shù)還未普遍,不管是3D Touch的...
    Tangentw閱讀 4,735評論 8 18
  • iOS 9之后提供以下幾個3D Touch API: 1.Home screen quick action 主屏幕...
    DeadRoach閱讀 951評論 1 1
  • 3D Touch介紹 從iPhone 6s開始,產(chǎn)品都添加了一項硬件屬性,叫做3D touch。作為屏幕的一部分,...
    歪筆書生_閱讀 706評論 0 0
  • 我還活著? 葉澤瞇著眼睛,有些懵,他看著天空中的太陽,很溫暖很刺眼,確實……還活著! 抬頭看一眼四周,發(fā)現(xiàn)自己正躺...
    相忘蠶閱讀 288評論 0 1

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