iOS 3D Touch 实现UITableViewCell 预览

来源:互联网 发布:重庆移动网络加速器 编辑:程序博客网 时间:2024/06/04 23:18

这里写图片描述
这里写图片描述
效果图好丑.
测试手机iPhone6s , 也就是使用了新特性 3DTouch. 囧 不知道的以为会有多难.

在开始之前

UIViewControllerPreviewingDelegate // 签订这个协议
- (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;}
2 0
原创粉丝点击