iOS开发中添加3DTouch功能

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 概述 在多数移动应用中任何时候都只能有一个应用程序处于活跃状态,如果其他应用此刻发生了一些用户感兴趣的那么通过通知...
    莫离_焱阅读 6,598评论 1 8
  • 初识iOS APP开发#### 在iOS APP开发中, main函数仍是程序的入口和出口, 但main函数不需要...
    DeanYan阅读 6,364评论 0 3
  • 极光推送: 1.JPush当前版本是1.8.2,其SDK的开发除了正常的功能完善和扩展外也紧随苹果官方的步伐,SD...
    Isspace阅读 6,811评论 10 16
  • application:didFinishLaunchingWithOptions:详解 iOS 程序启动时总会调...
    hhbsdc阅读 4,581评论 0 0
  • 于是在某个下午,丁二崩溃了。 时间还是两年前。 《了不起的盖茨比》开篇中说:每当你想要批评任何人的时候,你就记住,...
    流噪阅读 6,200评论 1 21