首页 > 系统 > iOS > 正文

iOS 3DTouch 开发

2019-11-06 09:42:40
字体:
来源:转载
供稿:网友

3DTouch 的实现有两种方法,一种是在plist文件中进行配置,另一种为代码配置。

1.在plist文件中配置(静态)

UIapplicationShortcutItems:数组中的元素就是我们的那些快捷选项标签。

UIApplicationShortcutItemTitle:标签标题(必填)

UIApplicationShortcutItemType:标签的唯一标识 (必填)

UIApplicationShortcutItemIconType:使用系统图标的类型,如搜索、定位、home等(可选)

UIApplicationShortcutItemIcon File:使用项目中的图片作为标签图标 (可选)

UIApplicationShortcutItemSubtitle:标签副标题 (可选)

UIApplicationShortcutItemUserInfo:字典信息,如传值使用 (可选)

2.在AppDelegate.m实现(动态)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];    ViewController *mainView = [storyboard instantiateViewControllerWithIdentifier:@"mainController"];    UINavigationController *mainNav = [[UINavigationController alloc] initWithRootViewController:mainView];    self.window.rootViewController = mainNav;    [self.window makeKeyAndVisible];        //创建应用图标上的3D touch快捷选项    [self creatShortcutItem];        UIApplicationShortcutItem *shortcutItem = [launchOptions valueForKey:UIApplicationLaunchOptionsShortcutItemKey];    //如果是从快捷选项标签启动app,则根据不同标识执行不同操作,然后返回NO,防止调用- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler    if (shortcutItem) {        //判断先前我们设置的快捷选项标签唯一标识,根据不同标识执行不同操作        if([shortcutItem.type isEqualToString:@"com.mycompany.myapp.one"]){            NSArray *arr = @[@"hello 3D Touch"];            UIActivityViewController *vc = [[UIActivityViewController alloc]initWithActivityItems:arr applicationActivities:nil];            [self.window.rootViewController PResentViewController:vc animated:YES completion:^{            }];        } else if ([shortcutItem.type isEqualToString:@"com.mycompany.myapp.search"]) {//进入搜索界面            SearchViewController *childVC = [storyboard instantiateViewControllerWithIdentifier:@"searchController"];            [mainNav pushViewController:childVC animated:NO];        } else if ([shortcutItem.type isEqualToString:@"com.mycompany.myapp.share"]) {//进入分享界面            SharedViewController *childVC = [storyboard instantiateViewControllerWithIdentifier:@"sharedController"];            [mainNav pushViewController:childVC animated:NO];        }        return NO;    }    return YES;}//创建应用图标上的3D touch快捷选项- (void)creatShortcutItem {    //创建系统风格的icon    UIApplicationShortcutIcon *icon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeShare];    //    //创建自定义图标的icon//    UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"分享.png"];        //创建快捷选项    UIApplicationShortcutItem * item = [[UIApplicationShortcutItem alloc]initWithType:@"com.mycompany.myapp.share" localizedTitle:@"分享" localizedSubtitle:@"分享副标题" icon:icon userInfo:nil];        //添加到快捷选项数组    [UIApplication sharedApplication].shortcutItems = @[item];}

3.点击快捷选项标签进入应用的响应

在AppDelegate.m文件中加如下代码:

//如果app在后台,通过快捷选项标签进入app,则调用该方法,如果app不在后台已杀死,则处理通过快捷选项标签进入app的逻辑在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions中- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];    ViewController *mainView = [storyboard instantiateViewControllerWithIdentifier:@"mainController"];    UINavigationController *mainNav = [[UINavigationController alloc] initWithRootViewController:mainView];    self.window.rootViewController = mainNav;    [self.window makeKeyAndVisible];        //判断先前我们设置的快捷选项标签唯一标识,根据不同标识执行不同操作    if([shortcutItem.type isEqualToString:@"com.mycompany.myapp.one"]){        NSArray *arr = @[@"hello 3D Touch"];        UIActivityViewController *vc = [[UIActivityViewController alloc]initWithActivityItems:arr applicationActivities:nil];        [self.window.rootViewController presentViewController:vc animated:YES completion:^{        }];    } else if ([shortcutItem.type isEqualToString:@"com.mycompany.myapp.search"]) {//进入搜索界面        SearchViewController *childVC = [storyboard instantiateViewControllerWithIdentifier:@"searchController"];        [mainNav pushViewController:childVC animated:NO];    } else if ([shortcutItem.type isEqualToString:@"com.mycompany.myapp.share"]) {//进入分享界面        SharedViewController *childVC = [storyboard instantiateViewControllerWithIdentifier:@"sharedController"];        [mainNav pushViewController:childVC animated:NO];    }        if (completionHandler) {        completionHandler(YES);    }}

4.修改UIApplicationShortcutItem

//获取第0个shortcutItem    UIApplicationShortcutItem *shortcutItem0 = [[UIApplication sharedApplication].shortcutItems objectAtIndex:0];    //将shortcutItem0的类型由UIApplicationShortcutItem改为可修改类型UIMutableApplicationShortcutItem    UIMutableApplicationShortcutItem * newShortcutItem0 = [shortcutItem0 mutableCopy];    //修改shortcutItem的标题    [newShortcutItem0 setLocalizedTitle:@"按钮1"];    //将shortcutItems数组改为可变数组    NSMutableArray *newShortcutItems = [[UIApplication sharedApplication].shortcutItems mutableCopy];    //替换原ShortcutItem    [newShortcutItems replaceObjectAtIndex:0 withObject:newShortcutItem0];    [UIApplication sharedApplication].shortcutItems = newShortcutItems;

5.3DTouch压力值的运用

直接在控制器页面加这个方法即可,按压控制器中的任何视图都会调用这个方法

-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {    NSArray *arrayTouch = [touches allObjects];    UITouch *touch = (UITouch *)[arrayTouch lastObject];    //通过tag确定按压的是哪个view,注意:如果按压的是label,将label的userInteractionEnabled属性设置为YES    if (touch.view.tag == 105) {        NSLog(@"move压力 = %f",touch.force);        //红色背景的label显示压力值        _lbForce.text = [NSString stringWithFormat:@"压力%f",touch.force];        //红色背景的label上移的高度=压力值*100        _bottom.constant = ((UITouch *)[arrayTouch lastObject]).force * 100;    }}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表