iOS 3D Touch 預(yù)覽特定的 UITableViewCell 視圖 - 改

具體應(yīng)用場景看簡書首頁、微博首頁, 實現(xiàn)預(yù)覽的效果, 而下面是我做的效果圖


預(yù)覽.png

預(yù)覽

在開始之前

UIViewControllerPreviewingDelegate // 簽訂這個協(xié)議

Objective-C版本

- (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的代理方法,輕按即可觸發(fā)彈出vc
- (UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{
    //通過[previewingContext sourceView]拿到對應(yīng)的cell的數(shù)據(jù);
    NSIndexPath *indexPath = [_tableView indexPathForCell:(UITableViewCell* )[previewingContext sourceView]];
    // 用于顯示預(yù)覽的vc
    ListViewController *listVc = [[ListViewController alloc] init];
    // 演示的是傳入一個字符串 , 實際可能是你需要的model
    listVc.strText = [self.arrayData objectAtIndex:indexPath.row];
    return listVc;
}
#pragma mark -  pop的代理方法,在此處可對將要進(jìn)入的vc進(jìn)行處理
- (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;
}

Swift版本

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        // 指定代理人
        if #available(iOS 9.0, *) {
            self.registerForPreviewingWithDelegate(self, sourceView: cell)
        } else {
            // Fallback on earlier versions
        }
    }
/// MARK: - peek的代理方法, 長按觸發(fā)彈出預(yù)覽VC
    func previewingContext(previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
        let preVC = PreviewViewController()
        if #available(iOS 9.0, *) {
            let indexPath = self.tableView.indexPathForCell((previewingContext.sourceView) as! AlamofieCustomCell)! as NSIndexPath
            preVC.preViewModel = self.dataArray[indexPath.row] as! AlamofireVCModel
        } else {
            // Fallback on earlier versions
        }
        return preVC
    }
/// MARK: - pop的代理方法,在此處可對將要進(jìn)入的vc進(jìn)行處理
    func previewingContext(previewingContext: UIViewControllerPreviewing, commitViewController viewControllerToCommit: UIViewController) {
        // code
    }
@available(iOS 9.0, *)
    override func previewActionItems() -> [UIPreviewActionItem] {
        var items = [UIPreviewAction]()
        let action1 = UIPreviewAction.init(title: "收藏", style: UIPreviewActionStyle.Default, handler: { (action: UIPreviewAction, previewViewController: UIViewController) in
                print("收藏")
            })
            
        let action2 = UIPreviewAction.init(title: "喜歡", style: UIPreviewActionStyle.Default, handler: { (action: UIPreviewAction, previewViewController: UIViewController) in
                print("喜歡")
            })    
        items.append(action1)
        items.append(action2)
        return items;
    }

iOS9:預(yù)覽特定的 UITableViewCell 視圖
更新文章標(biāo)題 - -- 時間: 2016.07.11 早九點
更新swift - -- 時間: 2016.07.19 早

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,291評論 4 61
  • 今天已經(jīng)是實習(xí)的第19天了,前幾天就想寫一下東西了,希望以后的自己可以回憶起來,有那么幾個證據(jù)證明我努力過。 細(xì)細(xì)...
    spongeboblhy閱讀 371評論 2 3
  • 2017.4.24 晴 星期一 閨女睡了,作業(yè)還沒寫完,明天要考試了,困就讓她睡吧,養(yǎng)足精神才能好好發(fā)揮。寫...
  • 一切都不會過去,無論是如歷史般以透明形式存在的精髓抑或是你所看到的這一個完美或不完美的世界,如破鏡重圓留下的痕跡,...
    話嘮_xixi閱讀 6,630評論 0 0
  • 通過第一天的訓(xùn)練,我想我最大的一個收獲就是: 遇到困難或者挫折等問題我的反應(yīng)將會是興奮的、激昂的、熱血沸騰的。 因...
    故里草木生閱讀 235評論 0 0

友情鏈接更多精彩內(nèi)容