iOS开发中添加3DTouch功能
在AppDelegate入口类的入口方法- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 中实现下面这段代码:
UIApplicationShortcutItem * shortItem1 = [[UIApplicationShortcutItem alloc]initWithType:@"1" localizedTitle:@"item1"];
UIApplicationShortcutItem * shortItem2 = [[UIApplicationShortcutItem alloc]initWithType:@"2" localizedTitle:@"item2"];
NSArray * shortItems = [[NSArray alloc]initWithObjects:shortItem1,shortItem2, nil];
[[UIApplication sharedApplication]setShortcutItems:shortItems];
或者下面这段代码,上面那种创建方式不会显示子标题和icon图片,下面这种实现方式可以实现
UIApplicationShortcutIcon * icon1 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"image1"];//创建icon图标
UIApplicationShortcutItem * shortItem1 = [[UIApplicationShortcutItem alloc]initWithType:@"1" localizedTitle:@"item1" localizedSubtitle:@"subItem1" icon:icon1 userInfo:nil];
UIApplicationShortcutIcon * icon2 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"image1"];
UIApplicationShortcutItem * shortItem2 = [[UIApplicationShortcutItem alloc]initWithType:@"1" localizedTitle:@"item2" localizedSubtitle:@"subItem2" icon:icon2 userInfo:nil];
点击选项操作要在AppDelegate中,实现如下代理方法:
-(void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
{
if ([shortcutItem.type isEqualToString:@"1"]) {
NSLog(@"item1");
}
else if ([shortcutItem.type isEqualToString:@"2"]){
NSLog(@"item2");
}
}
如此简单的3DTouch功能就算完成了,下面展示效果图片:
IMG_0799.PNG IMG_0800.PNG。
网友评论