iOS 3D Touch以编程方式

时间:2015-09-10 01:58:45

标签: ios iphone

iPhone 6s / iPhone 6s plus中引入了

3D Touch,我想知道我们是否可以访问3D Touch相关API并在我们的应用程序中以编程方式执行。是否有人有想法?

1 个答案:

答案 0 :(得分:6)

我正在分享用于按下应用程序图标的源代码填充列表中的4项

第1步: - 在appDelegate.m

中导入课程
import sys/utsname.h

第2步: -

#pragma MARK for Get Machine Name

-  (NSString *) machineName {
    struct utsname systemInfo;
    uname(&systemInfo);
    return [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    if([[self machineName] isEqualToString:@"iPhone8,2"]|| [[self machineName] isEqualToString:@"iPhone8,1"]) {
        [self addEventsFor3DTouchEvents];
    }
    return YES;
}


#pragma MARK for Adding Action for Three D Touch Eventes

- (void) addEventsFor3DTouchEvents {
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0) {
        UIApplicationShortcutItem *item1 = [[UIApplicationShortcutItem alloc] initWithType:@"dynamic1" localizedTitle:TITLE_NAME_1 localizedSubtitle:@"" icon:[UIApplicationShortcutIcon iconWithTemplateImageName:TITLE_IMAGE_NAME_1] userInfo:nil];

        UIApplicationShortcutItem *item2 = [[UIApplicationShortcutItem alloc] initWithType:@"dynamic1" localizedTitle:TITLE_NAME_2 localizedSubtitle:@"" icon:[UIApplicationShortcutIcon iconWithTemplateImageName:TITLE_IMAGE_NAME_2] userInfo:nil];

        UIApplicationShortcutItem *item3 = [[UIApplicationShortcutItem alloc] initWithType:@"dynamic1" localizedTitle:TITLE_NAME_3 localizedSubtitle:@"" icon:[UIApplicationShortcutIcon iconWithTemplateImageName:TITLE_IMAGE_NAME_3] userInfo:nil];

        UIApplicationShortcutItem *item4 = [[UIApplicationShortcutItem alloc] initWithType:@"dynamic1" localizedTitle:TITLE_NAME_4 localizedSubtitle:@"" icon:[UIApplicationShortcutIcon iconWithTemplateImageName:TITLE_IMAGE_NAME_4] userInfo:nil];

        [[UIApplication sharedApplication] setShortcutItems: @[ item1, item2, item3, item4 ]];
    }
}


#pragma mark - 3DTouch Delegate Methods

- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
    [self moveThrough3DTouch:shortcutItem];
}

#pragma MARK for Handling Action for Three D Touch Events

- (void)moveThrough3DTouch:(UIApplicationShortcutItem *)temp {

    if ([temp.localizedTitle isEqualToString:TITLE_NAME_1])  {

            [self.tabBarController setSelectedIndex:0];
    } else if([temp.localizedTitle isEqualToString:TITLE_NAME_2]) {

            [self.tabBarController setSelectedIndex:1];
    } else if([temp.localizedTitle isEqualToString:TITLE_NAME_3]) {

            [self.tabBarController setSelectedIndex:2];
    } else if([temp.localizedTitle isEqualToString:TITLE_NAME_4]) {

            [self.tabBarController setSelectedIndex:3];
    }
}

我在我的应用程序中使用Tab Bar控制器,如果想要在视图中移动控制:

- (void) moveToControllerScene {
        UIStoryboard *storyboard              = [UIStoryboard storyboardWithName:STORY_BOARD_IDENTIFIER bundle:nil];
        YOUR_CONTROLLER_OBJECT *obj           = [storyboard instantiateViewControllerWithIdentifier:@"YOUR_CONTROLLER_OBJECT"];
        [navController pushViewController:obj animated:YES];
}