標籤:
好醜.
測試手機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;}
iOS 實現點擊在tableview中使用3D Touch