标签栏幻灯片动画 斯威夫特 4.



我想在标签滑动中添加动画。如何在不使用任何库的情况下在 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
}
}
}

}

这是第二个视图控制器。

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

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

最新更新