Skip to content

Commit

Permalink
Version 1.1.3
Browse files Browse the repository at this point in the history
- Added `shouldMoveToTabAtIndex`
- Added `removeViewController`
- Added `removeAction `
- Fixed selection indicator glitch
  • Loading branch information
Minitour committed Apr 26, 2017
1 parent c56e9c0 commit cd810a7
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 31 deletions.
Binary file removed .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,5 @@ fastlane/report.xml
fastlane/Preview.html
fastlane/screenshots
fastlane/test_output

\.DS_Store
2 changes: 1 addition & 1 deletion AZTabBar.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "AZTabBar"
s.version = "1.1.2"
s.version = "1.1.3"
s.summary = "A custom tab bar controller for iOS written in Swift 3.0"
s.homepage = "https://github.com/Minitour/AZTabBarController"
s.license = "MIT"
Expand Down
11 changes: 8 additions & 3 deletions AZTabBarController/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class ViewController: UIViewController {



tabController.setViewController(ColorSelectorController.instance(), atIndex: 0)
//tabController.setViewController(ColorSelectorController.instance(), atIndex: 0)

let darkController = getNavigationController(root: LabelController.controller(text: "Search", title: "Recents"))
darkController.navigationBar.barStyle = .black
Expand All @@ -75,13 +75,15 @@ class ViewController: UIViewController {

tabController.highlightColor = #colorLiteral(red: 0.1803921569, green: 0.8, blue: 0.4431372549, alpha: 1)

//tabController.highlightedBackgroundColor

tabController.defaultColor = #colorLiteral(red: 0.09048881881, green: 0.09048881881, blue: 0.09048881881, alpha: 1)

//tabController.highlightButton(atIndex: 2)

tabController.buttonsBackgroundColor = UIColor(colorLiteralRed: (247.0/255), green: (247.0/255), blue: (247.0/255), alpha: 1.0)//#colorLiteral(red: 0.2039215686, green: 0.2862745098, blue: 0.368627451, alpha: 1)

tabController.selectionIndicatorHeight = 0
tabController.selectionIndicatorHeight = 3

tabController.selectionIndicatorColor = #colorLiteral(red: 0.1803921569, green: 0.8, blue: 0.4431372549, alpha: 1)

Expand Down Expand Up @@ -171,7 +173,7 @@ extension ViewController: AZTabBarDelegate{
}

func tabBar(_ tabBar: AZTabBarController, shouldAnimateButtonInteractionAtIndex index: Int) -> Bool {
return true //index != 2
return !(index == 3 || index == 2)
}

func tabBar(_ tabBar: AZTabBarController, didMoveToTabAtIndex index: Int) {
Expand All @@ -193,6 +195,7 @@ extension ViewController: AZTabBarDelegate{
func tabBar(_ tabBar: AZTabBarController, systemSoundIdForButtonAtIndex index: Int) -> SystemSoundID? {
return tabBar.selectedIndex == index ? nil : audioId
}

}

extension ViewController: AZSearchViewDelegate{
Expand Down Expand Up @@ -281,6 +284,8 @@ class ButtonController: UIViewController{
@IBAction func didClickButton(_ sender: UIButton) {
badgeCount += 1

//currentTabBar?.removeAction(atIndex: 2)
currentTabBar?.removeViewController(atIndex: 0)

if let tabBar = currentTabBar{
tabBar.setBadgeText("\(badgeCount)", atIndex: currentIndex)
Expand Down
110 changes: 83 additions & 27 deletions Sources/AZTabBarController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import UIKit
import AVFoundation
import EasyNotificationBadge

public typealias AZTabBarAction = () -> Void
public typealias AZTabBarAction = (() -> Void)

public protocol AZTabBarDelegate: class {

Expand All @@ -32,13 +32,22 @@ public protocol AZTabBarDelegate: class {
func tabBar(_ tabBar: AZTabBarController, shouldLongClickForIndex index: Int)-> Bool


/// Should the tab be switched to the new tab at a given index.
///
/// - Parameters:
/// - tabBar: The current instance of AZTabBarController.
/// - index: The index of the child view controller which the controller is about to change to.
/// - Returns: true to move and false to ignore.
func tabBar(_ tabBar: AZTabBarController, shouldMoveToTabAtIndex index: Int)-> Bool


/// This function is used to enable/disable animation for a certian tab.
///
/// - Parameters:
/// - tabBar: The current instance of AZTabBarController.
/// - index: The index of the tab.
/// - Returns: true if you wish to enable the animation, false otherwise.
func tabBar(_ tabBar: AZTabBarController, shouldAnimateButtonInteractionAtIndex index:Int)->Bool
func tabBar(_ tabBar: AZTabBarController, shouldAnimateButtonInteractionAtIndex index:Int)-> Bool


/// This function is used to play a sound when a certain tab is selected.
Expand All @@ -47,7 +56,7 @@ public protocol AZTabBarDelegate: class {
/// - tabBar: The current instance of the tab bar controller.
/// - index: The index you wish to play sound for.
/// - Returns: The system sound id. if nil is returned nothing will be played.
func tabBar(_ tabBar: AZTabBarController, systemSoundIdForButtonAtIndex index:Int)->SystemSoundID?
func tabBar(_ tabBar: AZTabBarController, systemSoundIdForButtonAtIndex index:Int)-> SystemSoundID?


/// This function is called whenever user taps one of the menu buttons.
Expand Down Expand Up @@ -86,13 +95,15 @@ public protocol AZTabBarDelegate: class {
//The point of this extension is to make the delegate functions optional.
public extension AZTabBarDelegate{

func tabBar(_ tabBar: AZTabBarController, statusBarStyleForIndex index: Int)-> UIStatusBarStyle{ return .default }
func tabBar(_ tabBar: AZTabBarController, statusBarStyleForIndex index: Int)-> UIStatusBarStyle { return .default }

func tabBar(_ tabBar: AZTabBarController, shouldLongClickForIndex index: Int)-> Bool{ return true }
func tabBar(_ tabBar: AZTabBarController, shouldLongClickForIndex index: Int)-> Bool { return true }

func tabBar(_ tabBar: AZTabBarController, shouldAnimateButtonInteractionAtIndex index:Int)->Bool{ return true }
func tabBar(_ tabBar: AZTabBarController, shouldAnimateButtonInteractionAtIndex index:Int)-> Bool { return true }

func tabBar(_ tabBar: AZTabBarController, systemSoundIdForButtonAtIndex index:Int)->SystemSoundID?{return nil}
func tabBar(_ tabBar: AZTabBarController, shouldMoveToTabAtIndex index: Int)-> Bool { return true }

func tabBar(_ tabBar: AZTabBarController, systemSoundIdForButtonAtIndex index:Int)-> SystemSoundID? {return nil}

func tabBar(_ tabBar: AZTabBarController, didSelectTabAtIndex index: Int){}

Expand Down Expand Up @@ -187,7 +198,7 @@ public class AZTabBarController: UIViewController {
}
}


/// The color of the selection indicator.
open var selectionIndicatorColor: UIColor!{
didSet{
self.updateInterfaceIfNeeded()
Expand All @@ -207,6 +218,7 @@ public class AZTabBarController: UIViewController {
}
}

/// When setting this to true, The tab bar will display the icons with their orignal colors instead of template color.
open var ignoreIconColors: Bool = false {
didSet{
updateInterfaceIfNeeded()
Expand All @@ -216,6 +228,7 @@ public class AZTabBarController: UIViewController {
}
}

/// Should the tab bar have animated transitions enabled?
open var animateTabChange: Bool = false

/// The current selected index.
Expand Down Expand Up @@ -502,6 +515,9 @@ public class AZTabBarController: UIViewController {
/// - controller: The view controller which you wish to display at a certain index.
/// - index: The index of the menu.
open func setViewController(_ controller: UIViewController, atIndex index: Int) {

if index >= tabCount { return }

if let currentViewController = self.controllers[index]{
currentViewController.removeFromParentViewController()
}
Expand All @@ -514,6 +530,23 @@ public class AZTabBarController: UIViewController {
}
}


/// Remove view controller at a given index. This won't work if you are trying to remove a view controller at a current index.
///
/// - Parameter index: The index of which you want to remove the view controller
open func removeViewController(atIndex index: Int){
//possible exceptions: IOOBException, removing current view
if index >= tabCount { return }

if selectedIndex == index { return }

if let currentVC = controllers[index] {
currentVC.removeFromParentViewController()
}

controllers[index] = nil
}


/// Change the current menu programatically.
///
Expand Down Expand Up @@ -542,10 +575,24 @@ public class AZTabBarController: UIViewController {
/// - action: A closure which contains the action that will be executed when clicking the menu at a certain index.
/// - index: The index of the menu of which you would like to add an action to.
open func setAction(atIndex index: Int, action: @escaping AZTabBarAction) {

if index >= tabCount { return }

self.actions[(index)] = action
}


/// Remove an action from an index.
///
/// - Parameter index: The index at which you wish to remove the action.
open func removeAction(atIndex index: Int){

if index >= tabCount { return }

self.actions[index] = nil
}


/// Set a badge with a text on a menu at a certain index. In order to remove an existing badge use `nil` for the `text` parameter.
///
/// - Parameters:
Expand Down Expand Up @@ -751,8 +798,13 @@ public class AZTabBarController: UIViewController {
}

private func moveToController(at index:Int,animated:Bool){

if let controller = controllers[index], !(animated && isAnimating) {

if let shouldMove = delegate?.tabBar(self, shouldMoveToTabAtIndex: index), shouldMove == false{
return
}

if buttons == nil{
selectedIndex = index
return
Expand Down Expand Up @@ -811,28 +863,31 @@ public class AZTabBarController: UIViewController {
//self.addChildViewController(controller)
controller.didMove(toParentViewController: self)

if let currentViewControllerView = currentViewControllerView, animated, animateTabChange {
if let currentViewControllerView = currentViewControllerView, animated, animateTabChange {
//animate

let offset: CGFloat = self.view.frame.size.width / 5
//let startX = index > selectedIndex ? self.view.frame.size.width + offset : -offset
let startX = index > selectedIndex ? offset : -offset
controller.view.transform = CGAffineTransform(translationX: startX, y: 0)
controller.view.alpha = 0
let offset: CGFloat = self.view.frame.size.width / 5
//let startX = index > selectedIndex ? self.view.frame.size.width + offset : -offset
let startX = index > selectedIndex ? offset : -offset
controller.view.transform = CGAffineTransform(translationX: startX, y: 0)
controller.view.alpha = 0

UIView.animate(withDuration: 0.2, animations: {
self.isAnimating = true
controller.view.transform = .identity
controller.view.alpha = 1
currentViewControllerView.transform = CGAffineTransform(translationX: -startX, y: 0)
}, completion: { (bool) in
self.isAnimating = false
currentViewControllerView.removeFromSuperview()
})
}
UIView.animate(withDuration: 0.2, animations: {
self.isAnimating = true
controller.view.transform = .identity
controller.view.alpha = 1
currentViewControllerView.transform = CGAffineTransform(translationX: -startX, y: 0)
}, completion: { (bool) in
self.isAnimating = false
currentViewControllerView.removeFromSuperview()
})
self.moveSelectionIndicator(toIndex: index,animated: animated)
}else{
self.moveSelectionIndicator(toIndex: index,animated: false)
}


self.moveSelectionIndicator(toIndex: index,animated:animated)

self.selectedIndex = index
delegate?.tabBar(self, didMoveToTabAtIndex: index)
self.statusBarStyle = delegate?.tabBar(self, statusBarStyleForIndex: index) ?? .default
Expand All @@ -858,7 +913,8 @@ public class AZTabBarController: UIViewController {
}

private func moveSelectionIndicator(toIndex index: Int,animated:Bool){
let constant:CGFloat = (self.buttons[index] as! UIButton).frame.origin.x
//let constant:CGFloat = (self.buttons[index] as! UIButton).frame.origin.x
let constant: CGFloat = ((buttonsContainer.frame.size.width / CGFloat(tabCount)) * CGFloat(index))

self.buttonsContainer.layoutIfNeeded()

Expand Down Expand Up @@ -1271,7 +1327,7 @@ public extension UIViewController{
var current: UIViewController? = parent

repeat{
if current is AZTabBarController{ return current as? AZTabBarController }
if current is AZTabBarController { return current as? AZTabBarController }
current = current?.parent
}while current != nil

Expand Down

0 comments on commit cd810a7

Please sign in to comment.