• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 有视频: After Peek and Pop (3D Touch) background keeps scrolling

[复制链接]
菜鸟教程小白 发表于 2022-12-11 18:30:25 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

错误视频:Video

我有一个带有 UITableViewCells 的常规表格 View 。它看起来像消息。我有另一个 View Controller OperationDetailsViewController。

在 TableView 委托(delegate)中我有这个:

// need that dict for 3D touch
var dict_previwingControllers_cellIsKey = [UITableViewCell: UIViewControllerPreviewing]()

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

    // setup peek and pop (register proper view)
    if traitCollection.forceTouchCapability == .available, let cellForChat = cell as? OperationsSingleCell {
        let previewingController = registerForPreviewing(with: self, sourceView: cellForChat.viewTextBG)
        dict_previwingControllers_cellIsKey[cell] = previewingController // remember because we need to unregister
    }
}

func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    // setup peek and pop (unregister)
    if traitCollection.forceTouchCapability == .available {
        if let previwingController = dict_previwingControllers_cellIsKey[cell] {
            unregisterForPreviewing(withContext: previwingController)
        }
    }
}

要在用户点击消息背景时显示预览,我有这个:

/// Create a previewing view controller to be shown at "eek".
func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
    // Obtain cell by previewing context
    guard let cell = (dict_previwingControllers_cellIsKey as NSDictionary).allKeys(for: previewingContext).first as? OperationsSingleCell else {return nil}
//        guard let indexPath = tableViewMain.indexPathForRow(at: location),
//            let cell = tableViewMain.cellForRow(at: indexPath) as? OperationsSingleCell else { return nil }


    guard let operationDetailsVC = storyboard?.instantiateViewController(withIdentifier: "OperationDetailsVC") as? OperationDetailsVC else {return nil}
    // custom properties
    operationDetailsVC.dbOrder = cell.dbOrder
    operationDetailsVC.navigationControllerOfParentVC = navigationController
    // need to load view so we will know content size
    operationDetailsVC.view.reloadInputViews()

    let contentHeight = operationDetailsVC.scrollViewMain.contentSize.height

    /*
     Set the height of the preview by setting the preferred content size of the detail view controller.
     Width should be zero, because it's not used in portrait.
     */
    operationDetailsVC.preferredContentSize = CGSize(width: 0.0, height: contentHeight)

    // Set the source rect to the cell frame, so surrounding elements are blurred.
    previewingContext.sourceRect = cell.viewTextBG.bounds

    return operationDetailsVC
}

/// Present the view controller for the "op" action.
func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
    // Reuse the "eek" view controller for presentation.
    show(viewControllerToCommit, sender: self)
}

但是当用户使用 3D Touch 时,背景会像视频一样滚动

这很奇怪,因为在 iMessage 应用程序中它不会发生



Best Answer-推荐答案


我尝试过使用默认的 tableView,但我没有这种情况。也就是说,当我3d触摸我的tableView单元格时,它会弹出detailView,并且背景 View (tableView)不可滚动。

另外,您不必注册预览每个单元格,您可以一次注册整个 tableview 单元格。

class ViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        if traitCollection.forceTouchCapability == .available {
            registerForPreviewing(with: self, sourceView: tableView)
        }
    }

}

extension ViewController: UITableViewDataSource, UITableViewDelegate {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 20
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = "Hello World"
        return cell
    }
}

预览委托(delegate)

extension ViewController: UIViewControllerPreviewingDelegate {
    func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
        guard let indexPath = tableView.indexPathForRow(at: location) else { return nil }
        guard let cell = tableView.cellForRow(at: indexPath) else { return nil }

        guard let operationDetailsVC = storyboard?.instantiateViewController(withIdentifier: "OperationDetailsVC") as? OperationDetailsVC else { return nil }

        operationDetailsVC.preferredContentSize = CGSize(width: 0, height: 300)
        previewingContext.sourceRect = cell.frame

        // You can decide to return a viewController or nil for certain cell
        return operationDetailsVC

    }

    func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
        show(viewControllerToCommit, sender: self)
    }
}

关于ios - 有视频: After Peek and Pop (3D Touch) background keeps scrolling,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41498924/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap