1
A
回答
0
嘿,這不是完美的代碼,但應該給你一個關於如何實現自定義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)
}
}
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
}
}
}
所以,你可以做到這一點。
+0
請!明星我! –
相關問題
- 1. 在Swift 2中的viewDidLoad中選擇了兩個TabBar圖標?
- 2. Twitter的類似反
- 3. 反應導航中選擇和未選中狀態的不同tabBar圖標?
- 4. Tabbar與Jquery效果相似
- 5. 在沒有UIScrollView的iphone應用中模擬反彈效果
- 6. 如何使用Pywinauto在Windows應用程序中選擇圖標
- 7. HTML應用程序 - 帶有類似選擇菜單的gmail
- 8. 使用類似Facebook/Twitter的設計構建iOS應用程序
- 9. 在Web應用程序中選擇非標準圖像區域
- 10. Angular2 ngrx的類似Twitter的應用程序。構建AppState
- 11. Android:在自定義圖庫中禁用「反彈」效果
- 12. Twitter Widget Cordova應用程序彈出
- 13. Splashpage與Swift類似的Twitter嗎?
- 14. Swift tabbar添加選擇器到tabItem
- 15. iphone應用程序的tabbar
- 16. 在Rails應用程序中實現類似Gmail的標籤
- 17. 允許用戶在應用程序中選擇本地圖像Swift
- 18. Xcode,我的應用程序圖標獲取光澤效果,但我的應用程序商店512圖標不?
- 19. 的TabBar圖標不顯示和選項都不起作用的反應本地
- 20. 如何在我的反應應用程序中運行彈出?
- 21. 有選擇地允許在TabBar/UINavigation應用程序中自動旋轉
- 22. 在基於視圖的應用程序中的水效果
- 23. 在XCode中選擇TabBar項目標題和圖像6.4
- 24. excel類似javascript中的應用程序
- 25. 在TabBar iPhone應用程序中的模式視圖
- 26. 類似於iOS彈簧板行爲的iOS應用程序
- 27. 蘋果手錶應用程序的應用程序圖標
- 28. TabBar應用程序與NavigationBar
- 29. 在我的應用程序UIActivityViewController中彈出WeChat/Weixin類似Safari的分享表?
- 30. Mac應用程序中的「Expose」效果
你可以通過實現一個自定義的TabBarViewController和TabBarView來達到這個效果 –
@GonjiDev謝謝。但我該怎麼做?你能告訴我一些片段嗎? – Sajad