I'm trying to create a UIViewController
transition when a tableView cell
is tapped. I'm using a modal segue to go to another Viewcontroller and i use the viewcontroller's transitioningDelegate
i'm going to but it still won't perform the animation. I'm not sure if i'm supposed to do it this way in prepareForSegue
or didSelectRow
. I'm new to ViewController transitions. How can i correct this.
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)
}
}
Lists ViewController that contains the tableview
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
}
}
Use UIViewControllerTransitioningDelegate
in your source ViewController
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
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.