iOS 3D Touch打开控制器(类似微信聊天样式)

使用过iPhone6s以上机型的同学们肯定使用过3D Touch的功能,随之而来的就是各种3D Touch的相关应用。其中,除了按压APP的logo显示的更多功能之外,还有一个非常常见的功能,想必大家一定使用过,先上图:


效果图.gif

想必大家在使用微信、App Store或者是其他APP的时候一定使用过这个功能,看起来很漂亮,但是实现起来....其实也没多难。

ok,废话就这么多,接下来讲干货:
peek and pop
这个功能是一套全新的用户交互机制,在使用3D Touch时,ViewController中会有如下三个交互阶段:
1.按压cell,周围视图会变模糊。


1.png

2.深按之后,弹出即将显示的控制器。


2.png

3.上滑控制器,下面显示对应提示按钮。


3.png

这三个按压的效果明白之后,之后就简单了,分分钟几个方法搞定。首先先介绍一下UIViewControllerPreviewingDelegate:
当UITableViewCell或者UICollectionViewCell注册UIViewControllerPreviewingDelegate之后才可以实现3DTouch的代理方法,我们实现按压打开控制器的主要使用的方法有三个

/// 给即将跳转的控制器传值,并且控制按压的视图周围的模糊背景大小等等
-(UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location;

/// 控制跳转的方法push & present
-(void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit;

/// 实现底部更多的按钮(这个方法需要在即将跳转的控制器中实现,和上面两个方法不在同一控制器)
-(NSArray<id<UIPreviewActionItem>> *)previewActionItems;

接下继续详细介绍:
1.首先我们需要给cell注册3DTouch,就是遵循UIViewControllerPreviewingDelegate

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"这是第%li行", indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    /// 先判断3DTouch是否可用
    if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {// 3DTouch可用
        // 注册3DTouch协议
        [self registerForPreviewingWithDelegate:self sourceView:cell];
    } else{
        NSLog(@"3DTouch不可用");
    }
    
    return cell;
}

2.注册好3DTouch之后就可以放心的使用上面的三步方法了,我们先来给控制器传个值什么的:

-(UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location {
    NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)[previewingContext sourceView]];
    
    /// 需要创建的控制器
    LYFSubViewController *viewController = [[LYFSubViewController alloc] init];
   /// 传值
    viewController.text = [NSString stringWithFormat:@"这是第%li行", indexPath.row];
    
    /// 没有毛玻璃的大小(80.f就是每一个cell的高度,大家可以改变高度来看看效果)
    CGRect rect = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 80.f);
    previewingContext.sourceRect = rect;
    
    return viewController;
}

3.接下来就是控制控制器的跳转方法啦:

-(void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
    /// 这里根据大家的需求处理,除了跳转方式也可以添加逻辑
    [self showViewController:viewControllerToCommit sender:self];
}

4.最后就是在你要跳转的控制器中实现下面的方法(就是列子中的LYFSubViewController):

/// 太简单了,就不解释了
-(NSArray<id<UIPreviewActionItem>> *)previewActionItems {
    UIPreviewAction *cancelAction = [UIPreviewAction actionWithTitle:@"取消" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"取消了");
    }];
    
    UIPreviewAction *confirmAction = [UIPreviewAction actionWithTitle:@"确定" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"确定");
    }];
    
    UIPreviewAction *defaultAction = [UIPreviewAction actionWithTitle:@"你好呀兄弟" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"你好呀,兄die");
    }];
    return @[cancelAction, confirmAction, defaultAction];
}

ok,大功告成了有木有,简直太eazy了。需要demo的同学们可以点击:https://github.com/Fdevelopmenter/3DTouchPreview.git
喜欢的点个赞在走啦

😂.png

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,033评论 3 119
  • 前言 关于这篇文章 由于iPhone 6S发布不到一年的时间,很多新特性、新技术还未普遍,不管是3D Touch的...
    Tangentw阅读 4,632评论 8 18
  • 前言 关于3D touch苹果官方文档是这么开始介绍的: 大意如下:iOS9开始,所有新的手机都增加了一个三维的用...
    VV木公子阅读 2,289评论 3 39
  • 我特别喜欢看《我的前半生》这部剧,尤其是靳东和马伊琍的对手戏,虽然刚开始他们都是针锋相对的,但我觉得这是情愫的因子...
    9785WYB阅读 520评论 0 1
  • das; -(e)s,-e/(计量时:)- ① <符号:Pfd>磅(重量单位:500克) ein halbes P...
    择地之松阅读 213评论 0 0