概述
iOS10系統登陸中國,在系統中對3D Touch的使用需求更頻繁,因此對iOS9中便引入的3D Touch功能作一些瞭解是頗有必要的html
在平常開發中,咱們常常須要使用3D Touch中的兩個功能ios
-
在主屏幕上對應用圖標使用3DTouch操做app
-
在應用程序內對某一控件使用3DTouch操做iview
ShortcutItem
概述
ShortcutItem功能容許用戶在主屏幕上對應用圖標使用3DTouch操做,若是本次操做有效,則會給出幾個快捷可選項容許用戶進行操做ui
靜態添加
在info.plist中添加UIApplicationShortcutItems關鍵字,以以下方式配置便可spa
其中各個關鍵字釋義以下:.net
UIApplicationShortcutItemType: 快捷可選項的特定字符串(必填)
UIApplicationShortcutItemTitle: 快捷可選項的標題(必填)
UIApplicationShortcutItemSubtitle: 快捷可選項的子標題(可選)
UIApplicationShortcutItemIconType: 快捷可選項的圖標(可選)
UIApplicationShortcutItemIconFile: 快捷可選項的自定義圖標(可選)
UIApplicationShortcutItemUserInfo: 快捷可選項的附加信息(可選)3d
動態添加
UIApplicationShortcutItemcode
每個快捷可選項是一個UIApplicationShortcutItem對象,其指定初始化器(NS_DESIGNATED_INITIALIZER)以下orm
- (instancetype)initWithType:(NSString *)type localizedTitle:(NSString *)localizedTitle localizedSubtitle:(nullable NSString *)localizedSubtitle icon:(nullable UIApplicationShortcutIcon *)icon userInfo:(nullable NSDictionary *)userInfo;
其中各個參數釋義以下:
type: 快捷可選項的特定字符串(必填)
localizedTitle: 快捷可選項的標題(必填)
localizedSubtitle: 快捷可選項的子標題(可選)
icon: 快捷可選項的圖標(可選)
userInfo: 快捷可選項的附加信息(可選)
UIApplicationShortcutIcon
每個快捷可選項圖標爲一個UIApplicationShortcutIcon對象,咱們可使用系統提供的多個圖標,也能夠自定義咱們本身的圖標
// 使用系統提供的圖標
+ (instancetype)iconWithType:(UIApplicationShortcutIconType)type;
// 自定義圖標
+ (instancetype)iconWithTemplateImageName:(NSString *)templateImageName;
系統提供的圖標樣式以下:
注: 自定義圖標須要使用鏤空圖標,同時建議1倍圖標大小爲35*35
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// 以"動態添加四個快捷可選項"爲例 UIApplicationShortcutIcon *searchShortcutIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeSearch]; UIApplicationShortcutItem *searchShortcutItem = [[UIApplicationShortcutItem alloc] initWithType:@ "com.liupeng.search" localizedTitle:@ "Search" localizedSubtitle:@ "Search Subtitle" icon:searchShortcutIcon userInfo:nil]; UIApplicationShortcutIcon *playShortcutIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypePlay]; UIApplicationShortcutItem *playShortcutItem = [[UIApplicationShortcutItem alloc] initWithType:@ "com.liupeng.play" localizedTitle:@ "Play" localizedSubtitle:@ "Play Subtitle" icon:playShortcutIcon userInfo:nil]; UIApplicationShortcutIcon *qrShortcutIcon = [UIApplicationShortcutIcon iconWithTemplateImageName:@ "shortcut_QR" ]; UIApplicationShortcutItem *qrShortcutItem = [[UIApplicationShortcutItem alloc] initWithType:@ "com.liupeng.qr" localizedTitle:@ "QR" localizedSubtitle:@ "QR Subtitle" icon:qrShortcutIcon userInfo:nil]; UIApplicationShortcutIcon *payShortcutIcon = [UIApplicationShortcutIcon iconWithTemplateImageName:@ "shortcut_pay" ]; UIApplicationShortcutItem *payShortcutItem = [[UIApplicationShortcutItem alloc] initWithType:@ "com.liupeng.pay" localizedTitle:@ "Pay" localizedSubtitle:@ "Pay Subtitle" icon:payShortcutIcon userInfo:nil]; [UIApplication sharedApplication].shortcutItems = @[searchShortcutItem, playShortcutItem, qrShortcutItem, payShortcutItem]; |
觸發回調
當用戶經過點擊快捷可選項進入應用程序會回調以下方法,咱們能夠在這裏經過快捷可選項的type來加以區分,以便進行不一樣的操做
1 2 3 4 5 6 7 8 9 10 11 12 |
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler { if ([shortcutItem.type isEqualToString:@ "com.liupeng.search" ]) { // do something ... } else if ([shortcutItem.type isEqualToString:@ "com.liupeng.play" ]) { // do something ... } // ... } |
注意
在動態添加快捷可選項前,須要用判斷是否支持3D Touch功能,以避免在不支持的設備上運行程序致使閃退
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
if ([self respondsToSelector:@selector(traitCollection)]) { if ([self.traitCollection respondsToSelector:@selector(forceTouchCapability)]) { if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) { // 支持3D Touch } else { // 不支持3D Touch } } } |
注: 在支持3D Touch的設備上,用戶能夠在程序運行期間經過設置 -> 通用 -> 輔助功能 -> 3D Touch來關閉3D Touch功能,因此咱們有必要經過重寫-traitCollectionDidChange:方法隨時處理
Peek & Pop
概述
Peek和Pop是應用內的一種全新交互模式,當用戶不斷增長力量在控件上按壓,會依次進入四個階段
使用
以"從ViewController中的UILabel控件經過Peek & Pop進入DetailViewController"爲例,對Peek & Pop的使用進行一下講解
1 2 |
@interface ViewController () <uiviewcontrollerpreviewingdelegate> @end</uiviewcontrollerpreviewingdelegate> |
1 |
[self registerForPreviewingWithDelegate:self sourceView:label]; |
1 2 3 4 5 6 7 8 9 10 11 12 |
- (nullable UIViewController *)previewingContext:(id <uiviewcontrollerpreviewing>)previewingContext viewControllerForLocation:(CGPoint)location { // previewingContext.sourceView: 觸發Peek & Pop操做的視圖 // previewingContext.sourceRect: 設置觸發操做的視圖的不被虛化的區域 DetailViewController *detailVC = [[DetailViewController alloc] init]; // 預覽區域大小(可不設置) detailVC.preferredContentSize = CGSizeMake(0, 300); return detailVC; }</uiviewcontrollerpreviewing> |
1 2 3 4 |
- (void)previewingContext:(id <uiviewcontrollerpreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit { [self.navigationController pushViewController:viewControllerToCommit animated:YES]; }</uiviewcontrollerpreviewing> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
- (NSArray<id<uipreviewactionitem>> *)previewActionItems { UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@ "選項一" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) { }]; UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@ "選項二" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) { }]; UIPreviewAction *action3 = [UIPreviewAction actionWithTitle:@ "選項三" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) { }]; UIPreviewActionGroup *actionGroup = [UIPreviewActionGroup actionGroupWithTitle:@ "選項組" style:UIPreviewActionStyleDefault actions:@[action1, action2]]; return @[action1, action2, action3, actionGroup]; }</id<uipreviewactionitem> |
原文:http://www.cocoachina.com/ios/20161114/18071.html