iOS 3D Touch 预览特定的 UITableViewCell 视图 - 改

具体应用场景看简书首页、微博首页, 实现预览的效果, 而下面是我做的效果图


预览.png

预览

在开始之前

UIViewControllerPreviewingDelegate // 签订这个协议

Objective-C版本

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellId"];
    // 对每个cell指定代理, 大致是这个意思
    [self registerForPreviewingWithDelegate:self sourceView:cell];
    //
    cell.textLabel.text = self.arrayData[indexPath.row];
    return cell;
}
#pragma mark - peek的代理方法,轻按即可触发弹出vc
- (UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{
    //通过[previewingContext sourceView]拿到对应的cell的数据;
    NSIndexPath *indexPath = [_tableView indexPathForCell:(UITableViewCell* )[previewingContext sourceView]];
    // 用于显示预览的vc
    ListViewController *listVc = [[ListViewController alloc] init];
    // 演示的是传入一个字符串 , 实际可能是你需要的model
    listVc.strText = [self.arrayData objectAtIndex:indexPath.row];
    return listVc;
}
#pragma mark -  pop的代理方法,在此处可对将要进入的vc进行处理
- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
{
}

在 ListViewController 中我用一个label作为演示的, 您可能还需要添加底部菜单(类似于 收藏 喜欢这样)

-(NSArray<id<UIPreviewActionItem>> *)previewActionItems
 {
     UIPreviewAction * action1 = [UIPreviewAction actionWithTitle:@"收藏" style:1 handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
            NSLog(@"收藏");
    }];

     UIPreviewAction * action2 = [UIPreviewAction actionWithTitle:@"喜欢" style:0 handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
            NSLog(@"喜欢");

     }];
     NSArray *items = @[action1,action2];
     return items;
}

Swift版本

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        // 指定代理人
        if #available(iOS 9.0, *) {
            self.registerForPreviewingWithDelegate(self, sourceView: cell)
        } else {
            // Fallback on earlier versions
        }
    }
/// MARK: - peek的代理方法, 长按触发弹出预览VC
    func previewingContext(previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
        let preVC = PreviewViewController()
        if #available(iOS 9.0, *) {
            let indexPath = self.tableView.indexPathForCell((previewingContext.sourceView) as! AlamofieCustomCell)! as NSIndexPath
            preVC.preViewModel = self.dataArray[indexPath.row] as! AlamofireVCModel
        } else {
            // Fallback on earlier versions
        }
        return preVC
    }
/// MARK: - pop的代理方法,在此处可对将要进入的vc进行处理
    func previewingContext(previewingContext: UIViewControllerPreviewing, commitViewController viewControllerToCommit: UIViewController) {
        // code
    }
@available(iOS 9.0, *)
    override func previewActionItems() -> [UIPreviewActionItem] {
        var items = [UIPreviewAction]()
        let action1 = UIPreviewAction.init(title: "收藏", style: UIPreviewActionStyle.Default, handler: { (action: UIPreviewAction, previewViewController: UIViewController) in
                print("收藏")
            })
            
        let action2 = UIPreviewAction.init(title: "喜欢", style: UIPreviewActionStyle.Default, handler: { (action: UIPreviewAction, previewViewController: UIViewController) in
                print("喜欢")
            })    
        items.append(action1)
        items.append(action2)
        return items;
    }

iOS9:预览特定的 UITableViewCell 视图
更新文章标题 - -- 时间: 2016.07.11 早九点
更新swift - -- 时间: 2016.07.19 早

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

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,196评论 4 61
  • 今天已经是实习的第19天了,前几天就想写一下东西了,希望以后的自己可以回忆起来,有那么几个证据证明我努力过。 细细...
    spongeboblhy阅读 2,548评论 2 3
  • 2017.4.24 晴 星期一 闺女睡了,作业还没写完,明天要考试了,困就让她睡吧,养足精神才能好好发挥。写...
    厦门路小学邵艺馨妈妈阅读 1,423评论 0 2
  • 一切都不会过去,无论是如历史般以透明形式存在的精髓抑或是你所看到的这一个完美或不完美的世界,如破镜重圆留下的痕迹,...
    话唠_xixi阅读 11,490评论 0 0
  • 通过第一天的训练,我想我最大的一个收获就是: 遇到困难或者挫折等问题我的反应将会是兴奋的、激昂的、热血沸腾的。 因...
    故里草木生阅读 1,371评论 0 0