3D touch介紹
3D touch 是ios9+塘淑、iphone6s+的新功能揽思,簡單的說3Dtouch就是用力按壓宰闰,通過3Dtouch增加了一組手勢交互方式郊霎。
3D touch主要常見的使用:
Home Screen Quick Actions (主屏快捷行為入口)
peek and pop (預(yù)覽和彈出)
WebView peek and pop API (HTML鏈接預(yù)覽功能)
Force Properties (按壓力度)
使用
- 在 plist.info 文件加入U(xiǎn)IApplicationShortcutItems 數(shù)組在其中定義 靜態(tài)快速選項(xiàng)
- 使用 UIApplicationShortcutItem 類和相關(guān)的API來定義 動(dòng)態(tài)快速選項(xiàng) 。使用新的 shortcutItems 屬性將 動(dòng)態(tài)快速選項(xiàng) 添加到你應(yīng)用共享的 UIApplication 對象中爷绘。
// 創(chuàng)建標(biāo)簽的ICON圖標(biāo)书劝。
UIApplicationShortcutIcon *icon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeAdd];
// 創(chuàng)建一個(gè)標(biāo)簽进倍,并配置相關(guān)屬性。
UIApplicationShortcutItem *item = [[UIApplicationShortcutItem alloc] initWithType:@"two" localizedTitle:@"在么" localizedSubtitle:@"我愛你" icon:icon userInfo:nil];
// 將標(biāo)簽添加進(jìn)Application的shortcutItems中购对。
[UIApplication sharedApplication].shortcutItems = @[item];
// 程序在后臺運(yùn)行猾昆,或者被激活,點(diǎn)擊快捷菜單進(jìn)來的頁面
if ([shortcutItem.localizedTitle isEqualToString:@"在嗎"]) {
NSLog(@"去洗澡");
} else if ([shortcutItem.localizedTitle isEqualToString:@"愛你"]) {
NSLog(@"呵呵");
}
};```
3.給控件加上 3DTouch 手勢骡苞,按壓預(yù)覽
首先控制器該遵循UIViewControllerPreviewingDelegate應(yīng)該判斷該控制器當(dāng)前是否實(shí)現(xiàn)了3dtouch手勢
`if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
NSLog(@"3D Touch 可用");
[self registerForPreviewingWithDelegate:(id)self sourceView:self.view];
} else {
NSLog(@"3D Touch 不可用垂蜗,shit ,換手機(jī)吧");
}`
點(diǎn)擊進(jìn)入預(yù)覽(peek)模式,實(shí)現(xiàn)協(xié)議方法
- (UIViewController *)previewingContext:(id)previewingContext viewControllerForLocation:(CGPoint)location;
繼續(xù)按壓進(jìn)入,實(shí)現(xiàn)該方法,這里可自己添加跳轉(zhuǎn)
- (void)previewingContext:(id)previewingContext commitViewController:(UIViewController *)viewControllerToCommit;
預(yù)覽模式上拉快捷操作菜單烙如,需在被預(yù)覽的控制器中實(shí)現(xiàn)
- (NSArray> *)previewActionItems么抗,返回菜單數(shù)組。
UIPreviewAction *action = [UIPreviewAction actionWithTitle:@"收藏" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"收藏成功");
}];
特殊說明亚铁,如果是模擬器蝇刀,默認(rèn)是不支持 3DTouch 的,但是機(jī)智的外國開發(fā)者研究出了在模擬器可以使用桌面快捷菜單的功能(長按打開)徘溢,具體見https://github.com/DeskConnect/SBShortcutMenuSimulator
另外吞琐,在程序內(nèi)時(shí),也有開發(fā)者開發(fā)了模擬器使用長按功能呼出 peep 預(yù)覽界面的功能(swift)然爆,具體見(https://github.com/marmelroy/PeekPop)