Swift - Type' CircularTransition'不符合协议' UIViewControllerAnimatedTransitioning'

时间:2017-01-01 15:46:37

标签: ios swift animation

    class CircularTransition: NSObject {

    var circle = UIView()

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

    var circleColor = UIColor.whiteColor()

    var duration = 0.3

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

    var transitionMode:CircularTransitionMode = .present

}

extension CircularTransition:UIViewControllerAnimatedTransitioning {

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

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

        if transitionMode == .present {
            if let presentedView = transitionContext.viewForKey(UITransitionContextToViewControllerKey) {
                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 = CGAffineTransformMakeScale(0.001, 0.001)
                containerView()!.addSubview(circle)


                presentedView.center = startingPoint
                presentedView.transform = CGAffineTransformMakeScale(0.001, 0.001)
                presentedView.alpha = 0
                containerView()!.addSubview(presentedView)

                UIView.animateWithDuration(duration, animations: {
                    self.circle.transform = CGAffineTransformIdentity
                    presentedView.transform = CGAffineTransformIdentity
                    presentedView.alpha = 1
                    presentedView.center = viewCenter

                    }, completion: { (success:Bool) in
                        transitionContext.completeTransition(success)
                })
            }

        } else {
            let transitionModeKey = (transitionMode == .pop) ? UITransitionContextToViewKey : UITransitionContextFromViewKey

            if let returningView = transitionContext.viewForKey(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.animateWithDuration(duration, animations: {
                    self.circle.transform = CGAffineTransformMakeScale(0.001, 0.001)
                    returningView.transform = CGAffineTransformMakeScale(0.001, 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 offestVector = sqrt(xLength * xLength + yLength * yLength) * 2
        let size = CGSize(width: offestVector, height: offestVector)

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

    }

}

enter image description here 错误:"输入' CircularTransition'不符合协议' UIViewControllerAnimatedTransitioning'"

我试图按照youtube教程(链接贝娄),问题是他使用的是Swift 3,但我不是。

  

Cred:https://www.youtube.com/watch?v=B9sH_VxPPo4

以下是视频中使用的代码。

  

整个代码,但在swift 3中:https://github.com/brianadvent/CircularTransition/tree/master/CircularTransitionft

1 个答案:

答案 0 :(得分:1)

我解决了。

我只是替换了以下内容,

func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {

func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {

func animateTransition(transitionContext: UIViewControllerContextTransitioning) {