3D Touch 實現

說明: 最近將之前寫的筆記等內容所有從本地iCloud轉移到SegmentFault上ios

3D Touch

Apple 自iPhone 6s起支持3D Touch 功能。一個很棒的功能。但其須要藉助硬件支持,全部 6s下的手機都不能實現。呼呼 。做爲一名開發者怎麼能被這個難住呢。在實現3D Touch 的學習以前先讓咱們的模擬器支持此功能吧,呼呼app

3D Touch 實現

3D Touch支持3種模式:學習

  1. peek and popui

    在消息列表頁面按壓會話,則會彈出這個郵件或會話的閱覽,若是繼續施加壓力按壓,則會push出具體的界面實現
  2. Home Screen Quick Actions代理

    經過主屏幕的應用Icon,用3D Touch呼出一個菜單,進行快速定位到應用功能模塊相關功能觸發相關功能
  3. Force Propertiescode

peek and pop

此種模式是分爲peek 和 pop 兩步的。具體的實現根據用戶的力度不一樣會分爲如下幾步:orm

  1. 首先代表內容能夠被預覽事件

  2. 展現預覽圖 (peek模式)開發

  3. 預覽視圖出現導航視圖 (pop 模式)文檔

具體實現步驟

  1. 首先須要檢測 3D Touch 的可用性

// 檢測3D Touch可用性
    if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) { 
    [self registerForPreviewingWithDelegate:self sourceView:cell]; 
    }
  1. 控制器遵照UIViewControllerPreviewingDelegate協議並實現代理方法
    刺代理只存在兩個代理方法。須要實現這兩個方法才能實現功能

- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
{
    //將觸摸點的座標轉化爲tableView座標系上的座標點
    CGPoint locationBaseTableView = [self.tableView convertPoint:location fromView:[previewingContext sourceView]];
    //根據觸摸點獲取當前觸摸的cell的indexPath
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
    //根據indexPath配置當前須要彈出的控制器,並返回   
    
    return VC;
}

此處系統會對其他部分作虛化處理,能夠在以上代理中經過添加一下代碼實現對虛化部分的控制

//    調整不被虛化的範圍,按壓的那個cell不被虛化(輕輕按壓時周邊會被虛化,再少用力展現預覽,再加力跳頁至設定界面)
//    CGRect rect = CGRectMake(0, 0, self.view.frame.size.width,40);
//    previewingContext.sourceRect = rect;
- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext
     commitViewController:(UIViewController *)viewControllerToCommit
{
    [self.navigationController pushViewController:viewControllerToCommit animated:YES];
}

以上步驟只是實現了pop和 week 模式,若是須要實現week Action ,那麼須要在被pop控制器中實現一下方法,並返回事件列表

- (NSArray<id<UIPreviewActionItem>> *)previewActionItems{
    
    UIPreviewAction *action = [UIPreviewAction actionWithTitle:@"action" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"action1");
    }];
    
    UIPreviewAction * action2 = [UIPreviewAction actionWithTitle:@"action2" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"action2");
    }];
    
    return @[action, action2];
}

經過touch事件獲取壓力值

//-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//    NSArray *arrayTouch = [touches allObjects];
//    UITouch *touch = (UITouch *)[arrayTouch lastObject];
        touch.force;
//}

Home Screen Quick Actions 實現

此種模式的實現通常有兩種方式添加標籤:

1. 經過plist文件進行添加   
    程序啓動起來就能夠看到,簡單、直接、明瞭
2. 經過代碼進行添加  
    程序必須運行一次才能夠看到,須要經過代碼添加(代碼必須執行一次才能夠)

1. 靜態添加入口標籤

UIApplicationShortcutitems            Array    
    item 0                                Distionary 
    item 1  
    item 2

每個item 能夠包含如下屬性:

  1. UIApplicationShortcutItemType

    • 設置一個標識符字符串,用來標識用戶根據那個標籤進入程序

    • 必有項

  2. UIApplicationShortcutItemTitle

    • 標籤的標題

    • 必有項

  3. UIApplicationShortcutItemSubtitle

    • 副標題

  4. UIApplicationShortcutItemIconType

    • 設置圖標的樣式,使用系統自帶的

  5. UIApplicationShortcutItemIconFile

    • 自定義標籤圖標文件路徑

  6. UIApplicationShortcutItemUserInfo

2. 代碼實現添加標籤

-(void) addShortcutItem {
    //使用系統提供的ShortcutIcon類型,
    UIApplicationShortcutIcon *addOneIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeAdd];
    UIApplicationShortcutItem *addOneItem = [[UIApplicationShortcutItem alloc] initWithType:@"one" localizedTitle:@"第一個" localizedSubtitle:nil icon:addOneIcon userInfo:nil];
    
    //自定義ShortcutIcon
    // 若是設置了自定義的icon,那麼系統自帶的就不生效
    UIApplicationShortcutIcon *myIcon = [UIApplicationShortcutIcon iconWithTemplateImageName:@"myImage"];
    UIApplicationShortcutItem *myItem = [[UIApplicationShortcutItem alloc] initWithType:@"two" localizedTitle:@"第二個" localizedSubtitle:nil icon:myIcon userInfo:nil];
    [UIApplication sharedApplication].shortcutItems = @[addOneItem, myItem];
}

3. 事件處理

#pragma mark 3D Touch
-(void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
{
 //此種方式是經過 type 進行區分, 還能夠經過titile  進行區分,通常的title都是不相同的
    if ([shortcutItem.type isEqualToString:@"UIApplicationShortcutItemType"]) {
        
    }else if([shortcutItem.type isEqualToString:@""]){
    
    }else {
        
    }
}

參考

  1. 跟着官方文檔學習3D Touch

  2. [官方文檔]()

相關文章
相關標籤/搜索