3DTouch-ShortcutItem - iOS9 - xcode7

來源:互聯網
上載者:User

標籤:

據說蘋果某個神秘的團隊閉門潛心研發多年的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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.