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è)的。
在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è)代理的方法溅话,同樣的方法要寫很多遍,這明顯不符合我們編程簡(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)就講完啦儒拂!