Swift自界說iOS中的TabBarController並為其添加動畫。本站提示廣大學習愛好者:(Swift自界說iOS中的TabBarController並為其添加動畫)文章只能為提供參考,不一定能成為您想要的結果。以下是Swift自界說iOS中的TabBarController並為其添加動畫正文
自界說TabBarController
有時刻默許的TabBarController不克不及知足我們的開辟需求,好比你想用黑色的圖標,體系卻只挪用圖標的輪廓,所以我們須要本身界說一下TabBar。
辦法一:修正TabBarController中的TabBar
新建 CustomTabBarController 類繼續自 UITabBarController,並在Storyboard中設置:
起首自界說 tabBar 的配景,在 viewDidLoad() 辦法中添加:
// 用圖片
self.tabBar.backgroundImage = UIImage(named: "TabBarBG")
// 或
// 直接用色彩
self.tabBar.barTintColor = UIColor.blackColor()
然後修正每一個子ViewController中的TabBarItem,在 viewDidLoad() 辦法中持續添加:
for (index, viewController) in self.viewControllers!.enumerate() {
// 聲明 TabBarItem 的Image,假如沒有imageWithRenderingMode辦法Image只會保存輪廓
let image = UIImage(named: "TabBar\(index)")?.imageWithRenderingMode(.AlwaysOriginal)
let selectedImage = UIImage(named: "TabBar\(index)Sel")?.imageWithRenderingMode(.AlwaysOriginal)
// 聲明新的無題目TabBarItem
let tabBarItem = UITabBarItem(title: nil, image: image, selectedImage: selectedImage)
// 設置 tabBarItem 的 imageInsets 可使圖標居中顯示
tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0)
viewController.tabBarItem = tabBarItem
}
完成!後果以下所示:
辦法二:自界說TabBar
上述辦法獨一的成績是,當你想設置item的選定配景時:
self.tabBar.selectionIndicatorImage = UIImage(named: "TabBarBGSel")
配景圖不克不及鋪滿全部item,雙方會有閒暇(假如年夜家有更好的辦法處理可以留言,感謝),如圖:
這個時刻須要自界說TabBar,起首聲明一個UIButton用來記載以後選中的Button:
var selectButton: UIButton!
在viewDidLoad()辦法的底部參加以下代碼:
// 先記載下Controller自帶的tabBar的frame
let rect = self.tabBar.frame
// 移除Controller自帶的TabBar
self.tabBar.removeFromSuperview()
// 用記載下的frame樹立一個UIView
let myView = UIView(frame: rect)
// 設置這個View的配景色
myView.backgroundColor = UIColor(patternImage: UIImage(named: "TabBarBG")!)
self.view.addSubview(myView)
for var i = 0; i < self.viewControllers?.count; i++ {
let button = UIButton()
// 依據子ViewController的個數盤算Button的寬度
let width = myView.frame.size.width / CGFloat(self.viewControllers!.count)
let x = CGFloat(i) * width
button.frame = CGRectMake(x, 0, width, myView.frame.size.height)
// 設置Button未選中時刻的圖標
let image = UIImage(named: "TabBar\(i)")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
button.setImage(image, forState: UIControlState.Normal)
// 設置Button選中時刻的圖標,留意這裡Button的狀況是Selected,而不是Highlighted
let selImage = UIImage(named: "TabBar\(i)Sel")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
button.setImage(selImage, forState: UIControlState.Selected)
// 設置Button未選中和選中時刻的配景圖
button.setBackgroundImage(UIImage(named: "TabBarBG"), forState: UIControlState.Normal)
button.setBackgroundImage(UIImage(named: "TabBarBGSel"), forState: UIControlState.Selected)
// 去失落UIButton自帶的高光後果
button.adjustsImageWhenHighlighted = false
myView.addSubview(button)
// 設置UIButton的標志
button.tag = i
button.addTarget(self, action: "onClick:", forControlEvents: UIControlEvents.TouchUpInside)
// 設置默許的選中項
if i == 0 {
button.selected = true
self.selectButton = button
}
}
/**
自界說Button的點擊事宜
:param: button
*/
func onClick(button: UIButton) {
// 將上個選中按鈕設置為未選中
self.selectButton.selected = false
// 以後按鈕設置為選中
button.selected = true
// 記載選中按鈕
self.selectButton = button
// 經由過程UITabBarController的selectedIndex屬性設置選中了哪一個UIViewController
self.selectedIndex = button.tag
}
終究後果以下:
給自界說TabBarController添加動畫
若何自界說TabBarController我們在下面曾經講過,如今為自界說的TabBar增長動畫後果。
直接上代碼:
// 用來記載以後選中按鈕
private var currentSelectedButton = UIButton()
// 用來指導選中的配景
private var selectionIndicatorImageView: UIImageView!
// 單個item的寬度
private var itemWidth: CGFloat!
在viewDidLoad()辦法中參加以下代碼:
override func viewDidLoad() {
super.viewDidLoad()
// 記載TabBarController自帶TabBar的地位
let rect = self.tabBar.frame
// 移除TabBarController自帶的TabBar
self.tabBar.removeFromSuperview()
// 自界說TabBar的配景
let backgroundView = UIView(frame: rect)
backgroundView.backgroundColor = UIColor(patternImage: UIImage(named: "TabBarBG")!)
self.view.addSubview(backgroundView)
itemWidth = backgroundView.frame.size.width / CGFloat(self.viewControllers!.count)
selectionIndicatorImageView = UIImageView(frame: CGRectMake(0, 0, itemWidth, backgroundView.frame.size.height))
selectionIndicatorImageView.image = UIImage(named: "TabBarBGSel")
backgroundView.addSubview(selectionIndicatorImageView)
for var i = 0; i < viewControllers!.count; i++ {
let button = CGMTabBarButton(frame: CGRectMake(itemWidth * CGFloat(i), 0, itemWidth, backgroundView.frame.size.height))
let image = UIImage(named: "TabBar\(i)")!
let selImage = UIImage(named: "TabBar\(i)Sel")!
button.setImage(image, forState: UIControlState.Normal)
button.setImage(selImage, forState: UIControlState.Selected)
button.addTarget(self, action: "onClick:", forControlEvents: UIControlEvents.TouchUpInside)
button.tag = i
// 去失落buttond的高光後果
button.adjustsImageWhenHighlighted = false
backgroundView.addSubview(button)
}
}
添加onClick辦法,動畫後果也在這外面完成:
func onClick(button: UIButton) {
// 將上個選中俺就設置為為選中
self.currentSelectedButton.selected = false
// 以後按鈕設置為選中
button.selected = true
self.currentSelectedButton = button
let x = CGFloat(button.tag) + 0.5
// 為TabBarItem的配景添加動畫
UIView.animateWithDuration(0.4, delay: 0.0, usingSpringWithDamping: 0.7, initialSpringVelocity: 10.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
self.selectionIndicatorImageView.center.x = self.itemWidth * x
}, completion: nil)
self.selectedIndex = button.tag
}
終究後果以下: