您的位置:首页 > 产品设计 > UI/UE

iOS 3D Touch 实现UITableViewCell 预览

2016-05-22 00:47 525 查看




效果图好丑.

测试手机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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: