简体   繁体   English

播放视频:“偷看”和“流行”(3D Touch)背景不断滚动之后

[英]Have video: After Peek and Pop (3D Touch) background keeps scrolling

Video of the bug: Video 错误的视频视频

I have a regular table view with UITableViewCells. 我有一个带有UITableViewCells的常规表视图。 It looks like messages. 它看起来像消息。 I have another view controller OperationDetailsViewController. 我有另一个视图控制器OperationDetailsViewController。

In table view delegate I have this: 在表视图委托中,我有以下内容:

// 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)
        }
    }
}

To show preview when user taps on message background I have this: 要在用户点击消息背景时显示预览,我需要这样做:

/// Create a previewing view controller to be shown at "Peek".
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 "Pop" action.
func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
    // Reuse the "Peek" view controller for presentation.
    show(viewControllerToCommit, sender: self)
}

But when user uses 3D Touch background keeps scrolling like on the video 但是当用户使用3D Touch时,背景会像在视频上一样不断滚动

It strange because in iMessage application it doesn't happen 这很奇怪,因为在iMessage应用程序中它不会发生

I've tried with default tableView and I don't have the situation. 我尝试使用默认的tableView,但没有这种情况。 That it to say, when I 3d touch my tableView cell, it pops up the detailView, and the background view (tableView) is not scrollable. 就是说,当我3D触摸tableView单元格时,它会弹出detailView,并且背景视图(tableView)无法滚动。

Also, you don't have to register for previewing every cell, you can just register the whole tableview cell at once. 另外,您不必注册预览每个单元格,只需一次注册整个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
    }
}

PreviewDelegate PreviewDelegate

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)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM