iOS 3D Touch 实现UITableViewCell 预览

这里写图片描述
这里写图片描述
效果图好丑.
测试手机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;
}
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值