ios 9以后出現了3D touch,借住簡書幾篇博客,寫一些自己的心得。
初步給Appicon添加touch事件,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
在這個appdelegate方法中添加UIApplicationShortcutItem,
[self creatShortcutItem];? //動態創建應用圖標上的3D
- (void)creatShortcutItem {
//創建系統風格的icon
UIApplicationShortcutIcon *searchIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeSearch];
//創建快捷選項
UIApplicationShortcutItem * item1 = [[UIApplicationShortcutItem alloc]initWithType:@"com.lubi.search" localizedTitle:@"搜索" localizedSubtitle:@"西市搜索" icon:searchIcon userInfo:nil];
UIApplicationShortcutItem * item2= [[UIApplicationShortcutItem alloc]initWithType:@"com.lubi.home" localizedTitle:@"首頁" localizedSubtitle:@"首頁課堂" icon:[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeHome] userInfo:nil];
//添加到快捷選項數組
[UIApplication sharedApplication].shortcutItems = @[item1,item2];
}
//ios 9 的 3D touch 執行touch 事件后會執行下面的方法,window的rootviewcontroller 的方法中,注冊通知監聽UIApplicationShortcutItem,之所以要用到通知,是因為當應用第一次下載或者應用退出后,這個方法里是無法獲得rootviewcontroller 的,所以采用通知,在rootviewcontroller里接收通知,完成跳轉事件的處理。
- (void)application:(UIApplication *)application performActionForShortcutItem:(nonnull UIApplicationShortcutItem *)shortcutItem completionHandler:(nonnull void (^)(BOOL))completionHandler
{
if ([[[UIDevice currentDevice]systemVersion]floatValue] >9.0? && self.window.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
NSLog(@"你的手機支持3D Touch!");
//
if ([shortcutItem.type isEqualToString:@"com.lubi.search"])
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"com.lubi.search" object:shortcutItem userInfo:nil];
}
else if ([shortcutItem.type isEqualToString:@"com.lubi.home"])
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"com.lubi.home" object:shortcutItem userInfo:nil];
}
}else
{
NSLog(@"你的手機不支持3D Touch!");
}
}
@implementation LXTabBarController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self addOneChildViewController:[[LXHomeViewController alloc]init] image:[UIImage imageNamed:@"iconfont-shouye"] selectedImage:nil title:@"首頁"];
[self addOneChildViewController:[[LXNewsViewController alloc]init] image:[UIImage imageNamed:@"news"] selectedImage:nil title:@"樂訊"];
[self addOneChildViewController:[[LXMessageViewController alloc]init] image:[UIImage imageNamed:@"message"] selectedImage:nil title:@"news"];
[self addOneChildViewController:[[LXUserViewController alloc]init] image:[UIImage imageNamed:@"iconfont-tabbargroupcommonbtn"] selectedImage:nil title:@"用戶"];
// 添加通知事件
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationShortcutItemResponse:) name:@"com.lubi.search" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationShortcutItemResponse:) name:@"com.lubi.home" object:nil];
}
//3D 處理3DTouch 事件
- (void)applicationShortcutItemResponse:(NSNotification *)notification
{
NSLog(@"%@",notification);
UIApplicationShortcutItem *item? =(UIApplicationShortcutItem *) notification.object;
if ([item.type isEqualToString:@"com.lubi.search"]) {
self.selectedIndex = 3;
}else if ([item.type isEqualToString:@"com.lubi.home"])
{
self.selectedIndex = 0;
}
}