os 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;
}
}
网友评论