自定义视图过渡,无需使用情节提要脚本(快速)

萨沙兹

我有两种观点,我想在整个过程中进行滑动样式转换,并且我已经完成了这一工作,但是要在其中进行操作时,我没有一个观点,因此不确定如何应用我的动画类。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。我在课堂上拥有的一切是:

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)
    }
  }
}

我不能让它工作任何指针吗?

迅捷建筑师

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

通用方法

  1. 修改您CustomSegue采用两个 UIStoryboardSegue UIViewControllerAnimatedTransitioning协议。
  2. 重构,CustomSegue以便两种协议都可以使用动画。
  3. delegate在导航控制器上设置a ,导航控制器可以是其本身,以提供对push的自定义转换pop
  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)
    }
}

主机ViewController类

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
        }
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用情节提要板实现自定义表格视图节的页眉和页脚

使用情节提要板实现自定义视图布局

自定义视图以编程方式添加时不可见,但使用情节提要添加时可见

自定义单元永远不会为零(不要使用情节提要)

使用情节提要创建的自定义UITableViewCell不显示标签数据-Swift

如何使用情节提要添加自定义数据源类

使用情节提要和自定义View Controller初始化

与自定义UITabbarController一起使用情节提要

无法使用情节提要自定义实例化窗口控制器

使用情节提要板在UIViewController中初始化自定义UIView

情节提要和自定义init

情节提要中的自定义字体?

如何快速使用自定义视图?

使用情节提要Segues消除Popover视图

如何在情节提要场景中嵌入自定义视图Xib?

自定义视图(xib)在情节提要板上不可见

在情节提要中自定义表格视图单元时出现NSInternalInconsistencyException异常

为什么在情节提要中看不到我的自定义视图的预览?

使用情节提要ViewController

快速自定义视图

如何使用数据触发器停止自定义情节提要?

使用带有某些单元的情节提要的自定义UITableViewCell

如何在情节提要中使用ViewController的自定义init

无法在情节提要中更改自定义UICollectionViewCell

自情节提要的TableView的自定义初始化

如何将ibOutlet从子视图链接到情节提要Xcode中的自定义UIView类

如何使用情节提要引用UISearchController

使用情节提要中的重叠对象

在ToolTip WPF上使用情节提要