3DTouch桌面快捷方式

3DTouch是iOS9,iPhone6s上新加的功能,之前只是一直有了解,但是由于设备的原因并没有很好的研究,后来购入一台6S所以就迫不及待的尝试了一下。
申明Quick Action有两种方式:静态和动态,静态是在plist文件中申明,动态则是在代码中注册,系统支持两者同时存在。但是系统限制每个app最多显示4个快捷图标,包括静态和动态。
静态的方法加入:
即在plist文件中加入UIApplicationShortcutItems标签,标签类型为NSArray,根据需要可以添加四个不同的子标签。类型为NSDictionary。然后加如所需键值对:
必填项(下面两个键值是必须设置的):UIApplicationShortcutItemType 这个键值设置一个快捷通道类型的字符串
选填项(下面这些键值不是必须设置的):UIApplicationShortcutItemSubtitle 设置标签的副标题UIApplicationShortcutItemIconType 设置标签Icon类型UIApplicationShortcutItemIconFile 设置标签的Icon文件,可以通过这个键值设置自定义文件。
这里写图片描述
动态方法加入:
在AppDelegate中加入以下方法:

- (void)configShortCutItems {
    NSMutableArray *shortcutItems = [NSMutableArray array];

    UIApplicationShortcutItem *item1 = [[UIApplicationShortcutItem alloc]initWithType:@"YPYD.UITouchText.home" localizedTitle:@"首页" localizedSubtitle:nil icon:[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeHome] userInfo:nil];

    UIApplicationShortcutItem *item2 = [[UIApplicationShortcutItem alloc]initWithType:@"YPYD.UITouchText.search" localizedTitle:@"购物车" localizedSubtitle:nil icon:[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeSearch]
                                                                             userInfo:nil];
    [shortcutItems addObject:item2];
    [shortcutItems addObject:item1];
    [[UIApplication sharedApplication] setShortcutItems:shortcutItems];
}

判断手机是否可以用3Dtouch:

- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void(^)(BOOL succeeded))completionHandler
{
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0 &&self.window.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable)
    {
        NSLog(@"你的手机支持3D Touch!");
        YPYDNetCountManager * sharedNetCountManager = [YPYDNetCountManager sharedNetCountManager];
        sharedNetCountManager.applicationShortcutItemTitle = shortcutItem.type;


        //首页
        if([shortcutItem.type isEqualToString:@"YPYD.UITouchText.home"])
        {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"YPYD.UITouchText.home" object:nil userInfo:nil];
        }
        //搜索商品
        if([shortcutItem.type isEqualToString:@"YPYD.UITouchText.search"])
        {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"YPYD.UITouchText.search" object:nil userInfo:nil];
        }
        //购物车
        if([shortcutItem.type isEqualToString:@"YPYD.UITouchText.cart"])
        {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"YPYD.UITouchText.cart" object:nil userInfo:nil];
        }
        //我的U
        if([shortcutItem.type isEqualToString:@"YPYD.UITouchText.myU"])
        {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"YPYD.UITouchText.home" object:nil userInfo:nil];
        }
    }
    else
    {
        NSLog(@"你的手机暂不支持3D Touch!");
    }
}

这个时候通过屏幕按压已经可以出现快捷按钮了,最后就是给相应的按钮添加功能。我这里只是做了简单的跳转功能,所以在tabBarController中加入以下方法实现界面的跳转。
先添加监听方法,用来监听事件的点击事件:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationShortcutItemResponse) name:@"YPYD.UITouchText.home" object:nil];

监听的方法:

- (void)applicationShortcutItemResponse
{
    YPYDNetCountManager * sharedNetCountManager = [YPYDNetCountManager sharedNetCountManager];

    //首页
    if([ sharedNetCountManager.applicationShortcutItemTitle isEqualToString:@"YPYD.UITouchText.home"])
    {
        self.tabBarController.selectedIndex = 0;
        [[NSNotificationCenter defaultCenter] postNotificationName:@"UITouchText.home" object:nil userInfo:nil];
    }
    //搜索商品
    if([ sharedNetCountManager.applicationShortcutItemTitle isEqualToString:@"YPYD.UITouchText.search"])
    {
        self.self.tabBarController.selectedIndex = 2;

    }
    //购物车
    if([ sharedNetCountManager.applicationShortcutItemTitle isEqualToString:@"YPYD.UITouchText.cart"])
    {
        self.self.tabBarController.selectedIndex = 3;
    }
    //我的 U
    if([ sharedNetCountManager.applicationShortcutItemTitle isEqualToString:@"YPYD.UITouchText.myU"])
    {
        self.self.tabBarController.selectedIndex = 4;
    }
}

现在就可以实现点击快捷图标跳转相应控制器了。总得来说接入很方便,欢迎大家 尝试。
效果图:
这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值