iOS之实现3D Touch
来源:互联网 发布:mysql添加主键 编辑:程序博客网 时间:2024/06/12 19:42
3D Touch简介
2015年,苹果发布了
iOS9
以及iphone6s/iphone6s Plus
,其中最具有创新的就是新的触控方式3D Touch
,相对于多点触摸在平面二维空间的操作,3D Touch
技术增加了对力度和手指面积的感知,可以通过长按快速预览、查看你想要的短信、图片或者超链接等内容,Peek和Pop
手势的响应时间可迅捷到 10ms和15ms等。
3D Touch三大模块
1. Home Screen Quick Actions
2. Peek、Pop
3. Force Properties
3D Touch实现
3D Touch
实现起来不算难,就是实现需要硬件的支持,只能在6s/6s p等上面可以测试体验,模拟器是不能的,ShortcutItem
主要由Item
类型,主标题,副标题、图标,还可添加一些附加信息,每个App最多添加4个快捷键。
操作 - 用手指用力按压App
的icon
,会出现类似的菜单快捷键(ShortcutItem)
,附上Demo
的效果图(Home Screen Quick Actions)
:
1. Home Screen Quick Actions
生成菜单 - 按压icon
弹出快捷键的实现方法分为静态菜单、动态菜单等2种。
. 静态菜单 - 配置项目的.plist文件
目前Xcode
版本的plist
文件还没对应的key
来获取,则 需要用户自己来手动输入UIApplicationShortcutItems
(NSArray
类型 - 内部都是字典
- 对应 - 每个item
);字典内一些key
的解释:UIApplicationShrtcutItemSubtitle
(副标题)UIApplicationShrtcutItemTitle
( 必填)(可监听该项判断用户是从哪一个标签进入App)UIApplicationShortcutItemType
( 必填)(可监听该项判断用户是从哪一个标签进入App)UIApplicationShrtcutItemIconType
(图标)(系统提供了29种样式的图标)UIApplicationShrtcutItemIconFile
(自定义图片的文件路径)- 直接传入图片的名字即可
注意:若是设置了自定义的图片 则系统的不再生效
. 动态菜单 - 代码实现快捷菜单,动态添加方法需要代码执行一次,因此静态方法比动态方法优先加载
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { /** * 通过代码实现动态菜单 * 一般情况下设置主标题、图标、type等,副标题是不设置的 - 简约原则 * iconWithTemplateImageName 自定义的icon * iconWithType 系统的icon */ //系统ShortcutIcon UIApplicationShortcutIcon *favrite = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeFavorite]; UIApplicationShortcutItem *itemOne = [[UIApplicationShortcutItem alloc] initWithType:@"favrite" localizedTitle:@"时尚之都" localizedSubtitle:nil icon:favrite userInfo:nil]; UIApplicationShortcutIcon *bookMark = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeBookmark]; UIApplicationShortcutItem *itemTwo = [[UIApplicationShortcutItem alloc] initWithType:@"book" localizedTitle:@"知识海洋" localizedSubtitle:nil icon:bookMark userInfo:nil]; //自定义ShortcutIcon UIApplicationShortcutIcon *iconContact = [UIApplicationShortcutIcon iconWithTemplateImageName:@"contact"]; UIApplicationShortcutItem *itemThree = [[UIApplicationShortcutItem alloc] initWithType:@"contact" localizedTitle:@"联系的人" localizedSubtitle:nil icon:iconContact userInfo:nil]; [UIApplication sharedApplication].shortcutItems = @[itemOne,itemTwo,itemThree]; return YES;}
实现点击菜单ShortcutItem
对应的item
跳转到对应的页面
//菜单跳转- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler{ UITabBarController *tabBarVC = (UITabBarController *)self.window.rootViewController; /* * 方式one - localizedTitle if ([shortcutItem.localizedTitle isEqualToString:@"时尚之都"]) { tabBarVC.selectedIndex = 0; }else if ([shortcutItem.localizedTitle isEqualToString:@"知识海洋"]){ //知识海洋 tabBarVC.selectedIndex = 1; }else{ tabBarVC.selectedIndex = 2; //联系的人 } */ //方式two - type if ([shortcutItem.type isEqualToString:@"movie"]) { //时尚之都 tabBarVC.selectedIndex = 0; }else if ([shortcutItem.type isEqualToString:@"book"]){ //知识海洋 tabBarVC.selectedIndex = 1; }else{ tabBarVC.selectedIndex = 2; //联系的人 }}
2. Peek、Pop
经过授权的应用视图控制器可响应用户不同的按压力量,随着按压力量的增加,会有三个交互阶段:
1.暗示预览功能可用,会有一个虚化的效果
2.Peek:
重按一下后出现的预览,展示预览的视图以及快捷菜单
3.Pop:
跳转到预览的视图控制器,是在Peek
后进一步按压后进入预览的视图控制器
首先需遵守代理协议UIViewControllerPreviewingDelegate
@interface TableViewController ()<UIViewControllerPreviewingDelegate>
具体实现
- (NSMutableArray *)dataArray{ if (!_dataArray) { _dataArray = [NSMutableArray new]; int i = 0; while (i < 30) { //模拟数据 [_dataArray addObject:@"http://www.baidu.com"]; i ++; } } return _dataArray;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellID = @"cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID]; } cell.textLabel.text = [NSString stringWithFormat:@"模拟第%ld个知识点",indexPath.row]; /** * UIForceTouchCapability 检测是否支持3D Touch */ if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) { //支持3D Touch //系统所有cell可实现预览(peek) [self registerForPreviewingWithDelegate:self sourceView:cell]; //注册cell } return cell;}- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [tableView deselectRowAtIndexPath:indexPath animated:YES]; WebViewController *webVC = [WebViewController new]; webVC.urlStr = self.dataArray[indexPath.row]; //传数据 webVC.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:webVC animated:YES];}#pragma mark - UIViewControllerPreviewingDelegate- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{ WebViewController *webVC = [WebViewController new]; //转化坐标 location = [self.tableView convertPoint:location fromView:[previewingContext sourceView]]; //根据locaton获取位置 NSIndexPath *path = [self.tableView indexPathForRowAtPoint:location]; //根据位置获取字典数据传传入控制器 webVC.urlStr = self.dataArray[path.row]; return webVC;}- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit{ viewControllerToCommit.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:viewControllerToCommit animated:YES];}
在还没触发Pop
,上划预览视图,则下面可去设置一些选项
//这个方法实现设置一些选项- (NSArray<id<UIPreviewActionItem>> *)previewActionItems{ //赞 UIPreviewAction *itemOne = [UIPreviewAction actionWithTitle:@"赞" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) { [self showHint:@"已赞"]; }]; //举报 UIPreviewAction *itemTwo = [UIPreviewAction actionWithTitle:@"举报" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) { [self showHint:@"举报成功"]; }]; //收藏 UIPreviewAction *itemThree = [UIPreviewAction actionWithTitle:@"收藏" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) { [self showHint:@"收藏成功"]; }]; [self performSelector:@selector(hideHud) withObject:nil afterDelay:1]; //创建一个组包含操作// UIPreviewActionGroup *group = [UIPreviewActionGroup actionGroupWithTitle:@"" style:UIPreviewActionStyleDefault actions:@[@"",@""]]; return @[itemOne,itemTwo,itemThree];}- (void)viewDidLoad { [super viewDidLoad]; //控制器view置为webView - 请求传入的url [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_urlStr]]];}
3. Force Properties
iOS9.0
为我们提供了一个新的交互参数:力度。我们可以检测某一交互的力度值,来做相应的交互处理
// Force of the touch, where 1.0 represents the force of an average touch@property(nonatomic,readonly) CGFloat force NS_AVAILABLE_IOS(9_0);// Maximum possible force with this input mechanism@property(nonatomic,readonly) CGFloat maximumPossibleForce NS_AVAILABLE_IOS(9_0);
//联系的人界面 测试压力大小对其view的背景颜色改变- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ UITouch *touch = touches.anyObject; /** * maximumPossibleForce 最大 6.67 */ NSLog(@"%.2f,%2f",touch.force,touch.maximumPossibleForce); //iOS 9.0之后 CGFloat radio = touch.force / touch.maximumPossibleForce; self.view.backgroundColor = [UIColor colorWithRed:radio green:radio blue:radio alpha:1];}
//时尚之都界面 测试压力改变在其view画圆大小- (void)drawRect:(CGRect)rect { [[UIColor blueColor] set]; [self.path fill];}- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ UITouch *touch = touches.anyObject; UIBezierPath *path = [UIBezierPath bezierPath]; //压力系数为半径 触摸点为圆心 [path addArcWithCenter:[touch locationInView:self] radius:touch.force * 25 startAngle:0 endAngle:2 * M_PI clockwise:YES]; self.path = path; [self setNeedsDisplay];}
整体效果图
基本上涉及到3D Touch的知识点就上面这些吧,也可以看下官网的3D Touch
原文链接:http://www.jianshu.com/p/018d0c226134
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
- iOS之实现3D Touch
- iOS 9 新特性之实现 3D Touch
- iOS新特性之实现3D Touch
- ios 3D Touch功能的实现
- ios 3D Touch功能的实现
- iOS 3D Touch 实现UITableViewCell 预览
- iOS 9之3D Touch
- iOS 9 之 3D touch
- iOS开发之3D Touch
- 【IOS】iphone6s 3d touch之HelloWorld
- iOS开发-------3D Touch之ApplicationShortcutItem
- ios 3D Touch
- iOS 3D touch
- iOS 3D Touch
- iOS 3D Touch
- iOS 9 新特性之实现 3D Touch 就是 So easy -- OC版
- iOS 9 新特性之实现 3D Touch 就是 So easy -- OC版
- iOS 9 新特性之实现 3D Touch 就是 So easy -- OC版
- python web开发 之 flask教程(四)
- 自动轮播加小圆点
- win7下配置nginx+php环境
- 前端性能优化
- JSP中<%%>,<%! %>,<%= %>区别
- iOS之实现3D Touch
- 创建maven web 3.0版本
- 动态改变数据库连接 in Entity Framework 5
- 根据关键字查找
- cmd下进入指定路径文件夹下
- MyBatis学习总结(一)——MyBatis快速入门
- 从SOA到微服务
- C#委托和事件的前世今生
- MICROSOFT VISUAL STUDIO COMMUNITY 2015软件许可条款