当前位置: 首页 > 工具软件 > 3dTouch > 使用案例 >

3DTouch实现

栾钟展
2023-12-01

3DTouch是6s和6plus的新功能

/**
 *      当程序启动时
 1. 判断launchOptions字典内的UIApplicationLaunchOptionsShortcutItemKey是否为空
 2. 当不为空是, application:didFinishLaunchWithOptions方法返回NO, 否则返回YES
 3. 在application: performActionForShortcutItem:completionHandler方法内处理点击事件
 */
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    ViewController *viewController = [[ViewController alloc] init];
    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:viewController];
    self.window.rootViewController = naVC;


    [self configShortCutItems];
    if (launchOptions[@"UIApplicationLaunchOptionsShortcutItemKey"] == nil) {
        return YES;
    } else {
        return NO;
    }


    return YES;
}

//  创建shortcutItems
- (void)configShortCutItems {
    NSMutableArray *shortcutItems = [NSMutableArray array];
    UIApplicationShortcutIcon *videoIcon = [UIApplicationShortcutIcon iconWithTemplateImageName:@"1"];
    UIApplicationShortcutItem *videoItem = [[UIApplicationShortcutItem alloc] initWithType:@"1" localizedTitle:@"video" localizedSubtitle:@"son" icon:videoIcon userInfo:@{@"minngzi": @"ligen"}];


    UIApplicationShortcutIcon *blueToothIcon = [UIApplicationShortcutIcon iconWithTemplateImageName:@"2"];
    UIApplicationShortcutItem *blueToothItem = [[UIApplicationShortcutItem alloc] initWithType:@"2" localizedTitle:@"blueTooth" localizedSubtitle:@"blueSon" icon:blueToothIcon userInfo:nil];

    [shortcutItems addObject:videoItem];
    [shortcutItems addObject:blueToothItem];

    [[UIApplication sharedApplication] setShortcutItems:shortcutItems];
}

//  处理shortcutItem
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
    switch (shortcutItem.type.integerValue) {
        case 1: {
            NSLog(@"video");
        } break;
        case 2: {
            NSLog(@"blueTooth");
            [[NSNotificationCenter defaultCenter] postNotificationName:@"ddd" object:nil userInfo:@{@"type": @"1",
                                                                                                    }];

        }break;

        default:
            break;
    }
}

在ViewController中处理通知:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getNotifications:) name:@"ddd" object:nil];

- (void)getNotifications:(NSNotification *)notice {
    NSString *type = notice.userInfo[@"type"];
    if ([type isEqualToString:@"1"]) {
        FirstViewController *firstVC = [[FirstViewController alloc] init];
        [self.navigationController pushViewController:firstVC animated:YES];
    } else {
        NSLog(@"henghengheng");
    }
}

git链接:https://github.com/blueandmoon/3DTouch.git

 类似资料: