iOS--3DTouch研究

前言:

之前没有接触过3DTouch,感觉老高大上了,上个月研究了一下,发现so easy,(对,妈妈再也不用我的学习了)。这个功能是需要在6S以上的设备才有的,用力按压APP图标,会弹出快捷方式,以供用户操作。体现形式主要有2种:
1:用力按压APP图标
2:用力按压APP内的界面,如一个按钮
具体开发中具体使用,在这里呢,简单给大家介绍第一种

一.集成--有2种方法

1.info.plist文件配置

第一步:在info.plist添加属性值
iOS--3DTouch研究_第1张图片
4656F21AD29DA804475D3487E36FDBCE.png

具体参数看不清楚的请看下面:
UIApplicationShortcutItemTitle
UIApplicationShortcutItemSubtitle
UIApplicationShortcutItemIconFile
UIApplicationShortcutItemType

第二步:在APPDelegate中添加代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    UITabBarController *tab = [UITabBarController new];
    NSMutableArray *controllers = [NSMutableArray array]; 
    FirstViewController *firVC = [[FirstViewController alloc] init];
    UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:firVC];
    nav1.title = @"111111";
    SecondViewController *secVC = [[SecondViewController alloc] init];
    UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:secVC];
    nav2.title = @"22222";
    [controllers addObject:nav1];
    [controllers addObject:nav2];
    tab.viewControllers = controllers;
    self.window.rootViewController = tab;
    return YES;
}
//实现代理方法,通过绑定的标签type来实现
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
{
    UITabBarController *tab = (UITabBarController *)self.window.rootViewController;
    UINavigationController *currentNav = [tab selectedViewController];
    
if([shortcutItem.type isEqualToString:@"ONE"]){
        FirstViewController *vc = [[FirstViewController alloc] init];
        vc.title = @"第一个";
        [currentNav pushViewController:vc animated:YES];
    }else if ([shortcutItem.type isEqualToString:@"TWO"]){
        SecondViewController *vc = [[SecondViewController alloc] init];
        vc.title = @"第二个";
        [currentNav pushViewController:vc animated:YES];
    }
}

2.在APPDelegate中添加代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    ViewController *vc = [[ViewController alloc] init];
    vc.title = @"首页";
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    self.window.rootViewController = nav;
    [self setUpTouch:application];
    return YES;
}
- (void)setUpTouch:(UIApplication *)application
{
    /**
     type 该item 唯一标识符
     localizedTitle :标题
     localizedSubtitle:副标题
     icon:icon图标 可以使用系统类型 也可以使用自定义的图片
     userInfo:用户信息字典 自定义参数,完成具体功能需求
     */
    //    UIApplicationShortcutIcon *icon1 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"标签.png"];
    UIApplicationShortcutIcon *cameraIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeLove];
    UIApplicationShortcutItem *cameraItem = [[UIApplicationShortcutItem alloc] initWithType:@"ONE" localizedTitle:@"拍照" localizedSubtitle:@"副标题" icon:cameraIcon userInfo:nil];
    UIApplicationShortcutIcon *shareIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeShare];
    UIApplicationShortcutItem *shareItem = [[UIApplicationShortcutItem alloc] initWithType:@"TWO" localizedTitle:@"分享" localizedSubtitle:@"副标题" icon:shareIcon userInfo:nil];
    /** 将items 添加到app图标 */
    application.shortcutItems = @[cameraItem,shareItem];
}

- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
{
    UINavigationController *nav = (UINavigationController *)self.window.rootViewController;
    
    if([shortcutItem.type isEqualToString:@"ONE"]){
        UIViewController *vc = [[UIViewController alloc] init];
        vc.title = @"第一个";
        vc.view.backgroundColor = [UIColor yellowColor];
        [nav pushViewController:vc animated:YES];
    }else if ([shortcutItem.type isEqualToString:@"TWO"]){
        UIViewController *vc = [[UIViewController alloc] init];
        vc.title = @"第二个";
        vc.view.backgroundColor = [UIColor grayColor];
        [nav pushViewController:vc animated:YES];
    }
}

二.效果

点击第一个图标,跳到第一个界面


iOS--3DTouch研究_第2张图片
D858F6D37F047A369CD832AF43CF0550.jpg

你可能感兴趣的:(iOS--3DTouch研究)