3DTouch 的實現有兩種方法,一種是在plist文件中進行配置,另一種為代碼配置。
1.在plist文件中配置(靜態)
UIapplicationShortcutItems:數組中的元素就是我們的那些快捷選項標簽。
UIApplicationShortcutItemTitle:標簽標題(必填)
UIApplicationShortcutItemType:標簽的唯一標識 (必填)
UIApplicationShortcutItemIconType:使用系統圖標的類型,如搜索、定位、home等(可選)
UIApplicationShortcutItemIcon File:使用項目中的圖片作為標簽圖標 (可選)
UIApplicationShortcutItemSubtitle:標簽副標題 (可選)
UIApplicationShortcutItemUserInfo:字典信息,如傳值使用 (可選)

2.在AppDelegate.m實現(動態)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];    ViewController *mainView = [storyboard instantiateViewControllerWithIdentifier:@"mainController"];    UINavigationController *mainNav = [[UINavigationController alloc] initWithRootViewController:mainView];    self.window.rootViewController = mainNav;    [self.window makeKeyAndVisible];        //創建應用圖標上的3D touch快捷選項    [self creatShortcutItem];        UIApplicationShortcutItem *shortcutItem = [launchOptions valueForKey:UIApplicationLaunchOptionsShortcutItemKey];    //如果是從快捷選項標簽啟動app,則根據不同標識執行不同操作,然后返回NO,防止調用- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler    if (shortcutItem) {        //判斷先前我們設置的快捷選項標簽唯一標識,根據不同標識執行不同操作        if([shortcutItem.type isEqualToString:@"com.mycompany.myapp.one"]){            NSArray *arr = @[@"hello 3D Touch"];            UIActivityViewController *vc = [[UIActivityViewController alloc]initWithActivityItems:arr applicationActivities:nil];            [self.window.rootViewController PResentViewController:vc animated:YES completion:^{            }];        } else if ([shortcutItem.type isEqualToString:@"com.mycompany.myapp.search"]) {//進入搜索界面            SearchViewController *childVC = [storyboard instantiateViewControllerWithIdentifier:@"searchController"];            [mainNav pushViewController:childVC animated:NO];        } else if ([shortcutItem.type isEqualToString:@"com.mycompany.myapp.share"]) {//進入分享界面            SharedViewController *childVC = [storyboard instantiateViewControllerWithIdentifier:@"sharedController"];            [mainNav pushViewController:childVC animated:NO];        }        return NO;    }    return YES;}//創建應用圖標上的3D touch快捷選項- (void)creatShortcutItem {    //創建系統風格的icon    UIApplicationShortcutIcon *icon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeShare];    //    //創建自定義圖標的icon//    UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"分享.png"];        //創建快捷選項    UIApplicationShortcutItem * item = [[UIApplicationShortcutItem alloc]initWithType:@"com.mycompany.myapp.share" localizedTitle:@"分享" localizedSubtitle:@"分享副標題" icon:icon userInfo:nil];        //添加到快捷選項數組    [UIApplication sharedApplication].shortcutItems = @[item];}3.點擊快捷選項標簽進入應用的響應 
在AppDelegate.m文件中加如下代碼:
//如果app在后臺,通過快捷選項標簽進入app,則調用該方法,如果app不在后臺已殺死,則處理通過快捷選項標簽進入app的邏輯在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions中- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];    ViewController *mainView = [storyboard instantiateViewControllerWithIdentifier:@"mainController"];    UINavigationController *mainNav = [[UINavigationController alloc] initWithRootViewController:mainView];    self.window.rootViewController = mainNav;    [self.window makeKeyAndVisible];        //判斷先前我們設置的快捷選項標簽唯一標識,根據不同標識執行不同操作    if([shortcutItem.type isEqualToString:@"com.mycompany.myapp.one"]){        NSArray *arr = @[@"hello 3D Touch"];        UIActivityViewController *vc = [[UIActivityViewController alloc]initWithActivityItems:arr applicationActivities:nil];        [self.window.rootViewController presentViewController:vc animated:YES completion:^{        }];    } else if ([shortcutItem.type isEqualToString:@"com.mycompany.myapp.search"]) {//進入搜索界面        SearchViewController *childVC = [storyboard instantiateViewControllerWithIdentifier:@"searchController"];        [mainNav pushViewController:childVC animated:NO];    } else if ([shortcutItem.type isEqualToString:@"com.mycompany.myapp.share"]) {//進入分享界面        SharedViewController *childVC = [storyboard instantiateViewControllerWithIdentifier:@"sharedController"];        [mainNav pushViewController:childVC animated:NO];    }        if (completionHandler) {        completionHandler(YES);    }}4.修改UIApplicationShortcutItem 
//獲取第0個shortcutItem    UIApplicationShortcutItem *shortcutItem0 = [[UIApplication sharedApplication].shortcutItems objectAtIndex:0];    //將shortcutItem0的類型由UIApplicationShortcutItem改為可修改類型UIMutableApplicationShortcutItem    UIMutableApplicationShortcutItem * newShortcutItem0 = [shortcutItem0 mutableCopy];    //修改shortcutItem的標題    [newShortcutItem0 setLocalizedTitle:@"按鈕1"];    //將shortcutItems數組改為可變數組    NSMutableArray *newShortcutItems = [[UIApplication sharedApplication].shortcutItems mutableCopy];    //替換原ShortcutItem    [newShortcutItems replaceObjectAtIndex:0 withObject:newShortcutItem0];    [UIApplication sharedApplication].shortcutItems = newShortcutItems;5.3DTouch壓力值的運用 
直接在控制器頁面加這個方法即可,按壓控制器中的任何視圖都會調用這個方法-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {    NSArray *arrayTouch = [touches allObjects];    UITouch *touch = (UITouch *)[arrayTouch lastObject];    //通過tag確定按壓的是哪個view,注意:如果按壓的是label,將label的userInteractionEnabled屬性設置為YES    if (touch.view.tag == 105) {        NSLog(@"move壓力 = %f",touch.force);        //紅色背景的label顯示壓力值        _lbForce.text = [NSString stringWithFormat:@"壓力%f",touch.force];        //紅色背景的label上移的高度=壓力值*100        _bottom.constant = ((UITouch *)[arrayTouch lastObject]).force * 100;    }}
新聞熱點
疑難解答