3D Touch 是 iPhone6S 上独有的一个功能,在屏幕下方增加了压力感应,从而可以检测平时手指按压屏幕的力度,然后系统可以识别到压力强度,再给出相应的选项。效果类似于以下:
创建按钮的方式有两种,一种是代码方式,一种是Info.plist
Info.plist:
实现代码如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// 判断系统版本,如果系统版本大于等于9.0,可以设置3D Touch
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0){
// 自定义图标
UIApplicationShortcutIcon *icon1 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"图片名称"];
//定义type,标题,子标题,图片
UIApplicationShortcutItem *shortItem1 = [[UIApplicationShortcutItem alloc] initWithType:@"类型1" localizedTitle:@"标题1" localizedSubtitle:@"子标题1" icon:icon1 userInfo:nil];
//定义type,标题,子标题,图片
UIApplicationShortcutItem *shortItem2 = [[UIApplicationShortcutItem alloc] initWithType:@"类型2" localizedTitle:@"标题2" localizedSubtitle:@"子标题2" icon:[UIApplicationShortcutIcon iconWithType: UIApplicationShortcutIconTypeCompose] userInfo:nil];
//定义type,标题,子标题,图片
UIApplicationShortcutItem *shortItem3 = [[UIApplicationShortcutItem alloc] initWithType:@"类型3" localizedTitle:@"标题3" localizedSubtitle:@"子标题3" icon:[UIApplicationShortcutIcon iconWithType: UIApplicationShortcutIconTypeLocation] userInfo:nil];
// item 数组
NSArray *shortItems = [[NSArray alloc] initWithObjects: shortItem1,shortItem2, shortItem3,nil];
NSArray *existingItems = [UIApplication sharedApplication].shortcutItems;
NSArray *updatedItems = [existingItems arrayByAddingObjectsFromArray:shortItems];
// 设置按钮
[UIApplication sharedApplication].shortcutItems = updatedItems;
}
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
ViewController *vc = [[ViewController alloc]init];
UINavigationController *naviVc = [[UINavigationController alloc]initWithRootViewController:vc];
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = naviVc;
[self.window makeKeyAndVisible];
return YES;
}
当点击标题按钮后就会触发3D Touch代理方法,设置自定义的跳转
#pragma mark-3DTouch添加的代理方法
-(void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler{
//判断设定的唯一标识,当选择
if ([shortcutItem.type isEqualToString:@"类型1"]) {
My3DTouchViewController *vc = [[My3DTouchViewController alloc]init];
self.window.rootViewController = vc;
}
if ([shortcutItem.localizedTitle isEqualToString:@"标题1"]) {
}
if ([shortcutItem.localizedTitle isEqualToString:@"标题1"]) {
}
}