ios - 点击单元格时如何执行到新 ViewController 的过渡动画

标签 ios swift uitableview uiviewcontroller

我正在尝试在点击 tableView 单元格 时创建 UIViewController 转换。我正在使用模态转场转到另一个 View Controller ,并且我将使用 View Controller 的 transitioningDelegate 但它仍然不会执行动画。我不确定我是否应该在 prepareForSeguedidSelectRow 中这样做。我是 ViewController 转换的新手。我该如何纠正这个问题。

流行动画师类(class)

class PopAnimator: NSObject, UIViewControllerAnimatedTransitioning {

    func transitionDuration(using pTransitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 1.0
    }

    func animateTransition(using pTransitionContext: UIViewControllerContextTransitioning) {
        let containerView = pTransitionContext.containerView
        guard let toView = pTransitionContext.view(forKey: .to) else { return }
        containerView.addSubview(toView)
        toView.alpha = 1.0
        UIView.animate(withDuration: 1.0, animations: {
            toView.alpha = 1.0
        }) { _ in
            pTransitionContext.completeTransition(true)
        }
    }
}

目标 View Controller

class ThingsTransitionVC: UIViewController {

    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var detailsLabel: UILabel!
    @IBOutlet weak var titleLabel: UILabel!

    var transitionThing: Thing?

    override func viewDidLoad() {
        super.viewDidLoad()
        if let thing = self.transitionThing {
            self.nameLabel.text = thing.name
            self.detailsLabel.text = thing.details
            self.titleLabel.text = thing.title
        }
    }

    @IBAction func cancelButton(_ sender: UIBarButtonItem) {
        self.presentingViewController?.dismiss(animated: true)
    }
}

列出包含 tableview 的 ViewController

class ThingsListVC: UIViewController {
       override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        if segue.identifier == "ThingSegue" {
            let navVC = segue.destination as! UINavigationController
            let thingTransitionVC = navVC.topViewController as! ThingsTransitionVC
            let theThing = sender as? Thing
            thingTransitionVC.transitionThing = theThing
            thingTransitionVC.transitioningDelegate = self
        }
    }

     func tableView(_ pTableView: UITableView, didSelectRowAt pIndexPath: IndexPath) {
    let thingSelected = self.objectForIndexPath(pIndexPath)
    self.performSegue(withIdentifier: "ThingSegue", sender: thingSelected)
    pTableView.deselectRow(at: pIndexPath , animated: true)
}

}

extension ThingsListVC: UIViewControllerTransitioningDelegate {
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        let popAnimator = PopAnimator()
        return popAnimator
    }

    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return nil
    }
}

最佳答案

在源 ViewController 中使用 UIViewControllerTransitioningDelegate

  public override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "segueIdentifier1st"
        {

            let controller = segue.destination as! firstVC
            controller.transitioningDelegate = self
            controller.modalPresentationStyle = .custom

        }
        else if segue.identifier == "segueIdentifier2nd"
        {
            let controller = segue.destination as! secondVC

            controller.transitioningDelegate = self

            controller.modalPresentationStyle = .custom
        }
        //interactiveTransition.attach(to: controller)
    }
    override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {

        return true
    }
    // MARK: UIViewControllerTransitioningDelegate

    public func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning?
    {
        transition.transitionMode = .present
        if presented.restorationIdentifier!  == "segueIdentifier1st"
        {
            transition.startingPoint = CGPoint(x: btnAddWidget.center.x , y: btnAddWidget.frame.origin.y+50)
        }
        else if presented.restorationIdentifier! == "segueIdentifier2nd"
        {
            //transition.startingPoint = CGPoint(x: self.view.frame.size.width-45, y:45)
            transition.startingPoint = getTransitionOrigin()
        }
        transition.bubbleColor = backgroundThemeColor
        return transition
    }
    public func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        transition.transitionMode = .dismiss
        if dismissed.restorationIdentifier!  == "segueIdentifier1st"
        {
            transition.startingPoint = CGPoint(x: btnAddWidget.center.x , y: btnAddWidget.frame.origin.y+50)
        }
        else if dismissed.restorationIdentifier! == "segueIdentifier2nd"
        {
            //transition.startingPoint = CGPoint(x: self.view.frame.size.width-45, y:45)
            transition.startingPoint = getTransitionOrigin()
        }
        transition.bubbleColor = backgroundThemeColor

        return transition
    }

关于ios - 点击单元格时如何执行到新 ViewController 的过渡动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55965767/

相关文章:

objective-c - UITableView 因 reloadRowsAtIndexPaths 崩溃

ios - 应用内购买不起作用,显示无产品

ios - 有没有办法防止在 MapboxGL for iOS 中滚动时标注消失?

ios - 如何在 AppDelegate 中定义的其他类中使用变量

ios - tableView 单元格中的 TextField 未将文本输出到控制台

ios - self.tableView.delegate = self swift

ios - 使用不等式运算符比较对象

ios - 覆盖 func 准备(对于 segue : UIStoryboardSegue, 发件人 : Any?)不工作

ios - 如何快速将文本字段的文本带到渐变背景的前面

ios - 高分重置为 0