浅谈3D Touch(2) -- UITouch && Peek && Pop
来源:互联网 发布:重庆长寿行知学校 编辑:程序博客网 时间:2024/06/05 07:40
UITouch
之所以先说UITouch是因为从Peek到Pop这个过程中,相信其内部用到了这个东西,我们来看一下iOS9在这个UITouch中加了哪些东西:
- UIForceTouchCapability
- UIForceTouchCapabilityUnknown //3D Touch检测失败
- UIForceTouchCapabilityUnavailable //3D Touch不可用
- UIForceTouchCapabilityAvailable //3D Touch可用
这3个枚举值就是我们来判断设备是否开启3D Touch功能,可以在UIViewController生命周期的viewWillAppear中我们是这么判断的:
if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable){ xxxxxxxx}
当然在生命周期内,如果用户有意修改了设备的3D Touch功能,我们还有一个地方来重新检测:
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection
刚刚说了Peek和Pop和这个有关,对的,就是这个类中的force
属性以及他的最大值maximumPossibleForce
,我们在UIView上做一个简单的实验
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event{ UITouch *touch = [touches anyObject]; NSLog(@"%f",touch.force); self.backgroundColor = [UIColor colorWithRed:(touch.force / touch.maximumPossibleForce )green:0 blue:1 alpha:1];}
按的越重,这个view的red就越大
为了得到maximumPossibleForce
值,我故意在这个函数中加上了
if (touch.force == touch.maximumPossibleForce){ NSLog(@"%f",touch.force);}
输出发现当3D touch开的时候这个值为20/3,所以得到结论force
是从0~20/3从Peek到Pop也正是根据这个值大于某个指定的值时触发
Peek & Pop
刚刚说了Peek和Pop的触发和UITouch的force
有关,在切入正题之前我们还要将触发这2个事件的另一个东西:
previewActionItems
我们创建一个PeekViewController,在这个利用到了UIPreviewAction 和 UIPreviewActionGroup 2个iOS9新加的类型和他们的初始化方法我们重写
- (NSArray <id <UIPreviewActionItem>> *)previewActionItems
从而来定义previewActionItems,这里没什么难度直接上代码
- (NSArray<id<UIPreviewActionItem>> *)previewActionItems{ // 生成UIPreviewAction UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"Action 1" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) { NSLog(@"Action 1 selected"); }]; UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"Action 2" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) { NSLog(@"Action 2 selected"); }]; UIPreviewAction *action3 = [UIPreviewAction actionWithTitle:@"Action 3" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) { NSLog(@"Action 3 selected"); }]; UIPreviewAction *tap1 = [UIPreviewAction actionWithTitle:@"tap 1" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) { NSLog(@"tap 1 selected"); }]; UIPreviewAction *tap2 = [UIPreviewAction actionWithTitle:@"tap 2" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) { NSLog(@"tap 2 selected"); }]; UIPreviewAction *tap3 = [UIPreviewAction actionWithTitle:@"tap 3" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) { NSLog(@"tap 3 selected"); }]; // 塞到UIPreviewActionGroup中 NSArray *actions = @[action1, action2, action3]; NSArray *taps = @[tap1, tap2, tap3]; UIPreviewActionGroup *group1 = [UIPreviewActionGroup actionGroupWithTitle:@"Action Group" style:UIPreviewActionStyleDefault actions:actions]; UIPreviewActionGroup *group2 = [UIPreviewActionGroup actionGroupWithTitle:@"Action Group" style:UIPreviewActionStyleDefault actions:taps]; NSArray *group = @[group1,group2]; return group;}
我们发现他是一个二维结构,所以能展示的东西还是非常多的。
好了准备工作到此结束,我们切入正题在ViewController中,创建一个label作为触发的view
_label = [[UILabel alloc] initWithFrame:CGRectMake(10, 300, self.view.frame.size.width - 20, 100)];_label.userInteractionEnabled = YES;_label.textAlignment = NSTextAlignmentCenter;_label.backgroundColor = [UIColor yellowColor];_label.font = [UIFont boldSystemFontOfSize:20];_label.text = @"Peek && Pop";[self.view addSubview:_label];
然后检测是否可以使用3DTouch,如果可以就注册:
- (void)check3DTouch{ // 如果开启了3D touch if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) { [self registerForPreviewingWithDelegate:(id)self sourceView:_label]; }}
最后我们来写Peek和Pop的代理方法
- peek:
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{ //防止重复加入 if ([self.presentedViewController isKindOfClass:[PeekViewController class]]) { return nil; } else { PeekViewController *peekViewController = [[PeekViewController alloc] init]; return peekViewController; }}
这个代理在按的过程中会进来多次,但是我们并不需要多个peek对象,所以如果触发的self.presentedViewController和我们想要的peekViewController已经是同一个了,那就return nil,我们按label
在出现peekViewController的时候向上移动我们能看到刚刚创建的previewActionItems的2个group
点开一个就能看到我们没个group的items
点击就会触发我们刚刚在block中定义的事件
- Pop:在Peek的基础上不要向上移动,而是按得更重一些,个人感觉已经到了
maximumPossibleForce
这个时候触发代理:
- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit{ PopViewController *popViewController = [[PopViewController alloc] init]; [self showViewController:popViewController sender:self];}
这时候我们看到了PopViewController
当然我们在PopViewController还需要给他加个dismiss的方法,让他可以回去
好了,官方说的所有3D Touch的功能都介绍好了,这是我的Demo
- 浅谈3D Touch(2) -- UITouch && Peek && Pop
- 3D Touch功能之Peek&Pop
- Objc 精选 - 3D Touch 之 Peek & Pop 操作
- iOS开发-------3D Touch之Peek,Pop,UIPreviewAction
- iOS 3D Touch使用<二、 Peek、Pop使用>
- 3D Touch和peek and pop和force
- Peek和Pop功能开发(3D Touch开发之一)
- 利用 3D touch 的“Peek and Pop”实现视频预览
- iOS9 3D Touch 标签菜单 peek and pop force 压力触控 功能开发高级版教程
- iOS9-3DTouch peek&&pop
- iOS 3D Touch浅谈
- iOS9 3DTouch、ShortcutItem、Peek And Pop
- 非3D Touch的设备上模拟Peek Preview效果
- 非3D Touch的设备上模拟Peek Preview效果
- 浅谈3D Touch(1) -- Home screen quick action
- iOS9 3DTouch、ShortcutItem、Peek And Pop技术一览
- iOS9 3DTouch(ShortcutItem、Peek/Pop)等简单使用
- iOS9 3DTouch、ShortcutItem、Peek And Pop技术一览
- RecyclerView的通用适配器,和滚动时不加载图片的封装
- 无法购买服务产品
- 浅谈3D Touch(1) -- Home screen quick action
- 类选择器还是 ID 选择器?
- 【Android】java.lang.SecurityException: Permission Denial: starting Intent 应用启动失败异常
- 浅谈3D Touch(2) -- UITouch && Peek && Pop
- jsp页面启用EL表达式
- PS: DeepProposal: Hunting Objects by Cascading Deep Convolutional Layers__ICCV2015
- android launcher3将桌面较长的图标名称显示完整解决方案
- LayoutInflater.inflate方法浅析
- git 的cherry-pick应用
- springMVC+Mybatis中的ModelMap与ModelAndView
- Android 高清加载巨图方案 拒绝压缩图片
- from setuptools import setup, find_packages