diff --git a/Example/ViewController.swift b/Example/ViewController.swift index e885324..986538b 100644 --- a/Example/ViewController.swift +++ b/Example/ViewController.swift @@ -140,8 +140,8 @@ class ContainerView: UIView, PagerTab { init(frame: CGRect, tabsView: TabsView) { tabsView.frame = CGRect(x: 0, y: 0, width: frame.width, height: tabsView.frame.height) self.tabsView = tabsView - self.onSelectedTabChanging = { oldTab, newTab in - tabsView.onSelectedTabChanging(oldTab, newTab) + self.onSelectedTabChanging = { oldTab, newTab, origin in + tabsView.onSelectedTabChanging(oldTab, newTab, origin) } super.init(frame: frame) addSubview(tabsView) @@ -163,7 +163,7 @@ class ContainerView: UIView, PagerTab { self.frame = CGRect(x: frame.minX, y: frame.minY, width: frame.width, height: tabsView.frame.height + 50) } - var onSelectedTabChanging: (Int, Int) -> Void = { _, _ in } { + var onSelectedTabChanging: (Int, Int, TabChangeOrigin) -> Void = { _, _, _ in } { didSet { tabsView.onSelectedTabChanging = onSelectedTabChanging } diff --git a/Sources/PagerViewController.swift b/Sources/PagerViewController.swift index a77b447..19432f9 100644 --- a/Sources/PagerViewController.swift +++ b/Sources/PagerViewController.swift @@ -144,7 +144,7 @@ public final class PagerView: UIView { // Setup TabsView. if tabsView != nil { - tabsView!.onSelectedTabChanging = { [weak self] newIndex, oldIndex in + tabsView!.onSelectedTabChanging = { [weak self] newIndex, oldIndex, _ in self?.didSelectTabAtIndex(index: newIndex, previouslySelected: oldIndex, animated: true) } addSubview(tabsView!) diff --git a/Sources/ParallaxPagerView.swift b/Sources/ParallaxPagerView.swift index 532f0bf..7401a51 100644 --- a/Sources/ParallaxPagerView.swift +++ b/Sources/ParallaxPagerView.swift @@ -306,7 +306,7 @@ public final class ParallaxPagerView: UIView { // Setup TabsView. if tabsView != nil { - tabsView!.onSelectedTabChanging = { [weak self] newIndex, oldIndex in + tabsView!.onSelectedTabChanging = { [weak self] newIndex, oldIndex, _ in self?.didSelectTabAtIndex(index: newIndex, previouslySelected: oldIndex, animated: true) } addSubview(tabsView!) diff --git a/Sources/Protocols.swift b/Sources/Protocols.swift index b10955a..00fea47 100644 --- a/Sources/Protocols.swift +++ b/Sources/Protocols.swift @@ -24,7 +24,7 @@ public protocol PagerDelegate { } public protocol PagerTab { - var onSelectedTabChanging: (_ oldTab: Int, _ newTab: Int) -> Void { set get } + var onSelectedTabChanging: (_ oldTab: Int, _ newTab: Int, _ origin: TabChangeOrigin) -> Void { set get } func currentSelectedIndex() -> Int func numberOfTabs() -> Int func setSelectedTab(at index: Int) diff --git a/Sources/Tabs/TabsView.swift b/Sources/Tabs/TabsView.swift index caa37ce..a5ef89a 100644 --- a/Sources/Tabs/TabsView.swift +++ b/Sources/Tabs/TabsView.swift @@ -50,7 +50,7 @@ fileprivate class TabView: UIView { } @objc private func tabClicked() { - tabsView.clickedTab(at: index) + tabsView.clickedTab(at: index, origin: .click) } required init?(coder aDecoder: NSCoder) { @@ -58,9 +58,14 @@ fileprivate class TabView: UIView { } } +public enum TabChangeOrigin { + case click + case other +} + public class TabsView: UIView { - public var onSelectedTabChanging: (_ oldTab: Int, _ newTab: Int) -> Void = { _, _ in } + public var onSelectedTabChanging: (_ oldTab: Int, _ newTab: Int, _ origin: TabChangeOrigin) -> Void = { _, _,_ in } @IBOutlet fileprivate weak var scrollView: UIScrollView! @IBOutlet fileprivate weak var scrollViewWidthConstraint: NSLayoutConstraint! @@ -179,9 +184,9 @@ public class TabsView: UIView { } } - fileprivate func clickedTab(at index: Int) { + fileprivate func clickedTab(at index: Int, origin: TabChangeOrigin) { if index == selectedIndex { return } - onSelectedTabChanging(index, selectedIndex) + onSelectedTabChanging(index, selectedIndex, origin) selectedIndex = index let selectedTab = tabsList[index] @@ -196,9 +201,9 @@ public class TabsView: UIView { } } - func setSelected(index: Int) { + func setSelected(index: Int, origin: TabChangeOrigin) { guard index >= 0, index < tabsList.count else { return } - clickedTab(at: index) + clickedTab(at: index, origin: origin) } } @@ -213,7 +218,7 @@ extension TabsView: PagerTab { } public func setSelectedTab(at index: Int) { - setSelected(index: index) + setSelected(index: index, origin: .other) } }