之前在實習(xí)的時候調(diào)研3d touch,并將其應(yīng)用到項目中,現(xiàn)在整理一下,放在這邊,歡迎大家討論^0^
3D Touch三大模塊
1.Home screen quick actions
在主屏幕按壓應(yīng)用圖標(biāo)快速獲取到進(jìn)入某些應(yīng)用功能的菜單
注:在xcode7以上都可以支持3D touch的開發(fā),但是模擬機不能進(jìn)行調(diào)試,但是通過https://github.com/DeskConnect/SBShortcutMenuSimulator中指示,可以在模擬機中調(diào)試?Home screen quick actions,但是對于以下兩個模塊只能在真機調(diào)試。
2.Peek and Pop
交互過程分為三個步驟
1)輕壓UI元素,周圍界面變模糊,提示用戶這邊3d touch中peek可用
2)深按一會,會彈出該元素的預(yù)覽視圖
3)再用力按住,即可全部進(jìn)入視圖;若視圖上有交互控件,通過向上滑動,可以進(jìn)一步操作
3.Force Properties
通過檢測交互的力度值來做相應(yīng)的交互處理;比如通過力度控制快進(jìn)的快慢。
3D Touch APIs
ios9中提供的API根據(jù)其功能使用分為四類
Home screen quick action API?? 針對主屏幕應(yīng)用圖標(biāo)的按壓
UIKit peek and pop API??????????????對元素按壓進(jìn)行預(yù)覽,向上滑動快捷操作
Web view peek and pop API??????網(wǎng)頁鏈接進(jìn)行預(yù)覽,操作
UITouch force properties????????????檢測力度進(jìn)行交互
這邊主要調(diào)研 第二類UIKit peek and pop API
1.UIViewController類中增加了新方法,可以注冊viewController可適用3d touch,也可撤銷注冊
registerForPreviewingWithDelegate:?sourceView:
unregisterForPreviewingWithContext:(id<UIViewControllerPreviewing> _Nonnull)previewing;
2.Controller類需要實現(xiàn)UIViewControllerPreviewingDelegate protocol
@required
- previewingContext:viewControllerForLocation:??用于響應(yīng)用戶輕按后提供預(yù)覽視圖
@required
- previewingContext:commitViewController: 用戶再用力按后完全進(jìn)入預(yù)覽視圖
3.UIViewController中增加新方法 可以返回在預(yù)覽視圖中增加 快捷操作
-? (NSArrayUIPreviewActionItem>> * _Nonnull)previewActionItems
4.還有一些其他的類和協(xié)議
UIPreviewAction、UIPreviewActionGroup
UIPreviewActionItem protocol、UIViewControllerPreviewing protocol
demo實現(xiàn)
1.檢測3D touch是否可用,注冊viewController使用3D touch
2.實現(xiàn)UIViewControllerPreviewingDelegate protocol 方法
previewingContext.sourceRect 是指 用戶輕按時,需要顯示的部分,周圍視圖都將變模糊