开发环境及调试设备支持:
Xcode7或以上,iOS9或以上,iPhone6s或以上
3DTouch功能主要分为两部分:
1、主屏幕icon上的快捷标签(Home Screen Quick Actions)
主屏幕icon上的快捷标签的实现方式有两种,
1.1在工程文件info.plist里静态设置
静态设置(在info.plist)中添加如下字段:
效果如下:
1.2代码的动态实现。
动态添加需要Application在iOS9之后新增的一个属性:
@property (nullable, nonatomic, copy) NSArray*shortcutItems NS_AVAILABLE_IOS(9_0) __TVOS_PROHIBITED;
然后在项目中添加代码:
下面列举一下UIApplicationShortcutItem每个参数的含义:
2、Peek and Pop
1、注册(在哪个页面上使用该功能就注册在哪个页面上)
[self registerForPreviewingWithDelegate:selfsourceView:cell];
2、继承协议UIViewControllerPreviewingDelegate
3、实现UIViewControllerPreviewingDelegate方法
//peek(预览)- (nullable UIViewController *)previewingContext:(id)previewingContext viewControllerForLocation:(CGPoint)location
{
//获取按压的cell所在行,[previewingContext sourceView]就是按压的那个视图
NSIndexPath *indexPath = [_myTableView indexPathForCell:(UITableViewCell* )[previewingContext sourceView]];
//设定预览的界面
MyPreviewingViewController *childVC = [[MyPreviewingViewController alloc] init];
childVC.preferredContentSize = CGSizeMake(0.0f,500.0f);
childVC.myStr = [NSString stringWithFormat:@"我是%@,用力按一下进来-------",_myArray[indexPath.row]];
//调整不被虚化的范围,按压的那个cell不被虚化(轻轻按压时周边会被虚化,再少用力展示预览,再加力跳页至设定界面)
CGRect rect = CGRectMake(0, 0, self.view.frame.size.width,40);
previewingContext.sourceRect = rect;
//返回预览界面
return childVC;
}
//pop(按用点力进入)- (void)previewingContext:(id)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
// [self.view addSubview: viewControllerToCommit.view];
[self showViewController:viewControllerToCommit sender:self];
}
4、当弹出预览时,上滑预览视图,出现预览视图中快捷选项- (NSArray> *)previewActionItems {
// setup a list of preview actions
UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"删除" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"你点了-删除" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
[alert show];
}];
UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"置顶" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"点-置顶" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
[alert show];
}];
UIPreviewAction *action3 = [UIPreviewAction actionWithTitle:@"嘎哈" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"不嘎哈" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
[alert show];
}];
NSArray *actions = @[action1,action2,action3];
// and return them (return the array of actions instead to see all items ungrouped)
return actions;
}
到此,3D Touch简单的使用就基本上实现了,如果以上有什么理解错误的地方,欢迎大家交流讨论。