@@ -38,6 +38,8 @@ public class HHTabBarView: UIView {
3838
3939 ///For Internal Navigation
4040 private( set) public var referenceUITabBarController = UITabBarController ( )
41+ ///Current Tab Index: Internal Usage Only.
42+ private( set) var currentTabIndex : Int = 0
4143
4244 // MARK: Setters
4345 ///Animation Type. Default: none.
@@ -85,18 +87,18 @@ public class HHTabBarView: UIView {
8587 public func rightToLeft( ) {
8688 let t = CGAffineTransform . init ( scaleX: - 1 , y: - 1 )
8789 self . transform = t
88- _ = self . subviews. map { $0. transform = t }
90+ _ = self . subviews. map { $0. transform = t }
8991 }
9092
9193 ///Reverse the Tabs from LeftToRight [Usage English/Arabic UI]
9294 public func leftToRight( ) {
9395 let t = CGAffineTransform . init ( scaleX: 1 , y: 1 )
9496 self . transform = t
95- _ = self . subviews. map { $0. transform = t }
97+ _ = self . subviews. map { $0. transform = t }
9698 }
9799
98100 ///Completion Handler for Tab Changes
99- public var onTabTapped : ( ( _ tabIndex: Int ) -> Void ) ! = nil
101+ public var onTabTapped : ( ( _ tabIndex: Int , _ isSameTab : Bool , _ controller : Any ? ) -> Void ) ? = nil
100102
101103 // MARK: Init
102104 private override init ( frame: CGRect ) {
@@ -128,38 +130,57 @@ public class HHTabBarView: UIView {
128130 frame = getHHTabBarViewFrame ( )
129131 }
130132
131- //Helper to Select a Particular Tab.
133+ /// Helper to Select a Particular Tab.
132134 public func selectTabAtIndex( withIndex tabIndex: Int ) {
133135 // Tab Selection/Deselection
134- _ = tabBarTabs. map { $0. isSelected = ( $0. tabIndex == tabIndex) ? true : false }
136+ _ = tabBarTabs. map { $0. isSelected = ( $0. tabIndex == tabIndex) ? true : false }
135137 // Apply Tab Changes in UITabBarController
136138 referenceUITabBarController. selectedIndex = tabIndex
137139 // Lock or Unlock the Tabs if requires.
138140 lockUnlockTabs ( )
139- // Disable interaction for the current tab.
140- let currentHHTabButton = tabBarTabs [ tabIndex]
141- currentHHTabButton. isUserInteractionEnabled = false
141+
142+ currentTabIndex = tabIndex
142143 }
143144
144- ///A convenience method to show or hide HHTabBarView.
145+ /// A convenience method to show or hide HHTabBarView.
145146 public func toggleShowOrHide( ) {
146147 self . isHidden = !isHidden
147148 }
148149
149- //Overriding Default Properties
150+ // Overriding Default Properties
151+ /// To hide the HHTabBarView.
150152 override public var isHidden : Bool {
151153 willSet {
152154 self . referenceUITabBarController. tabBar. isHidden = !isHidden
153155 }
154156 }
155157
158+ /// Lock Current tab, if don't want to select the same tab again.
159+ public func lockCurrentTab( ) {
160+ for (index, tab) in tabBarTabs. enumerated ( ) {
161+ if index == currentTabIndex {
162+ if let controllers = self . referenceUITabBarController. viewControllers, let navcon = controllers [ currentTabIndex] as? UINavigationController {
163+ if navcon. viewControllers. count == 1 {
164+ tab. isUserInteractionEnabled = false
165+ break
166+ }
167+ }
168+ }
169+ }
170+ }
171+
172+ /// Unlock all of the tabs at once.
173+ public func unlockAllTabs( ignoreAlreadyLocked: Bool = false ) {
174+ unlockAllTabs ( ) ; if ignoreAlreadyLocked { lockSpecifiedTab ( ) }
175+ }
176+
156177 // MARK: Helpers
157178 private func getHHTabBarViewFrame( ) -> CGRect {
158179 let screentWidth = UIScreen . width
159180 let screentHeight = UIScreen . height
160181 var tabBarHeight = hhTabBarViewHeight
161182
162- if self . tabBarViewPosition == . top {
183+ if tabBarViewPosition == . top {
163184 return CGRect . init ( x: 0.0 , y: tabBarViewTopPositionValue, width: screentWidth, height: tabBarHeight)
164185 } else {
165186 if #available( iOS 11 . 0 , * ) {
@@ -174,11 +195,18 @@ public class HHTabBarView: UIView {
174195 return tabBarTabs. isEmpty ? false : true
175196 }
176197
198+ private func unlockAllTabs( ) {
199+ _ = tabBarTabs. map { $0. isUserInteractionEnabled = true }
200+ }
201+
177202 private func lockUnlockTabs( ) {
178203 //Unlock All Tabs Before Locking.
179- _ = tabBarTabs. map { $0. isUserInteractionEnabled = true }
180-
204+ unlockAllTabs ( )
181205 //Then Lock the provided Tab Indexes.
206+ lockSpecifiedTab ( )
207+ }
208+
209+ private func lockSpecifiedTab( ) {
182210 if !lockTabIndexes. isEmpty {
183211 for index in lockTabIndexes {
184212 let hhTabButton = tabBarTabs [ index]
@@ -199,24 +227,30 @@ public class HHTabBarView: UIView {
199227 addSubview ( hhTabButton)
200228 xPos += width
201229 }
202- self . defaultIndex = 0
230+ defaultIndex = 0
203231 }
204232
205233 //Actions
206234 @objc private func actionTabTapped( tab: HHTabButton ) {
207- if onTabTapped != nil {
235+ let tappedTabIndex = tab. tabIndex
236+ var isSameTab : Bool = false
237+ let controller = referenceUITabBarController. viewControllers ? [ tappedTabIndex]
238+ if currentTabIndex == tappedTabIndex {
239+ isSameTab = true
240+ } else {
241+ isSameTab = false
208242 animateTabBarButton ( tabBarButton: tab)
209243 selectTabAtIndex ( withIndex: tab. tabIndex)
210- onTabTapped ( tab. tabIndex)
211244 }
245+ onTabTapped ? ( tappedTabIndex, isSameTab, controller)
212246 }
213247
214248 //Perform Animation on Tab Changes.
215249 private func animateTabBarButton( tabBarButton: HHTabButton ) {
216250 switch self . tabChangeAnimationType {
217- case . flash: tabBarButton. flash ( ) ; break
218- case . shake: tabBarButton. shake ( ) ; break
219- case . pulsate: tabBarButton. pulsate ( ) ; break
251+ case . flash: tabBarButton. flash ( )
252+ case . shake: tabBarButton. shake ( )
253+ case . pulsate: tabBarButton. pulsate ( )
220254 default : break
221255 }
222256 }
0 commit comments