iOS擴(kuò)展新特性之3DTouch開(kāi)發(fā)

3D Touch是在iPhone6s之后且系統(tǒng)是iOS9以上才能使用的功能,詳情見(jiàn)官方文檔
https://developer.apple.com/library/prerelease/content/documentation/UserExperience/Conceptual/Adopting3DTouchOniPhone/index.html#//apple_ref/doc/uid/TP40016543
3D Touch總的來(lái)說(shuō)分如下兩種
(1)A user can now press your Home screen icon to immediately access functionality provided by your app.
(2)Within your app, a user can now press views to see previews of additional content and gain accelerated access to features.
一種是按壓應(yīng)用icon彈出的快捷菜單,另一種是在應(yīng)用里面,按壓view彈出另一個(gè)視圖,再深按一次可push到另一個(gè)頁(yè)面。

一、Home Screen Quick Actions(按壓應(yīng)用圖標(biāo))
在info.plist文件里添加一項(xiàng)UIApplicationShortcutItems,這是個(gè)數(shù)組,里面添加任意項(xiàng),Item0里面的三項(xiàng)分別是圖片名稱、文本內(nèi)容、類型,在AppDelegate里是根據(jù)這個(gè)類型跳轉(zhuǎn)到指定頁(yè)的。


屏幕快照 2017-01-19 17.49.33.png

在AppDelegate如下方法里實(shí)現(xiàn)跳轉(zhuǎn)
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation


 if (articleType == kTypeAttetion) {
    [self.tabbar setTabBarSelectedWithIndex:kTabbarTypeAttention];
} else {
    [self.tabbar setTabBarSelectedWithIndex:kTabbarTypeHome];
}
HBBaseNavController *nc = (HBBaseNavController *)self.tabbar.selectedViewController;
[nc popToRootViewControllerAnimated:YES];

UIViewController *vc = [[UIViewController alloc] init];
  [nc pushViewController:vc animated:YES];

跳轉(zhuǎn)的時(shí)候要注意的是,先讓TabBarViewController跳轉(zhuǎn)到指定Tab,再讓這個(gè)Tab下的NavigationController 回到根視圖(popToRootViewController),然后再push到對(duì)應(yīng)的ViewController里去。

二、Peek and Pop(彈出一個(gè)視圖和push到一個(gè)新的頁(yè)面)

1.注冊(cè)事件
如果是在一個(gè)tableview上實(shí)現(xiàn)這個(gè)功能,則需要在cellForRow里面去注冊(cè)這個(gè)事件,

if (IOS9_OR_LATER) {
    if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
        if (!cell.hasRegister3DTouch) {
            [[self viewController] registerForPreviewingWithDelegate:HBTouchDelegate sourceView:cell];
            cell.hasRegister3DTouch = YES;
        }
    }
}

這里面有兩點(diǎn)重要的處理:

*第一點(diǎn)是關(guān)于這個(gè)cell是否被注冊(cè)過(guò),由于tableview的cell是復(fù)用的,所以只注冊(cè)一遍即可,否則在列表上拉加載幾屏后會(huì)出現(xiàn)嚴(yán)重的卡頓。

*第二點(diǎn)是這個(gè)View所屬ViewController的代理指向問(wèn)題,如果有多個(gè)列表,每個(gè)列表里都要注冊(cè)這個(gè)代理,這樣每個(gè)列表里都要實(shí)現(xiàn)這個(gè)代理的方法,同樣的方法要寫(xiě)很多遍,這明顯不符合我們編程簡(jiǎn)潔、高效的宗旨,所以可以把代理的實(shí)現(xiàn)抽象成一個(gè)類,然后注冊(cè)時(shí)將代理指向這個(gè)類(在上面代碼中指的是HBTouchDelegate,實(shí)現(xiàn)的就是UIViewControllerPreviewingDelegate的兩個(gè)代理方法)。

2.實(shí)現(xiàn)代理方法
#pragma mark - UIViewControllerPreviewingDelegate
- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location {

UIView *sourceCell = [previewingContext sourceView];
if (![[sourceCell viewController].presentedViewController isKindOfClass:[HBPeekViewController class]]) {
    HBPeekViewController *peekViewController = [HBPeekViewController new];
 peekViewController.preferredContentSize = CGSizeMake(0, 300);//這個(gè)是彈出視圖的寬高,默認(rèn)是屏幕寬高
    return peekViewController;
 } else {
    return nil;
}
}

- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit NS_AVAILABLE_IOS(9_0) {
[[[previewingContext sourceView] viewController] jumpWithArticleEntity:(HBArticleEntity *)self.touchEntity];
}

3.peekViewController中給彈出的視圖添加一些快捷操作,點(diǎn)贊、收藏、對(duì)上面的文字進(jìn)行復(fù)制等等,只要在previewActionItems方法,創(chuàng)建UIPreviewAction到數(shù)組中,可以在UIPreviewAction的block里實(shí)現(xiàn)對(duì)應(yīng)的操作即可

- (NSArray<id<UIPreviewActionItem>>*)previewActionItems {
// 生成UIPreviewAction
 UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"贊" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
         //do something  
 }];

UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"看全文" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
   //do something
}];

UIPreviewAction *action3 = [UIPreviewAction actionWithTitle:@"收藏" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
   //do something
 }];
 NSArray *group = nil;
 group = @[action1, action2, action3];
 return group;
}

到這里3DTouch主要的幾點(diǎn)就講完啦!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

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