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