标签栏幻灯片动画雨燕4

时间:2018-06-21 08:31:50

标签: ios uitabbarcontroller swift4 uitabbar

我想向标签滑动添加动画。在不使用任何库的情况下,如何在Swift 4中完成?

我在屏幕上向右或向左滑动两个标签,以更改标签栏。

我将如何使它看起来像在向右或向左滑动标签栏,而不是立即更改标签栏。

此外,我已经在FirstViewController和SecondViewController中分别定义了手势。有没有一种方法可以在一个地方定义它并调用它。 #newToSwift

import UIKit

class FirstViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:)))
        let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:)))

        leftSwipe.direction = .left
        rightSwipe.direction = .right

        view.addGestureRecognizer(leftSwipe)
        view.addGestureRecognizer(rightSwipe)

    }

    @objc func handleSwipes(_ sender:UISwipeGestureRecognizer) {
        print(sender.direction);
        if sender.direction == .left {
            if (self.tabBarController?.selectedIndex)! < 2 { // set your total tabs here
                self.tabBarController?.selectedIndex += 1
            }
        } else if sender.direction == .right {
            if (self.tabBarController?.selectedIndex)! > 0 {
                self.tabBarController?.selectedIndex -= 1
            }
        }
    }


}

这是第二个ViewController。

// SecondViewController.swift

import UIKit

class SecondViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:)))
        let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:)))

        leftSwipe.direction = .left
        rightSwipe.direction = .right

        view.addGestureRecognizer(leftSwipe)
        view.addGestureRecognizer(rightSwipe)
    }

    @objc func handleSwipes(_ sender:UISwipeGestureRecognizer) {
        print(sender.direction);
        if sender.direction == .left {
            if (self.tabBarController?.selectedIndex)! < 2 { // set your total tabs here
                self.tabBarController?.selectedIndex += 1
            }
        } else if sender.direction == .right {
            if (self.tabBarController?.selectedIndex)! > 0 {
                self.tabBarController?.selectedIndex -= 1
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我想您可以创建自己的UITabBarController子类,并将手势识别器添加到其视图中,然后从那里获取它。应该与您共享的代码相同。