为IOS程序添加3D Touch功能

苹果公司为最新的iphone添加了3D touch功能,作为IOS开发者,我们只需要简单的几步,就能让我们的程序实现这个功能
一、在 -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 中注册 shortcuts
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{
/**  注册shortcuts
  *       参数 “type”    用于促发点击的识别
  *       参数 “title”    标题
  *       参数 “subtitle” 副标题
  *       参数 “icon”    shortcutItem的样式
  *       参数 “userInfo” 用户信息
**/

UIApplicationShortcutIcon * shortcutIcon1 =[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeAdd];
UIApplicationShortcutItem * item1 = [[UIApplicationShortcutItem alloc]initWithType:@"first" localizedTitle:@"添加" localizedSubtitle:@"添加一个好友" icon:shortcutIcon1 userInfo:nil];

UIApplicationShortcutIcon * shortcutIcon2 = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeFavorite];
UIApplicationShortcutItem * item2 = [[UIApplicationShortcutItem alloc]initWithType:@"second" localizedTitle:@"收藏" localizedSubtitle:@"收藏一个故事" icon:shortcutIcon2 userInfo:nil];

application.shortcutItems = @[item1,item2];

return YES;

}

实现效果


为IOS程序添加3D Touch功能_第1张图片
IMG_2927.jpg

为IOS程序添加3D Touch功能_第2张图片
IMG_2928.jpg
二、实现促发方法
-(void)application:(UIApplication *)application  performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler{

if ([shortcutItem.type isEqual:@"first"]) {

NSLog(@"执行添加事件");

}

else if([shortcutItem.type isEqual:@"second"]){

NSLog(@"执行收藏的操作 ");

}

}

你可能感兴趣的:(为IOS程序添加3D Touch功能)