iOS 3D Touch 開發

1 3D Touch 的三大模塊

在我們的app中使用3D Touch功能,主要分一下三個模塊:

1.1 Home Screen Quick Actions

通過主屏幕的應用Icon,我們可以用3D Touch 呼出一個菜單,進行快速定位應用功能模塊相關功能的開發。

maps_directions_home_2x.png

1.2 Peek and Pop

這個功能是一套全新的用戶交互機制,在使用 3D Touch時, 會有如下三個交互階段:
(1) 輕按,提示用戶這里有3D Touch 的交互,會使交互空間周圍模糊

preview_available_2_2x.png

(2) 繼續深按,會出現預覽視圖

peek_2x.png

(3) 向上滑動預覽視圖通過視圖上的交互控件進行進一步交互

peek_quick_actions_2x.png
 這個模塊的設計可以在網址鏈接上進行網頁的預覽交互。

1.3 Force Properties

iOS9 為我們提供了新的交互參數:force 和 maximumPossibleForce 。

2 Home Screen Quick Actions 功能

應用最多有4個快捷選項標簽,有兩種方式開發 Home Screen Quick Actions 功能。

2.1 靜態標簽
打開項目的plist文件,添加如下項

靜態標簽.png

選項介紹
UIApplicationShortcutItems : 數組中的元素就是我們的那些快捷選項標簽.
UIApplicationShortcutItemType : 標簽的唯一 標示(必填)
UIApplicationShortcutItemTitle : 標簽的標題(必填)
UIApplicationShortcutItemSubtitle : 標簽的副標題 (選填)
UIApplicationShortcutItemIconType : 標簽的Icon類型 (選填)
UIApplicationShortcutItemIconFile : 標簽的Icon 文件,圖標格式35x35像素單色 (選填)
UIApplicationShortcutItemUserInfo: 字典信息,傳值使用(選填)

2.2 動態標簽

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.window = [[UIWindow alloc] init];
    
    ViewController *mainView = [[ViewController alloc] init];
    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.dimi.diandi86.main"]){
            
            DimiViewController *dimiVC = [[DimiViewController alloc] init];
            [self.window.rootViewController presentViewController:dimiVC animated:YES completion:^{
                
            }];
        } else if ([shortcutItem.type isEqualToString:@"com.dimi.diandi86.home"]) {
            DimiViewController *dimiVC = [[DimiViewController alloc] init];
            [self.window.rootViewController presentViewController:dimiVC animated:YES completion:^{
                
            }];
        } else if ([shortcutItem.type isEqualToString:@"com.dimi.diandi86.wonderful"]) {
            DimiViewController *dimiVC = [[DimiViewController alloc] init];
            [self.window.rootViewController presentViewController:dimiVC animated:YES completion:^{
                
            }];
        }
        return NO;
    }
    
    return YES;
}

- (void)creatShortcutItem
{
    //創建系統風格的icon
    UIApplicationShortcutIcon *icon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeShare];
    
    //    //創建自定義圖標的icon,圖標格式35x35像素單色
    //UIApplicationShortcutIcon *icon = [UIApplicationShortcutIcon iconWithTemplateImageName:@"Wonderful.png"];
    
    //創建快捷選項
    UIApplicationShortcutItem * item = [[UIApplicationShortcutItem alloc] initWithType:@"com.dimi.diandi86.wonderful" localizedTitle:@"精彩" localizedSubtitle:nil icon:icon userInfo:nil];
    
    //添加到快捷選項數組
    [UIApplication sharedApplication].shortcutItems = @[item];
}

效果圖如下:


3D Touch 快捷標簽.png

2.3 點擊快捷選項標簽進入應用的響應

//如果app在后臺,通過快捷選項標簽進入app,則調用該方法,如果app不在后臺已殺死,則處理通過快捷選項標簽進入app的邏輯在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions中
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void(^)(BOOL succeeded))completionHandler
{
    //判斷先前我們設置的快捷選項標簽唯一標識,根據不同標識執行不同操作
    if (shortcutItem) {
        //判斷先前我們設置的快捷選項標簽唯一標識,根據不同標識執行不同操作
        if([shortcutItem.type isEqualToString:@"com.dimi.diandi86.main"]){
            DimiViewController *dimiVC = [[DimiViewController alloc] init];
            [self.window.rootViewController presentViewController:dimiVC animated:YES completion:^{
                
            }];
        } else if ([shortcutItem.type isEqualToString:@"com.dimi.diandi86.home"]) {
            DimiViewController *dimiVC = [[DimiViewController alloc] init];
            [self.window.rootViewController presentViewController:dimiVC animated:YES completion:^{
                
            }];
        } else if ([shortcutItem.type isEqualToString:@"com.dimi.diandi86.wonderful"]) {
            DimiViewController *dimiVC = [[DimiViewController alloc] init];
            [self.window.rootViewController presentViewController:dimiVC animated:YES completion:^{
                
            }];
        }
    }
    
    if (completionHandler) {
        completionHandler(YES);
    }
}

