1

I'm trying to add custom segue animations to my app but keep getting this error:

Cannot assign value of type 'SlideAnimation.Type' to type 'UIViewControllerTransitioningDe

My ViewController code:

import UIKit

class ViewController: UIViewController{
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let destination = segue.destination
        destination.transitioningDelegate = SlideAnimation
    }
}

My SlideAnimation.swift code:

import UIKit

class SlideAnimation: NSObject,UIViewControllerAnimatedTransitioning,UIViewControllerTransitioningDelegate {
    let duration = 0.5

    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return self
    }

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

    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return duration
    }

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        guard let toView = transitionContext.view(forKey: UITransitionContextViewKey.to) else{
           return
        }

        let container = transitionContext.containerView
        let screenOffUp = CGAffineTransform(translationX: 0, y: -container.frame.height)
        let screenOffDown = CGAffineTransform(translationX: 0, y:container.frame.height)

        container.addSubview(fromView)
        container.addSubview(toView)

        toView.transform = screenOffUp

        UIView.animate(withDuration: duration, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0.8, options: [], animations: {
            fromView.transform = screenOffDown
            fromView.alpha = 0.5
            toView.transform = CGAffineTransform.identity
            toView.alpha = 1


        }) {(success) in
            transitionContext.completeTransition(success)
        }
    }
  }
}

I'm also new to Xcode and this website so sorry if this is very unclear.

rmaddy
  • 298,130
  • 40
  • 468
  • 517
Laurcode
  • 17
  • 3

1 Answers1

0

The issue is being caused by the line:

destination.transitioningDelegate = SlideAnimation

You need to pass an instance of a class that conforms to UIViewControllerTransitioningDelegate but you are passing the name of a class.

You need to create an instance of SlideAnimation and set that as the delegate:

destination.transitioningDelegate = SlideAnimation()

But this actually will cause a new issue because the transitioningDelegate is a weak reference and will become nil immediately. You need to create a strong reference to the SlideAnimation instance.

import UIKit

class ViewController: UIViewController{
    let transitionDelegate = SlideAnimation()

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let destination = segue.destination
        destination.transitioningDelegate = transitionDelegate
    }
}
rmaddy
  • 298,130
  • 40
  • 468
  • 517