美文网首页程序员
iOS 3D Touch打开控制器(类似微信聊天样式)

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

作者: 枫developer | 来源:发表于2018-08-12 09:50 被阅读0次

    使用过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

    相关文章

      网友评论

        本文标题:iOS 3D Touch打开控制器(类似微信聊天样式)

        本文链接:https://www.haomeiwen.com/subject/gzhfbftx.html