在网上查了好多,验证了各种情形。主要讲3d touch在各种情形下的开发方法,开发主屏幕应用icon上的快捷选项标签(Home Screen Quick Actions),静态设置UIApplicationShortcutItem, 动态添加,修改UIApplicationShortcutItem,peek和pop的实现
- 3DTouch开发准备
- 需要支持3DTouch的设备,如iphone6s或者以上、ios9或以上,xcode7或以上,如果没有硬件支持可以使用模拟器github:https://github.com/DeskConnect/SBShortcutMenuSimulator
开始创建3DTouch功能
- UIApplicationShortcutItems:数组中的元素就是快捷选项标签 - UIApplicationShortcutItemTitle:标签标题(必填) - UIApplicationShortcutItemType:标签的唯一标示(必填) - UIApplicationShortcutItemIconType:使用系统图标的类型,如搜索、定位、home - UIApplicationShortcutItemIconFile:使用项目中的图片作为标签图标(可选) - UIApplicationShortcutItemSubtitle:标签副标题(可选) - UIApplicationShortcutItemUserInfo:字典信息,如传值使用(可选)
动态标签(代码如下)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[self createShortcutItem];//创建快捷方式,同时可以在plist里面创建静态的快捷方式
[self changeShortcutItem];//改变快捷方式,只能改变动态的快捷方式(没找也想到怎么改静态的,有朋友知道的求赐教)
UIApplicationShortcutItem *shortcutItem = [launchOptions valueForKey:UIApplicationLaunchOptionsShortcutItemKey];//获取所有快捷方式(静态加动态)
if (shortcutItem) {
//如果应用不在后台直接使用快捷方式启动就需要实行这部分代码
if ([shortcutItem.type isEqualToString:@"one"]) {//根据不同类型实现不同业务逻辑
ECServiceViewControllerOne *serviceViewControllerOne = [[ECServiceViewControllerOne alloc]init];
self.window.rootViewController = serviceViewControllerOne;
}else
{
ECServiceViewControllerTwo *serviceViewControllerTwo = [[ECServiceViewControllerTwo alloc]init];
self.window.rootViewController = serviceViewControllerTwo;
}
return NO;//一定返回no,禁止重复执行业务逻辑
}
return YES;
}
//动态创建应用图标上面快捷方式
-(void)createShortcutItem
{
UIApplicationShortcutIcon *icon1 = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeAdd];//使用系统图片
UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"shortcut_chat_action"];//使用自己的图片
UIApplicationShortcutItem *item = [[UIApplicationShortcutItem alloc]initWithType:@"three" localizedTitle:@"第三个" localizedSubtitle:@"hello" icon:icon2 userInfo:nil];
[UIApplication sharedApplication].shortcutItems = @[item];//直接赋值就可以,静态的并不在这个数组里面,也就是目前就一个元素
}
//改变动态快捷方式的内容
-(void)changeShortcutItem
{
UIApplicationShortcutItem *item1 = [[UIApplication sharedApplication].shortcutItems lastObject];//获取某个动态快捷方式
UIMutableApplicationShortcutItem *newitem = [item1 mutableCopy];//修改为可变item
[newitem setLocalizedTitle:@"吃饭"];//重新设置LocalizedTitle
NSMutableArray *newArray = [[UIApplication sharedApplication].shortcutItems mutableCopy];//获取所有动态快捷方式所在的数组items,并且转换为可变的数组
[newArray replaceObjectAtIndex:[UIApplication sharedApplication].shortcutItems.count-1 withObject:newitem];//替换数组内需要改变的item
[UIApplication sharedApplication].shortcutItems = newArray;//将新的items赋值给系统items
}
//如果应用在后台那么点击快捷方式就会执行这句
-(void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
{
if ([shortcutItem.type isEqualToString:@"one"]) {
ECServiceViewControllerTwo *serviceViewControllerTwo = [[ECServiceViewControllerTwo alloc]init];
ECServiceViewControllerOne *serviceViewControllerOne = [[ECServiceViewControllerOne alloc]init];
self.window.rootViewController = serviceViewControllerTwo;
}else
{
ECServiceViewControllerTwo *serviceViewControllerTwo = [[ECServiceViewControllerTwo alloc]init];
self.window.rootViewController = serviceViewControllerTwo;
}
if (completionHandler) {
completionHandler(YES);//跳转完成
}
}
- peek(展示预览)和pop(跳页至浏览的界面)
- 首先给view注册3d touch的peek和pop功能,这里找个按钮
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
self.title = @"one";
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.titleLabel.text = @"按我";
button.frame = CGRectMake(20, 50, 100, 100);
button.backgroundColor = [UIColor blueColor];
[self.view addSubview:button];
if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {//判断是否可以进行3d touch
[self registerForPreviewingWithDelegate:self sourceView:button];//给按钮注册3d touch
}
// Do any additional setup after loading the view.
}
- 需要继承协议UIViewControllerPreviewingDelegate
- 实现UIViewControllerPreviewingDelegate方法
-(UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
{
ECServiceViewControllerTwo *two = [[ECServiceViewControllerTwo alloc]init];
two.preferredContentSize = CGSizeMake(0.0, 500.0);//设置预览显示大小
previewingContext.sourceRect = CGRectMake(0, 0, self.view.bounds.size.width, 50);//设置不被虚化的范围
return two;//返回预览图
}
-(void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
{
[self showViewController:viewControllerToCommit sender:self];//用力进入预览图
}
- 在预览图文件里面写预览视图快捷选项
-(NSArray<id<UIPreviewActionItem>> *)previewActionItems
{
UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"action1" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"action1");//写点击快捷按钮实现的业务逻辑
}];
UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"action2" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"action1");
}];
UIPreviewAction *action3 = [UIPreviewAction actionWithTitle:@"action3" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"action1");
}];
NSArray *action = @[action1,action2,action3];
return action;
}
- 3DTouch压力值的使用
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NSArray *arrayTouch = [touches allObjects];
UITouch *touch = (UITouch *)[arrayTouch lastObject];
if (touch.view.tag == 105) {
NSLog(@"压力 = %f",touch.force);
}
}
注意快捷标签图片在左右是根据app在手机上面的位置自动调整的