3DTouch
一、主屏按压(Home Screen Quik Actions)
1.静态标签
在info.plist文件中新增项
关键字 | 意义 |
---|---|
UIApplicationShortcutItems | 数组中的元素就是我们的那些快捷选项标签。 |
UIApplicationShortcutItemTitle | 标签标题(必填) |
UIApplicationShortcutItemType | 标签的唯一标识 (必填) |
UIApplicationShortcutItemIconType | 使用系统图标的类型,如搜索、定位、home等(可选) |
UIApplicationShortcutItemIcon File | 使用项目中的图片作为标签图标 (可选) |
UIApplicationShortcutItemSubtitle | 标签副标题 (可选) |
UIApplicationShortcutItemUserInfo | 字典信息,如传值使用 (可选) |
2.动态标签
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//创建应用图标上的3D touch快捷选项
[self creatShortcutItem];
UIApplicationShortcutItem *shortcutItem = [launchOptions valueForKey:UIApplicationLaunchOptionsShortcutItemKey];
//如果是从快捷选项标签启动app,则根据不同标识执行不同操作,然后返回NO,防止调用- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
if (shortcutItem) {
//判断先前我们设置的快捷选项标签唯一标识,根据不同标识执行不同操作
if([shortcutItem.type isEqualToString:@"first"]){
NSArray *arr = @[@"hello 3D Touch"];
UIActivityViewController *vc = [[UIActivityViewController alloc]initWithActivityItems:arr applicationActivities:nil];
[self.window.rootViewController presentViewController:vc animated:YES completion:^{
}];
} else if ([shortcutItem.type isEqualToString:@"share"]) {//进入分享界面
NSLog(@"share");
} else if ([shortcutItem.type isEqualToString:@""]) {//进入界面
}
return NO;
}
return YES;
}
//创建应用图标上的3D touch快捷选项
- (void)creatShortcutItem {
//创建系统风格的icon
UIApplicationShortcutIcon *icon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeShare];
// //创建自定义图标的icon
// UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"分享.png"];
//创建快捷选项
UIApplicationShortcutItem * item = [[UIApplicationShortcutItem alloc] initWithType:@"share" localizedTitle:@"分享" localizedSubtitle:@"分享副标题" icon:icon userInfo:nil];
//添加到快捷选项数组
[UIApplication sharedApplication].shortcutItems = @[item];
}
3.点击快捷选项标签进入应用的响应
在appdelegate中实现方法
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
并在方法中根据shortcutItem.type判断是点击了哪个选项标签
4.peek(预览)和 pop(跳转至预览界面)
- 首先注册peek 和 pop 功能
- (UIView *)touchView {
if (!_touchView) {
_touchView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 80)];
_touchView.center = self.view.center;
_touchView.backgroundColor = [UIColor colorWithRed:0.2 green:0.4 blue:0.5 alpha:1];
// 给view注册3DTouch 的 peek 和pop 功能
if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
NSLog(@"3D Touch 可用!");
//给cell注册3DTouch的peek(预览)和pop功能
[self registerForPreviewingWithDelegate:self sourceView:_touchView];
} else {
NSLog(@"3D Touch 无效");
}
}
return _touchView;
}
- 遵守协议UIViewControllerPreviewingDelegate
- 实现协议方法
#pragma mark - preview
// peek (预览)
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location {
// [previewingContext sourceView]即为所按压的视图
UIView *view = [previewingContext sourceView];
// 设置不被虚化的范围
CGRect rect = view.bounds;
[previewingContext setSourceRect:rect];
// peek (预览界面)
touchViewController *tvc = [[touchViewController alloc] init];
tvc.preferredContentSize = CGSizeMake(0, 100); // 预览图的大小
return tvc;
}
// pop (在预览基础上更用力按压)
- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
// 根视图控制器不是navigationController那么将不回有返回键
[self showViewController:viewControllerToCommit sender:self];
}
5.预览视图上滑显示快捷选项
在预览视图控制器中实现方法
// 预览视图上滑时显示的快捷选项
- (NSArray<id<UIPreviewActionItem>> *)previewActionItems {
UIPreviewAction *action0 = [UIPreviewAction actionWithTitle:@"第一名" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"0001");
}];
UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"第二名" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"0002");
}];
UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"第三名" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"0003");
}];
return @[action0,action1,action2];
}
6.压力值
// 取得压力
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[touches allObjects] lastObject];
NSLog(@"压力:%f",touch.force);
}