mercredi 8 février 2017

Custom transition not called

Vote count: 0

I have a class for a transition named CircularTransition, but the transition does not work.

CircularTransition.swift :

import UIKit

class CircularTransition: NSObject {

var circle = UIView()

var startingPoint = CGPoint.zero {
    didSet {
        circle.center = startingPoint
    }
}

var circleColor = UIColor.white

var duration = 0.3

enum CircularTransitionMode: Int {
    case present, dismiss, pop
}

var transitionMode: CircularTransitionMode = .present

}

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

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
    let containerView = transitionContext.containerView

    if transitionMode == .present {
        if let presentedView = transitionContext.view(forKey: UITransitionContextViewKey.to) {
            let viewCenter = presentedView.center
            let viewSize = presentedView.frame.size

            circle = UIView()

            circle.frame = frameForCircle(withViewCenter: viewCenter, size: viewSize, startPoint: startingPoint)

            circle.layer.cornerRadius = circle.frame.size.height / 2
            circle.center = startingPoint
            circle.backgroundColor = circleColor
            circle.transform = CGAffineTransform(scaleX: 0.001, y: 0.001)
            containerView.addSubview(circle)
            presentedView.center = startingPoint
            presentedView.transform = CGAffineTransform(scaleX: 0.001, y: 0.001)
            presentedView.alpha = 0
            containerView.addSubview(presentedView)

            UIView.animate(withDuration: duration, animations: {
                self.circle.transform = CGAffineTransform.identity
                presentedView.transform = CGAffineTransform.identity
                presentedView.alpha = 1
                presentedView.center = viewCenter
            }, completion: { (success: Bool) in
                transitionContext.completeTransition(success)
            })
        }
    } else {
        let transitionModeKey = (transitionMode == .pop) ? UITransitionContextViewKey.to : UITransitionContextViewKey.from

        if let returningView = transitionContext.view(forKey: transitionModeKey) {
            let viewCenter = returningView.center
            let viewSize = returningView.frame.size

            circle.frame = frameForCircle(withViewCenter: viewCenter, size: viewSize, startPoint: startingPoint)
            circle.layer.cornerRadius = circle.frame.size.height / 2
            circle.center = startingPoint

            UIView.animate(withDuration: duration, animations: {
                self.circle.transform = CGAffineTransform(scaleX: 0.001, y: 0.001)
                returningView.transform = CGAffineTransform(scaleX: 0.001, y: 0.001)
                returningView.center = self.startingPoint
                returningView.alpha = 0

                if self.transitionMode == .pop {
                    containerView.insertSubview(returningView, belowSubview: returningView)
                    containerView.insertSubview(self.circle, belowSubview: returningView)
                }
            }, completion: { (success: Bool) in
                returningView.center = viewCenter
                returningView.removeFromSuperview()

                self.circle.removeFromSuperview()

                transitionContext.completeTransition(success)
            })
        }
    }
}
}

func frameForCircle (withViewCenter viewCenter: CGPoint, size viewSize: CGSize, startPoint: CGPoint) -> CGRect {
let xLength = fmax(startPoint.x, viewSize.width - startPoint.x)
let yLength = fmax(startPoint.y, viewSize.height - startPoint.y)

let offsetVector = sqrt(xLength * xLength + yLength * yLength) * 2
let size = CGSize(width: offsetVector, height: offsetVector)

return CGRect(origin: CGPoint.zero, size: size)
}

And in the view controller:

class EnterPlaylistIDViewController: UIViewController, UITextFieldDelegate, UIViewControllerTransitioningDelegate {
    @IBOutlet weak var menuButton: UIButton!
    let transition = CircularTransition()

func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    transition.transitionMode = .present
    transition.startingPoint = menuButton.center
    transition.circleColor = UIColor.yellow
    return transition
}

func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    transition.transitionMode = .dismiss
    transition.startingPoint = menuButton.center
    transition.circleColor = UIColor.yellow
    return transition
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "select" {
        let vc = segue.destination as! SecondTableViewController
        vc.id = textField.text!
    } else if segue.identifier == "player" {
        let secondVC = segue.destination as! AudioVC
        secondVC.transitioningDelegate = self
        secondVC.modalPresentationStyle = .custom
    }
}
}

The segues are configured correctly in Interface Builder. The problem is that the transition is not custom, is the normal "show" one. (the view controllers are linked to navigation controllers)

asked 29 secs ago

Let's block ads! (Why?)



Custom transition not called

Aucun commentaire:

Enregistrer un commentaire