3D Touch
是iPhone6s
以上機型而且是iOS9
及以上系統而引入的三維觸控功能git
做用:github
開關設置:app
在3D Touch設備上,能夠在設置
>通用
>輔助功能
>三維觸控
中選擇開啓或者關閉touch功能,以及設置靈敏度動畫
用戶能夠按住屏幕的應用圖標啓動touch提供的一組快速操做的功能,當用戶選擇其中某一項操做時,在appdelegate的應用委託中會收到回調響應的信息atom
目前最多支持應用顯示四個主屏幕的快速操做,在限制的範圍內,從菜單中的最頂部位置開始,系統首先顯示靜態快速操做。當添加了四個靜態的快速操做後,即使添加動態快速操做也不會有任何響應。spa
有些應用中出現五項快捷操做。通過筆者的研究,分享應用的touch Item是上線後,系通通一爲應用提供的功能,應用上線後系統爲每個應用提供了默認的分享touch功能
3d
在應用首次啓動以前,主屏幕僅顯示程序的靜態快捷操做,首次啓動後,動態添加的操做也會顯示。若是用戶應用更新後還沒有啓動更新,則主屏幕的快捷操做會響應以前操做,這個時候UIApplicationShortcutItemUserInfo
就起到了關鍵性的做用,由於該鍵中應定義應用版本的信息以響應不一樣版本的操做代理
實例: code
item旗下支持的鍵:orm
描述啓動項的祕鑰(必填)
item的名稱(必填)
item的輔助註釋(選填)
item中系統提供icon的選項(選填)
指定項目的中資源icon文件(選填)大小爲35x35 point
自定義的鍵值對(選填)
該類系統目前僅提供三個類方法
//使用系統定義的icon建立
+ (instancetype)iconWithType:(UIApplicationShortcutIconType)type;
//使用資源文件中的圖片資源建立
+ (instancetype)iconWithTemplateImageName:(NSString *)templateImageName;
//使用聯繫人建立
+ (instancetype)iconWithContact:(CNContact *)contact;
複製代碼
//僅定義啓動項的祕鑰以及名稱
- (instancetype)initWithType:(NSString *)type localizedTitle:(NSString *)localizedTitle;
- (instancetype)initWithType:(NSString *)type
localizedTitle:(NSString *)localizedTitle
localizedSubtitle:(NSString *)localizedSubtitle
icon:(UIApplicationShortcutIcon *)icon
userInfo:(NSDictionary<NSString *,id<NSSecureCoding>> *)userInfo;
複製代碼
能夠經過UIApplicationShortcutItem
設置可變的touch操做。動態設置的touch是可變的,apple中提供了變換Item的實例。
實例代碼:
//該方法獲取到的items實際上是上次設置的項
NSArray <UIApplicationShortcutItem *> *existingShortcutItems = [[UIApplication sharedApplication] shortcutItems];
UIApplicationShortcutItem *existingShortcutItem = [existingShortcutItems firstObject];
NSMutableArray <UIApplicationShortcutItem *> *updatedShortcutItems = [existingShortcutItems mutableCopy];
UIMutableApplicationShortcutItem *mutableShortcutItem = [existingShortcutItem mutableCopy];
NSInteger index = [existingShortcutItems indexOfObject:existingShortcutItem];
[mutableShortcutItem setLocalizedTitle: @"New Title"];
[updatedShortcutItems replaceObjectAtIndex: index withObject: mutableShortcutItem];
[[UIApplication sharedApplication] setShortcutItems: updatedShortcutItems];
複製代碼
在Appdelegate中實現協議以驅動快捷操做的響應
- (void)application:(UIApplication *)application
performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem
completionHandler:(void (^)(BOOL succeeded))completionHandler;
完成操做後,應使用對應的布爾值完成處理程序
複製代碼
當應用程序未啓動,此時經過快捷touch啓動項目時:
系統會先回調- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
代理
而後再調用- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
的回調。
此種狀況apple建議在didFinishLaunchingWithOptions
中獲取launchOptions
中UIApplicationLaunchOptionsShortcutItemKey
鍵值的相關信息以處理Touch的快捷啓動,此時返回NO
後,系統不會再回調- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
在使用快速預覽的時候,須要判斷Touch的可用性,而且須要注意用在運行程序時關閉Touch功能
實例:
//遵照UITraitEnvironment協議
1.判斷是否可用來決定的是否註冊touch視圖
if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
//註冊touch視圖
}
2.監聽Touch開關變化
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
}
複製代碼
//用戶在Touch向上滑動時顯示的快捷操做
@property(nonatomic, readonly) NSArray<id<UIPreviewActionItem>> *previewActionItems;
//註冊視圖參與Touch預覽功能,指定源視圖響應觸摸
- (id<UIViewControllerPreviewing>)registerForPreviewingWithDelegate:(id<UIViewControllerPreviewingDelegate>)delegate
sourceView:(UIView *)sourceView;
//取消註冊的上下文標識
- (void)unregisterForPreviewingWithContext:(id<UIViewControllerPreviewing>)previewing;
複製代碼
//sourceView的邊界,動畫啓動的定位
@property(nonatomic) CGRect sourceRect;
//Touch手勢
@property(nonatomic, readonly) UIGestureRecognizer *previewingGestureRecognizerForFailureRelationship;
//Delegate
@property(nonatomic, readonly) id<UIViewControllerPreviewingDelegate> delegate;
//源視圖
@property(nonatomic, readonly) UIView *sourceView;
複製代碼
/* 當用戶在預覽視圖控制器中Touch的時候回調
*
* previewingContext 預覽視圖控制器上下文對象
* location 觸摸在源視圖座標系中的位置
* 返回顯示對應的控制器,能夠經過返回nil,禁用預覽
*/
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext
viewControllerForLocation:(CGPoint)location;
//Touch響應點後的操做,通常推出須要顯示的視圖控制器
- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext
commitViewController:(UIViewController *)viewControllerToCommit;
複製代碼
/* 建立一個快捷操做
*
* title 動做的標題
* style 快捷操做的類型
*/
+ (instancetype)actionWithTitle:(NSString *)title
style:(UIPreviewActionStyle)style
handler:(void (^)(UIPreviewAction *action, UIViewController *previewViewController))handler;
//快捷操做調用的快
@property(nonatomic, copy, readonly, nonnull) void (^handler)(id<UIPreviewActionItem> action, UIViewController *previewViewController);
複製代碼
/* 快捷操做組
*
* title 組的標題
* style 組的類型
*/
+ (instancetype)actionGroupWithTitle:(NSString *)title
style:(UIPreviewActionStyle)style
actions:(NSArray<UIPreviewAction *> *)actions;
複製代碼
//快速行動的標題
@property(nonatomic, copy, readonly) NSString *title;
複製代碼
UIViewControllerPreviewingDelegate
協議,並實現相關的代理previewActionItems
屬性便可