Я пытаюсь создать переход UIViewController
при нажатии tableView cell
. Я использую модальный переход, чтобы перейти к другому Viewcontroller, и я использую Viewcontroller transitioningDelegate
, я собираюсь, но он все равно не будет выполнять анимацию. Я не уверен, должен ли я сделать это таким образом в prepareForSegue
или didSelectRow
. Я новичок в переходах ViewController. Как я могу это исправить.
Pop Animator 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)
}
}
}
Destination ViewController
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)
}
}
Списки 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
}
}