不使用故事板转换的自定义视图转换(Swift)

7

我希望能够在两个视图之间实现滑动式的过渡,并且当有segue时,我已经做到了这一点,但是这里没有segue,所以我不确定如何应用我的动画类。我的类中只有:

let stb = UIStoryboard(name: "Walkthrough", bundle: nil)
    let walkthrough = stb.instantiateViewControllerWithIdentifier("walk") as! BWWalkthroughViewController

self.presentViewController(walkthrough, animated: true, completion: nil)

我想应用下面的自定义segue:

class CustomSegue: UIStoryboardSegue {

  override func perform() {
    // Assign the source and destination views to local variables.
    var firstVCView = self.sourceViewController.view as UIView!
    var secondVCView = self.destinationViewController.view as UIView!

    // Get the screen width and height.
    let screenWidth = UIScreen.mainScreen().bounds.size.width
    let screenHeight = UIScreen.mainScreen().bounds.size.height

    // Specify the initial position of the destination view.
    secondVCView.frame = CGRectMake(0.0, screenHeight, screenWidth, screenHeight)

    // Access the app's key window and insert the destination view above the current (source) one.
    let window = UIApplication.sharedApplication().keyWindow
    window?.insertSubview(secondVCView, aboveSubview: firstVCView)

    // Animate the transition.
    UIView.animateWithDuration(0.2, animations: { () -> Void in
      firstVCView.frame = CGRectOffset(firstVCView.frame, -screenWidth, 0.0)
      secondVCView.frame = CGRectOffset(secondVCView.frame, -screenWidth, 0.0)

      }) { (Finished) -> Void in

      self.sourceViewController.presentViewController(self.destinationViewController as! UIViewController,
          animated: false,
          completion:nil)
    }
  }
}

我无法让它工作,请给我一些指针吗?


请注意,您不需要使用 UIScreen.mainScreen(),因为它可能会引起一些混淆。另外请注意,如果您将动画减慢到4秒,它似乎会显示出不希望的图像残留。 - SwiftArchitect
1个回答

5

虽然第一个答案应该是“使用Storyboard Segues”,但你可以通过以下方式解决自定义转场问题:

通用方法

  1. 修改你的CustomSegue以同时采用UIStoryboardSegueUIViewControllerAnimatedTransitioning协议。
  2. 重构CustomSegue,使动画可被两个协议使用。
  3. 设置导航控制器的delegate(可以是它本身),以提供自定义转场到pushpop操作。
  4. animationControllerForOperation创建并返回一个CustomSegue实例,其中包含你选择的标识符。

概述

// Adopt both protocols
class CustomSegue: UIStoryboardSegue, UIViewControllerAnimatedTransitioning {

    func animate(firstVCView:UIView,
        secondVCView:UIView,
        containerView:UIView,
        transitionContext: UIViewControllerContextTransitioning?) {
            // factored transition code goes here
                }) { (Finished) -> Void in
                    if let context = transitionContext {
                        // UIViewControllerAnimatedTransitioning
                    } else {
                        // UIStoryboardSegue
                    }
            }
    }

    func transitionDuration(transitionContext: UIViewControllerContextTransitioning) -> NSTimeInterval {
        // return timing
    }

    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        // Perform animate using transitionContext information
        // (UIViewControllerAnimatedTransitioning)
    }

    override func perform() {
        // Perform animate using segue (self) variables
        // (UIStoryboardSegue) 
    }
}

以下是完整的代码示例。它已经经过测试。请注意,我还修复了原问题中的一些动画错误,并添加了淡入效果以强调动画。


CustomSegue 类

// Animation for both a Segue and a Transition
class CustomSegue: UIStoryboardSegue, UIViewControllerAnimatedTransitioning {

    func animate(firstVCView:UIView,
        secondVCView:UIView,
        containerView:UIView,
        transitionContext: UIViewControllerContextTransitioning?) {

            // Get the screen width and height.
            let offset = secondVCView.bounds.width

            // Specify the initial position of the destination view.
            secondVCView.frame = CGRectOffset(secondVCView.frame, offset, 0.0)

            firstVCView.superview!.addSubview(secondVCView)
            secondVCView.alpha = 0;

            // Animate the transition.
            UIView.animateWithDuration(self.transitionDuration(transitionContext!),
                animations: { () -> Void in
                    firstVCView.frame = CGRectOffset(firstVCView.frame, -offset, 0.0)
                    secondVCView.frame = CGRectOffset(secondVCView.frame, -offset, 0.0)
                    secondVCView.alpha = 1; // emphasis

                }) { (Finished) -> Void in
                    if let context = transitionContext {
                        context.completeTransition(!context.transitionWasCancelled())
                    } else {
                        self.sourceViewController.presentViewController(
                            self.destinationViewController as! UIViewController,
                            animated: false,
                            completion:nil)
                    }
            }
    }

    func transitionDuration(transitionContext: UIViewControllerContextTransitioning) -> NSTimeInterval {
        return 4 // four seconds
    }

    // Perform Transition (UIViewControllerAnimatedTransitioning)
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        self.animate(transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)!.view,
            secondVCView: transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)!.view,
            containerView: transitionContext.containerView(),
            transitionContext: transitionContext)
    }

    // Perform Segue (UIStoryboardSegue)
    override func perform() {
        self.animate(self.sourceViewController.view!!,
            secondVCView: self.destinationViewController.view!!,
            containerView: self.sourceViewController.view!!.superview!,
            transitionContext:nil)
    }
}

主视图控制器类

class ViewController: UIViewController, UINavigationControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.delegate = self
    }

    func navigationController(navigationController: UINavigationController, animationControllerForOperation operation: UINavigationControllerOperation, fromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        switch operation {
        case .Push:
             return CustomSegue(identifier: "Abc", source: fromVC, destination: toVC)

        default:
            return nil
        }
    }
}

谢谢,我不知道如何将其应用到我的代码中? - SashaZ
太棒了,我想在家里应用它。下班后我会试一试,太神奇了! - SashaZ
我使用了你的代码,但是无法对宽度和高度属性进行动画处理。你有什么想法吗? - Lenny1357
尝试过这个,但是 transitionContext 不能为 nil。因为使用 transitionDuration(using: transitionContext!) 暗示着 "transitionContext!" 不为 nil。 - Pascale Beaulac

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接