iOS 實現點擊在tableview中使用3D Touch

來源:互聯網
上載者:User

標籤:



好醜.
測試手機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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.