摘要:從6s.6sPlus開始(iOS9),蘋果添加一項3D Touch功能項,帶給用戶不一樣的用戶體驗.但是模擬器暫時是不支持3D Touch 的.蘋果官方是這么說的:
With Xcode 7.0 you must develop on a device that supports 3D Touch. Simulator in Xcode 7.0 does not support 3D Touch.
但是, github 大神提供了一種插件,是你的 Xcode 同樣可以體驗3D Touch.是不是有點兒小激動呀.
大神 github 網址:https://github.com/DeskConnect/SBShortcutMenuSimulator
具體的配置,作者已經些的比較詳細了,但如果你有真機,這些就都不用考慮啦.
一.3D Touch 有三大模塊:
1.Home Screen Quick Actions ----通過用力按壓屏幕應用 icon 從而選擇相應的功能跳轉相應的界面.(也是本文主要介紹的模塊).
2.peek and pop ----進入應用后,按壓相應的 view 從而進行進一步的操作
3.Force Properties ----力度。我們可以檢測某一交互的力度值,來做相應的交互處理。例如,我們可以通過力度來控制快進的快慢,音量增加的快慢等。
二.Home Screen Quick Actions 的實現
這一項有兩種實現方式:靜態. 動態
靜態標簽是我們在項目的配置plist文件中配置的標簽,在用戶安裝程序后就可以使用,并且排序會在動態標簽的前面。
1.我們先來看靜態標簽的配置:
首先,在info.plist文件中添加如下鍵值(復制,粘貼吆):
先添加了一個UIApplicationShortcutItems的數組,這個數組中添加的元素就是對應的靜態標簽,在每個標簽中我們需要添加一些設置的鍵值:
必填項(下面兩個鍵值是必須設置的):
UIApplicationShortcutItemType 這個鍵值設置一個快捷通道類型的字符串
UIApplicationShortcutItemTitle 這個鍵值設置標簽的標題
選填項(下面這些鍵值不是必須設置的):
UIApplicationShortcutItemSubtitle 設置標簽的副標題
UIApplicationShortcutItemIconType 設置標簽Icon類型
UIApplicationShortcutItemIconFile? 設置標簽的Icon文件
UIApplicationShortcutItemUserInfo 設置信息字典(用于傳值)
我們如上截圖設置后,運行程序,用我們前面的方法進行測試,效果如下:
其中UIApplicationShortcutItemIconType 設置標簽Icon類型,蘋果提供一下:
typedef enum UIApplicationShortcutIconType : NSInteger {
UIApplicationShortcutIconTypeCompose,
UIApplicationShortcutIconTypePlay,
UIApplicationShortcutIconTypePause,
UIApplicationShortcutIconTypeAdd,
UIApplicationShortcutIconTypeLocation,
UIApplicationShortcutIconTypeSearch,
UIApplicationShortcutIconTypeShare,
UIApplicationShortcutIconTypeProhibit,
UIApplicationShortcutIconTypeContact,
UIApplicationShortcutIconTypeHome,
UIApplicationShortcutIconTypeMarkLocation,
UIApplicationShortcutIconTypeFavorite,
UIApplicationShortcutIconTypeLove,
UIApplicationShortcutIconTypeCloud,
UIApplicationShortcutIconTypeInvitation,
UIApplicationShortcutIconTypeConfirmation,
UIApplicationShortcutIconTypeMail,
UIApplicationShortcutIconTypeMessage,
UIApplicationShortcutIconTypeDate,
UIApplicationShortcutIconTypeTime,
UIApplicationShortcutIconTypeCapturePhoto,
UIApplicationShortcutIconTypeCaptureVideo,
UIApplicationShortcutIconTypeTask,
UIApplicationShortcutIconTypeTaskCompleted,
UIApplicationShortcutIconTypeAlarm,
UIApplicationShortcutIconTypeBookmark,
UIApplicationShortcutIconTypeShuffle,
UIApplicationShortcutIconTypeAudio,
UIApplicationShortcutIconTypeUpdate
} UIApplicationShortcutIconType;
當然也可以自定制圖片啦.具體方法在下面哦.
重點來啦:那么,具體的跳轉該如何實現的呢.
類似推送,當我們點擊標簽進入應用程序時,也可以進行一些操作,我們可以看到,在applocation中增加了這樣一個方法:
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
當然具體的跳轉方法我以貼圖為例,以供參考
2.接下來再來看動態標簽的配置:
動態標簽是我們在程序中,通過代碼添加的,與之相關的類,主要有三個:
UIApplicationShortcutItem 創建3DTouch標簽的類
UIMutableApplicationShortcutItem 創建可變的3DTouch標簽的類
UIApplicationShortcutIcon 創建標簽中圖片Icon的類
// 以下是全部代碼,且都是在delegate.m文件中實現的
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//使用系統提供的ShortcutIcon類型
UIApplicationShortcutIcon *shareOpportunityIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeShare];
UIApplicationShortcutItem *shareOpportunityItem = [[UIApplicationShortcutItem alloc] initWithType:@"shareOpportunity" localizedTitle:@"分享"***"" localizedSubtitle:nil icon:addOpportunityIcon userInfo:nil];
UIApplicationShortcutIcon *meMarkIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeTaskCompleted];
UIApplicationShortcutItem *meMarkItem = [[UIApplicationShortcutItem alloc] initWithType:@"bookMark" localizedTitle:@"我的優惠券" localizedSubtitle:nil icon:meMarkIcon userInfo:nil];
UIApplicationShortcutIcon *searchGuestIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeSearch];
UIApplicationShortcutItem *searchGuestItem = [[UIApplicationShortcutItem alloc] initWithType:@"searchGuest" localizedTitle:@"搜索" localizedSubtitle:nil icon:searchGuestIcon userInfo:nil];
//自定義ShortcutIcon
// 如果設置了自定義的icon,那么系統自帶的就不生效
UIApplicationShortcutIcon *myGuestIcon = [UIApplicationShortcutIcon iconWithTemplateImageName:@"myGuestImage"];
UIApplicationShortcutItem *myGuestItem = [[UIApplicationShortcutItem alloc] initWithType:@"myGuest" localizedTitle:@"我的客戶" localizedSubtitle:nil icon:myGuestIcon userInfo:nil];
[UIApplication sharedApplication].shortcutItems = @[addOpportunityItem,bookMarkItem,searchGuestItem,myGuestItem];
return YES;
}
// 作用:點擊快速啟動項菜單上的某個快速啟動項跳轉到指定界面
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
{
// 方式一:type
if ([shortcutItem.type isEqualToString:@"addOpportunity"]) {
NSLog(@"點擊了添加機會item");
} else if ([shortcutItem.type isEqualToString:@"bookMark"]) {
NSLog(@"點擊了添加小記item");
} else if ([shortcutItem.type isEqualToString:@"myGuest"]) {
NSLog(@"點擊了我的客戶item");
} else {
NSLog(@"點擊了搜索客戶item");
}
// 方式二:title或者subtitle
if ([shortcutItem.localizedTitle isEqualToString:@"添加機會"]) {
NSLog(@"點擊了添加機會item");
} else if ([shortcutItem.localizedTitle isEqualToString:@"添加小記"]) {
NSLog(@"點擊了添加小記item");
} else if ([shortcutItem.localizedTitle isEqualToString:@"我的客戶"]) {
NSLog(@"點擊了我的客戶item");
} else {
NSLog(@"點擊了搜索客戶item");
}
}
相應的跳轉方法和上面的靜態跳轉方法是一樣的.