3

我有一个 TabBarController,它有 3 个 navigationController,每个都有一个对应的 viewController:AViewController、BViewController 和 CViewController。第一个有一个 UIView 元素,我想在 viewDisappears 使用时对它运行动画:

UIView.animateWithDuration(duration, delay: 0.0, options: .CurveEaseOut, animations: {

期间...

override func viewWillDisappear(animated: Bool) {

如果用户单击选项卡栏中的项目 2 或 3,我希望该动画首先发生,然后将用户带到项目 #2 或 #3。

问题是,当用户单击不同的项目时,TabBarController 中的此代码首先被触发,然后我的 ViewController A 中的 viewwillDisappears

override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {

然后我的动画没有运行。

我应该把我的动画放在哪里,以便在用户被带到标签栏中的不同项目之前运行它?

4

2 回答 2

2

您可以实现tabBarController:shouldSelectViewController:from UITabBarControllerDelegate,返回 false,播放动画,然后以编程方式更改选项卡。

于 2016-08-16T03:38:15.097 回答
1

您需要编写自己的 UIViewControllerAnimatedTransitioning ( https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewControllerAnimatedTransitioning_Protocol/ )

试试这个代码:

TabBarController.swift

import UIKit
class TabBarController: UITabBarController, UITabBarControllerDelegate {

var buttons = [UIButton]()

override func viewDidLoad() {
    super.viewDidLoad()
    self.delegate = self

    var newViewControllers = [UIViewController]()

    for index in 0..<2 {
        let viewController = ViewController()
        var tabBarItem = UITabBarItem()

        switch index {
        case 0:
            viewController.view.backgroundColor = UIColor.blueColor()
            viewController.label.textColor = UIColor.whiteColor()
            tabBarItem = UITabBarItem(tabBarSystemItem: .More, tag: index)
        case 1:
            viewController.view.backgroundColor = UIColor.greenColor()
            viewController.label.textColor = UIColor.blackColor()
            tabBarItem = UITabBarItem(tabBarSystemItem: .Favorites, tag: index)
        default:
            break
        }

        viewController.label.text = "Text \(index)"
        viewController.tabBarItem = tabBarItem
        newViewControllers.append(viewController)
        //tabBar.items![index] = tabBarItem

    }

    selectedIndex = 0
   //    = newViewControllers
    setViewControllers(newViewControllers, animated: false)
    // Do any additional setup after loading the view, typically from a nib.
}

func tabBarController(tabBarController: UITabBarController, animationControllerForTransitionFromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    let fromIndex = tabBarController.viewControllers!.indexOf(fromVC)!
    let toIndex = tabBarController.viewControllers!.indexOf(toVC)!

    let tabChangeDirection: TabOperationDirection = toIndex < fromIndex ? .Left : .Right
    let transitionType = SDETransitionType.TabTransition(tabChangeDirection)
    let slideAnimationController = SlideAnimationController(type: transitionType)
    return slideAnimationController

}
}

ViewController.swift

import UIKit
class ViewController: UIViewController {

let label = UILabel(frame: CGRect(x: 40, y: 60, width: 80, height: 40))

override func viewDidLoad() {
    super.viewDidLoad()
    view.addSubview(label)
}

var labelHidden = false {
    willSet(value) {
        if value {
            self.label.frame.origin.y = -40
        } else {
            self.label.frame.origin.y = 60
        }
    }
}
}

SlideAnimationController.swift(资源:https ://github.com/seedante/iOS-ViewController-Transition-Demo )

import UIKit

enum SDETransitionType{
case NavigationTransition(UINavigationControllerOperation)
case TabTransition(TabOperationDirection)
case ModalTransition(ModalOperation)
}

enum TabOperationDirection{
case Left, Right
}

enum ModalOperation{
case Presentation, Dismissal
}

class SlideAnimationController: NSObject, UIViewControllerAnimatedTransitioning {

private var transitionType: SDETransitionType

init(type: SDETransitionType) {
    transitionType = type
    super.init()
}

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

func animateTransition(transitionContext: UIViewControllerContextTransitioning) {

    guard let containerView = transitionContext.containerView(), fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey), toVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey) else{
        return
    }

    let fromView = fromVC.view
    let toView = toVC.view

    var translation = containerView.frame.width
    var toViewTransform = CGAffineTransformIdentity
    var fromViewTransform = CGAffineTransformIdentity

    switch transitionType{
    case .NavigationTransition(let operation):
        translation = operation == .Push ? translation : -translation
        toViewTransform = CGAffineTransformMakeTranslation(translation, 0)
        fromViewTransform = CGAffineTransformMakeTranslation(-translation, 0)
    case .TabTransition(let direction):
        translation = direction == .Left ? translation : -translation
        fromViewTransform = CGAffineTransformMakeTranslation(translation, 0)
        toViewTransform = CGAffineTransformMakeTranslation(-translation, 0)
    case .ModalTransition(let operation):
        translation =  containerView.frame.height
        toViewTransform = CGAffineTransformMakeTranslation(0, (operation == .Presentation ? translation : 0))
        fromViewTransform = CGAffineTransformMakeTranslation(0, (operation == .Presentation ? 0 : translation))
    }

    switch transitionType{
    case .ModalTransition(let operation):
        switch operation{
        case .Presentation: containerView.addSubview(toView)
        case .Dismissal: break
        }
    default: containerView.addSubview(toView)
    }

    toView.transform = toViewTransform

    if let fromVC = fromVC as? ViewController {
        UIView.animateWithDuration(0.3, delay: 0.0, options: .CurveEaseOut, animations: {
            fromVC.labelHidden = true
            }, completion: {
                bool in
                UIView.animateWithDuration(self.transitionDuration(transitionContext), animations: {
                    fromView.transform = fromViewTransform
                    toView.transform = CGAffineTransformIdentity
                    }, completion: { finished in
                        fromView.transform = CGAffineTransformIdentity
                        toView.transform = CGAffineTransformIdentity

                        let isCancelled = transitionContext.transitionWasCancelled()
                        transitionContext.completeTransition(!isCancelled)

                        fromVC.labelHidden = false
                })
        })
    }
}
}
于 2016-08-16T06:13:01.330 回答