iOS开发--3D Touch使用代码实例

iOS系统9.0以上并且6s以及6sp才支持3D Touch功能,实现该功能很简单,两步即可轻松搞定。假设我们3D Touch包括"通讯录"、“广场”、“聊天”三个选项。


第一步初始化选项

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    // 。。。。。
    
    // 3D Touch
    return [self add3DTouchWithLauchOptions:launchOptions];
}

- (BOOL)add3DTouchWithLauchOptions:(NSDictionary *)launchOptions
{
    BOOL shouldPerformAdditionalDelegateHandling = YES;
    
    if ([[self class] iosVersion] < 9.0) {
        return shouldPerformAdditionalDelegateHandling;
    }
    
    UIApplicationShortcutItem *item = launchOptions[UIApplicationLaunchOptionsShortcutItemKey];
    if (item) {
        shouldPerformAdditionalDelegateHandling = NO;
    }
    
    if ([UIApplication sharedApplication].shortcutItems.count == 0) {
        UIMutableApplicationShortcutItem *contactItem = [[UIMutableApplicationShortcutItem alloc] initWithType:@"contact" localizedTitle:@"通讯录" localizedSubtitle:@"" icon:[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeContact] userInfo:nil];
        
        UIMutableApplicationShortcutItem *squareItem = [[UIMutableApplicationShortcutItem alloc] initWithType:@"square" localizedTitle:@"广场" localizedSubtitle:@"" icon:[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeShare] userInfo:nil];
        
        UIMutableApplicationShortcutItem *talkItem = [[UIMutableApplicationShortcutItem alloc] initWithType:@"talk" localizedTitle:@"聊天" localizedSubtitle:@"" icon:[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeMessage] userInfo:nil];
        
        [[UIApplication sharedApplication] setShortcutItems:@[contactItem, squareItem, talkItem]];
    }
    
    return shouldPerformAdditionalDelegateHandling;
}

第二步响应操作

- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
{
    BOOL handledShortCutItem = [self handledShortCutItem:shortcutItem];
    
    completionHandler(handledShortCutItem);
}

- (BOOL)handledShortCutItem:(UIApplicationShortcutItem *)item
{
    BOOL handled = YES;
    
    UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
    NSString *type = item.type;
    if ([type isEqualToString:@"contact"]) {
        
        NSLog(@"contact");
        tabBarController.selectedIndex = 0;
        
    } else if ([type isEqualToString:@"square"]) {
        
        NSLog(@"square");
        tabBarController.selectedIndex = 1;
        
    } else if ([type isEqualToString:@"talk"]) {
        
        NSLog(@"talk");
        tabBarController.selectedIndex = 2;
        
    } else {
        handled = NO;
    }
    
    return handled;
}

官方提供了可选的ICon

typedef NS_ENUM(NSInteger, UIApplicationShortcutIconType) {
    UIApplicationShortcutIconTypeCompose,
    UIApplicationShortcutIconTypePlay,
    UIApplicationShortcutIconTypePause,
    UIApplicationShortcutIconTypeAdd,
    UIApplicationShortcutIconTypeLocation,
    UIApplicationShortcutIconTypeSearch,
    UIApplicationShortcutIconTypeShare,
    UIApplicationShortcutIconTypeProhibit       NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeContact        NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeHome           NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeMarkLocation   NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeFavorite       NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeLove           NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeCloud          NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeInvitation     NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeConfirmation   NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeMail           NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeMessage        NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeDate           NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeTime           NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeCapturePhoto   NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeCaptureVideo   NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeTask           NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeTaskCompleted  NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeAlarm          NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeBookmark       NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeShuffle        NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeAudio          NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeUpdate         NS_ENUM_AVAILABLE_IOS(9_1)
} NS_ENUM_AVAILABLE_IOS(9_0) __TVOS_PROHIBITED;



阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页