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;



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值