效果圖如下


點擊快捷標簽選項的響應.png

3 Peek and Pop 功能

應用啟動之后界面如下
3D Touch主界面.png

3.1 讓ViewController 遵守UIViewControllerPreviewingDelegate協議

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate, UIViewControllerPreviewingDelegate>

@property (nonatomic, strong) UITableView *mainTableView;

@property (nonatomic, strong) NSMutableArray *dataArray;

@end

3.2 給 cell 注冊 3D Touch 的 Peek and Pop 功能

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cellIdentifier";
    UITableViewCell * cell = nil;
    cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    cell.textLabel.text = [self.dataArray objectAtIndex:indexPath.row];
    
    if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
        NSLog(@"3D Touch  可用!");
        //給cell注冊3DTouch的peek(預覽)和pop功能
        [self registerForPreviewingWithDelegate:self sourceView:cell];
    } else {
        NSLog(@"3D Touch 無效");
    }
    
    return cell;
}

3.3 實現 UIViewControllerPreviewingDelegate 方法

#pragma mark - UIViewControllerPreviewingDelegate method
//peek(預覽模式)
- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
{
    //獲取按壓的cell所在行,[previewingContext sourceView]就是按壓的那個視圖
    NSIndexPath *indexPath = [self.mainTableView indexPathForCell:(UITableViewCell* )[previewingContext sourceView]];
    
    //設定預覽的界面
    DimiViewController *childVC = [[DimiViewController alloc] init];
    childVC.preferredContentSize = CGSizeMake(0.0f,500.0f);
    
    //調整不被虛化的范圍,按壓的那個cell不被虛化(輕輕按壓時周邊會被虛化,再少用力展示預覽,再加力跳頁至設定界面)
    CGRect rect = CGRectMake(0, 0, self.view.frame.size.width,40);
    previewingContext.sourceRect = rect;
    
    //返回預覽界面
    return childVC;
}

//pop(繼續按壓進入)
- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
{
    DimiViewController *childVC = [[DimiViewController alloc] init];
    
    [self.navigationController pushViewController:childVC animated:YES];
}

預覽界面效果圖如下

預覽界面效果圖.png

3.4 預覽界面向上滑動交互實現,在DimiViewController 中實現previewActionItems方法

- (NSArray<id<UIPreviewActionItem>> *)previewActionItems
{
    // setup a list of preview actions
    UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"Aciton1" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"Aciton1");
    }];
    
    UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"Aciton2" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"Aciton2");
    }];
    
    NSArray *actions = @[action1,action2];
    
    return actions;
}

效果圖如下

預覽交互效果圖.png

4 Force Properties 的運用

-(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 == 520) {
        self.touchTipValueLabel.text = [NSString stringWithFormat:@"壓力值:%f",touch.force];
    }
}

#pragma mark - getter and setter
- (UILabel*)touchLabel
{
    if (_touchLabel == nil) {
        _touchLabel = [[UILabel alloc] init];
        _touchLabel.font = [UIFont systemFontOfSize:25];
        _touchLabel.text = @"按下我";
        _touchLabel.userInteractionEnabled = YES;
        _touchLabel.textAlignment = NSTextAlignmentCenter;
        _touchLabel.tag = 520;
    }
    
    return _touchLabel;
}

- (UILabel*)touchTipValueLabel
{
    if (_touchTipValueLabel == nil) {
        _touchTipValueLabel = [[UILabel alloc] init];
        _touchTipValueLabel.font = [UIFont systemFontOfSize:25];
        _touchTipValueLabel.text = @"壓力值:";
        _touchTipValueLabel.textAlignment = NSTextAlignmentCenter;
    }
    
    return _touchTipValueLabel;
}

效果圖如下

壓力值的運用.png

3D Touch 學習就到此結束啦...

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,797評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,179評論 3 414
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,628評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,642評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,444評論 6 405
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 54,948評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,040評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,185評論 0 287
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,717評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,602評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,794評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,316評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,045評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,418評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,671評論 1 281
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,414評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,750評論 2 370

推薦閱讀更多精彩內容