標籤:
據說蘋果某個神秘的團隊閉門潛心研發多年的3DTouch終於,應用在iOS9上,且公開了API。
在系統主介面用力按壓 APP 表徵圖,如上會出現自訂菜單
有兩種方法可以實現
一.代碼(這種方法也是可以動態改變 3DTouch菜單的內容與功能):
第一步:自訂一個初始化菜單的方法;
-(void)init3DTouch{ if ([[UIDevice currentDevice] systemVersion].floatValue < 9.0) { return; } UIApplication *app = [UIApplication sharedApplication]; NSMutableArray *its = [[app shortcutItems] mutableCopy]; if (its.count == 0) { NSMutableArray *items = [[NSMutableArray alloc] init]; UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeAdd]; UIApplicationShortcutItem *i2 = [[UIApplicationShortcutItem alloc] initWithType:@"com.xiaomi.i2" localizedTitle:@"發起群聊" localizedSubtitle:@"" icon:icon2 userInfo:nil]; [items addObject:i2]; UIApplicationShortcutIcon *icon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeShare]; UIApplicationShortcutItem *i1 = [[UIApplicationShortcutItem alloc] initWithType:@"com.xiaomi.i1" localizedTitle:@"電話會議" localizedSubtitle:@"" icon:icon userInfo:nil]; [items addObject:i1]; [app setShortcutItems:items]; }}
第二步:實現 UIApplication 協議來響應系統首頁按壓APP出現菜單的,菜單對應點擊事件;
-(void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler{ if ([shortcutItem.type isEqualToString:@"com.xiaomi.i1"]) { // 點擊菜單一 ‘發起群聊’ 執行的內容 } else if ([shortcutItem.type isEqualToString:@"com.xiaomi.i2"]){ // 點擊菜單二 ‘電話會議’ 執行的內容 }}// Called when the user activates your application by selecting a shortcut on the home screen,// except when -application:willFinishLaunchingWithOptions: or -application:didFinishLaunchingWithOptions returns NO.
第三步:判斷 launchOptions != nil 則返回NO;順便擷取啟用APP的對象,手動執行所點擊的菜單事件
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [self init3DTouch]; // 當APP 不再後台運行時 通過3DTouch 啟用APP,launchOptions不為空白手動調用 performActionForShortcutItem if ( launchOptions != nil ) { UIApplicationShortcutItem *i = [launchOptions objectForKey: UIApplicationLaunchOptionsShortcutItemKey]; [self application:application performActionForShortcutItem:i completionHandler:^(BOOL succeeded) { NSLog(@"launchOptions no null"); }]; return NO; } return YES;}
二.info.plist
=============================
參考:http://www.jianshu.com/p/74fe6cbc542b
3DTouch-ShortcutItem - iOS9 - xcode7