iOS新特性實現3DTouch開發

好久沒有寫過文章了,今天就給大家帶來一篇比較簡單的開發教程吧!目的是爲了讓大夥能夠避開那些不必要的坑,快速高效的實現功能。今天呢來和大家講一下iOS9的新特性3DTouch開發(iOS10都已經出了Beta版,這篇教程真是來的有點晚……)

3DTouch的功能就是用戶可以在HomeScreen上通過用力按壓屏幕,使屏幕彈出快捷菜單,就好像是我們在Windows上右鍵鼠標一樣,效果圖如下:
這裏寫圖片描述


實現3DTouch有兩種方式,一種是通過配置info.Plist來實現的靜態方式,另一種是通過代碼控制的動態方式。

靜態方式實現3DTouch

實現info.Plist中的如下配置

<array>
        <dict>
            <key>UIApplicationShortcutItemIconType</key>
            <string>UIApplicationShortcutIconTypeShare</string>
            <key>UIApplicationShortcutItemTitle</key>
            <string>分享</string>
            <key>UIApplicationShortcutItemType</key>
            <string>3dtouch.share</string>
        </dict>
    </array>

解釋一下:
1.UIApplicationShortcutItemIconType 3DTouch圖標的類型,例如分享,下載,播放,搜索等等
它的值可以選如下這些:

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.UIApplicationShortcutItemTitle 3DTouch顯示的標題,這個值不能沒有
3.UIApplicationShortcutItemType 3DTouch的標識符 ,這個值不能沒有,我們通過這個標識符來判斷觸發的是哪個按鈕。
4.除了這幾個必要的值以爲,我們還可以往裏面傳入例如:
UIApplicationShortcutItemSubtitle 設置標籤的副標題
UIApplicationShortcutItemIconFile 設置標籤的Icon文件
UIApplicationShortcutItemUserInfo:字典信息,如傳值使用

info.Plist設置完了以後,我們還要加入捕獲操作的的響應代碼,我們在AppDelegate.m中加入如下代碼:

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

在邏輯中使用shortcutItem.type來判斷是哪個按鈕觸發的消息。

通過info.Plist得到的效果圖如下:

這裏寫圖片描述


2.動態方式實現3DTouch

在AppDelegate.m中加入如下代碼:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    NSLog(@"+++++++didFinishLaunchingWithOptions+++++++");
    //3D Touch按壓程序圖標的快捷項
    //快捷菜單的圖標
    UIApplicationShortcutIcon *icon1=[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeCaptureVideo];
    UIApplicationShortcutIcon *icon2=[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeAdd];
    UIApplicationShortcutIcon *icon3=[UIApplicationShortcutIcon iconWithTemplateImageName:@"icon.png"];

    //快捷菜單
    UIApplicationShortcutItem *item1=[[UIApplicationShortcutItem alloc]initWithType:@"test1"
                                                                     localizedTitle:@"測試1"
                                                                  localizedSubtitle:nil
                                                                               icon:icon1
                                                                           userInfo:nil];
    UIApplicationShortcutItem *item2=[[UIApplicationShortcutItem alloc]initWithType:@"test2"
                                                                     localizedTitle:@"測試2"
                                                                  localizedSubtitle:@"這是測試2"
                                                                               icon:icon2
                                                                           userInfo:nil];
    UIApplicationShortcutItem *item3=[[UIApplicationShortcutItem alloc]initWithType:@"測試3"
                                                                     localizedTitle:@"搜索"
                                                                  localizedSubtitle:nil
                                                                               icon:icon3
                                                                           userInfo:nil];
    //設置app的快捷菜單
    [[UIApplication sharedApplication] setShortcutItems:@[item1,item2,item3]];

    return YES;
}

在上述代碼中,我動態的創建了3個3DTouch的選項,當我們通過標籤進入app時,就會在appdelegate中調用這樣一個回調,我們可以獲取shortcutItem的信息進行相關邏輯操作:

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

    NSString *title = nil;
    if([shortcutItem.type isEqualToString:@"test1"]){
        title=@"測試1";
    }else if([shortcutItem.type isEqualToString:@"test2"]){
        title=@"測試2";
    }else if([shortcutItem.type isEqualToString:@"test3"]){
        title=@"測試3";
    }

    //這裏就彈個框子意思一下
    //由於UIAlertView在iOS 9被廢棄,因此選用UIAlertController
    UIAlertController *alertController=[UIAlertController alertControllerWithTitle:@"提示"
                                                                          message:[NSString stringWithFormat:@"你點擊了“%@”",title]
                                                                   preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action=[UIAlertAction actionWithTitle:@"知道了"
                                                  style:UIAlertActionStyleDefault
                                                handler:^(UIAlertAction  *action) {
                                                    [alertController dismissViewControllerAnimated:YES completion:nil];
                                                }];
    [alertController addAction:action];
    [self.window.rootViewController presentViewController:alertController
                                                 animated:YES
                                               completion:nil];
}

這樣我們的動態創建方式也完成了。


總結

因爲3DTouch的預覽功能不太好演示,所以在此就不在做操作了,大家可以通過頭文件來熟悉一下它的一些特性,另外3DTouch在UIView中也增加了預覽的功能,下篇文章,我們再來講下關於3DTouch的Pop和Peek功能。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章