蘋(píng)果6s已經(jīng)問(wèn)世很久了,相信大家對(duì)3D Touch功能也不陌生了,個(gè)人非常喜歡那個(gè)重按的手感.之前一直感覺(jué)這個(gè)新功能很神秘,后來(lái)查了一下,發(fā)現(xiàn)真是簡(jiǎn)單,只需要一個(gè)代理方法即可,下面來(lái)和大家分享一下(只需要兩步~~~)
第一步:當(dāng)然是初始化
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
#warning 1 重按手勢(shì)圖標(biāo)初始化
// UIApplicationShortcutIcon為創(chuàng)建顯示的圖片的類(lèi)
// 創(chuàng)建圖片一共有兩種方式 一種使用系統(tǒng)的圖片 一種使用自定義的圖片
// iconWithType:這個(gè)類(lèi)方法為系統(tǒng)方法
// iconWithTemplateImageName:這個(gè)為自己創(chuàng)建的方法
//系統(tǒng)自帶圖標(biāo)
UIApplicationShortcutIcon *icon1 = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeAdd];
//自定義圖標(biāo)
UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"自己的小圖標(biāo)"];
#warning 2 初始化Item
// initWithType:用作以后響應(yīng)的時(shí)候判斷
// localizedTitle:在3DTouch中顯示的文字
// localizedSubtitle:子標(biāo)題
// icon:圖片
// userInfo:傳入的其余信息
UIApplicationShortcutItem *item1 = [[UIApplicationShortcutItem alloc]initWithType:@"item1" localizedTitle:@"加好友" localizedSubtitle:nil icon:icon1 userInfo:nil];
UIApplicationShortcutItem *item2 = [[UIApplicationShortcutItem alloc]initWithType:@"item2" localizedTitle:@"掃一掃" localizedSubtitle:nil icon:icon2 userInfo:nil];
NSArray *array = @[item1,item2];
[UIApplication sharedApplication].shortcutItems = array;
return YES;
}
第二步:3DTouch 觸發(fā)的代理方法
-(void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
{
//這里可以實(shí)現(xiàn)界面跳轉(zhuǎn)等方法
if ([shortcutItem.type isEqualToString:@"item1"]) {
NSLog(@"按點(diǎn)擊了第一個(gè)標(biāo)題");
}
else if ([shortcutItem.type isEqualToString:@"item2"])
{
NSLog(@"按點(diǎn)擊了第二個(gè)標(biāo)題");
}
}