TabBar图标对选择的反弹效果就像Swift中的Twitter应用程序一样

时间:2017-02-09 11:46:31

标签: ios swift uitabbarcontroller uitabbar uitabbaritem

当我点击其中一个应用程序时,如何创建标签栏图标跳出效果,如 Twitter 应用程序?看起来像它的缩小,然后恢复正常。

enter image description here

谢谢。

3 个答案:

答案 0 :(得分:5)

这是对我有用的非常简单的解决方案。子类UITabBarController并覆盖func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem)

class AnimatedTabBarController: UITabBarController {

    private var bounceAnimation: CAKeyframeAnimation = {
        let bounceAnimation = CAKeyframeAnimation(keyPath: "transform.scale")
        bounceAnimation.values = [1.0, 1.4, 0.9, 1.02, 1.0]
        bounceAnimation.duration = TimeInterval(0.3)
        bounceAnimation.calculationMode = CAAnimationCalculationMode.cubic
        return bounceAnimation
    }()

    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        // find index if the selected tab bar item, then find the corresponding view and get its image, the view position is offset by 1 because the first item is the background (at least in this case)
        guard let idx = tabBar.items?.index(of: item), tabBar.subviews.count > idx + 1, let imageView = tabBar.subviews[idx + 1].subviews.first as? UIImageView else {
            return
        }

        imageView.layer.add(bounceAnimation, forKey: nil)
    }
}

结果看起来像这样

Bounce in tab bar

答案 1 :(得分:1)

嘿,这不是完美的代码,但应该让您了解如何实现自定义TabBarViewController。我没有编写视图布局部分,所以你应该处理它,而且标签项动画也不在这里。

class TabBarViewController: UIViewController, TabBarViewDelegate {

    private var containerView: UIView!
    private var tabBar: TabBarView!

    var selectedTabIndex: Int! {
        didSet {
            if oldValue != nil && oldValue != selectedTabIndex {
                let previousVc = childViewControllers[oldValue]
                previousVc.beginAppearanceTransition(false, animated: false)
                previousVc.view.removeFromSuperview()
                previousVc.endAppearanceTransition()
            }

            let vc = childViewControllers[selectedTabIndex]
            vc.beginAppearanceTransition(true, animated: false)
            containerView.addSubview(vc.view)
            vc.endAppearanceTransition()
        }
    }

    override func loadView() {
        let viewControllers = [YourViewController1(), YourViewController2()]

        containerView = UIView()

        tabBar = TabBarView(numberOfTabs: viewControllers.count)
        tabBar.delegate = self

        viewcontrollers.forEach {
            addChildViewController($0)
            $0.didMove(toParentViewController: self)
        }

        view = UIView()
        view.addSubview(containerView)
        view.addSubview(tabBar)

        selectedTabIndex = 0
    }

    func didSelect(tab: Int) {
        if selectedTabIndex != tab {
            selectedTabIndex = tab
        }
    }
}

protocol TabBarViewDelegate: class {
    func didSelect(tab: Int)
}

class TabBarView: UIView {

    private let tabs: [TabBarItemView]

    weak var delegate: TabBarViewDelegate? {
        didSet {
            tabs.forEach {
                $0.delegate = delegate
            }
        }
    }

    init(numberOfTabs: Int) {
        let tabs = (0...numberOfTabs).map({TabBarItemView(index: $0)})
        super.init(frame: .zero)
        let tabsStackView = UIStackView(arrangedSubviews: tabs) //.horizontal axis
        addSubview(tabsStackView)
    }
}

class TabBarItemView: UIView {

    weak var delegate: TabBarViewDelegate?

    private let index: Int
    private let imageView = UIImageView(image: UIImage(named: ""))

    init(index: Int) {
        self.index = index
        super.init(frame: .zero)

        let tap = UITapGestureRecognizer()
        tap.addTarget(self, action: #selector(onSelected))
        gestureRecognizers = [tap]
        addSubview(imageView)
    }

    @objc private func onSelected() {
        //animate imageView
        delegate?.didSelect(tab: index)
    }
}

答案 2 :(得分:1)

我知道如何做到这一点。轻松玩!!! 假设有5个标签栏项,标签是0,1,2,3,4,然后在你的标签栏控制器中:

 class xxxVC: UITabBarController {

        var storedImageViewArr:[UIImageView?] = []

    private var times = [Int].init(repeating: 0, count: 5)
    private var tempTimes = [Int].init(repeating: 0, count: 5)

     let bounceAnimation = CAKeyframeAnimation(keyPath: "transform.scale")   

    override func viewDidLoad() {
            super.viewDidLoad()

      // create image views
     getImageView()

    //set path
        setPath()
    }//xxxVC class over line

    //custom functions
    extension xxxVC{

    fileprivate func getImageView(){
times[0] = 1
            storedImageViewArr = [0,1,2,3,4].map{ (offset) -> UIImageView in
    let tempImageView = self.tabBar.subviews[offset].subviews.first as! UIImageView
    tempImageView.contentMode = .center
    return tempImageView
            }

    fileprivate func setPath(){

            bounceAnimation.values = [1.0 ,0.6, 0.9, 1.15, 0.95, 1.02, 1.0]
            bounceAnimation.duration = TimeInterval(0.5)
            bounceAnimation.calculationMode = kCAAnimationCubic
        }
    }

    //UITabBarControllerDelegate
    extension xxxVC{

        override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {

    //the arr in this count is sure, so it costs O(1)
    if times[item.tag] == 0{
            setAnimation(at: item.tag)
            times = tempTimes
            times[item.tag] += 1;return
        }
    }
    }

所以,你可以做到。