UIViewControllerAnimatedTransitioning每隔一段时间才有效吗?

时间:2017-08-29 17:36:36

标签: ios swift swift3 uitabbarcontroller transition

我已经在这里工作了几个小时,无法理解我做错了什么。我按照Swift: Problems with custom UIView.transition?

中描述的UITabBarControllerDelegate符合自定义标签栏控制器转换的操作

我没有使用普通的故事板标签栏按钮,我以编程方式切换selectedIndex。我的问题是只有实现了这个:

func tabBarController(_ tabBarController: UITabBarController, animationControllerForTransitionFrom fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        let animator = ModalTransition()
        animator.fromView = fromVC.view
        animator.toView = toVC.view
        return  animator

    }

动画和索引的切换仅在每隔一次发生。我有自定义按钮来切换索引和每隔一次,单击切换按钮时没有任何反应。这是我的动画:

//
//  ModalTransition.swift
//  Adventures In Design
//
//  Created by Skylar Thomas on 8/28/17.
//

import UIKit

class ModalTransition: NSObject, UIViewControllerAnimatedTransitioning {

    weak var transitionContext: UIViewControllerContextTransitioning?
    var fromView = UIView()
    var toView = UIView()
    var duration = 1.1


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

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {

        let containerView = transitionContext.containerView
        containerView.addSubview(toView)
        containerView.sendSubview(toBack: toView)

        print("ANIMATING")

        UIView.animate(withDuration: duration, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.0, options: .curveLinear, animations: {
            self.fromView.center.y += 900

        }, completion: {
            finished in

            //only works every OTHER click
            transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
            self.fromView.center.y -= 900
        })


    }
}

造成这种情况的原因是什么?是它的东西

1 个答案:

答案 0 :(得分:1)

我认为你没有分配你的toView和FromView。试试这样的事情

ehouarn@perret C:\Users\ehouarn\Desktop\npm test
> npm install --save node-sass
[...]
+ node-sass@4.5.3
added 164 packages in 17.786s
npm info ok