Skip to content

Commit

Permalink
add origin in select tab (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
deepakaroraDH authored Oct 24, 2019
1 parent ae432cc commit 34afe1d
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
6 changes: 3 additions & 3 deletions Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/PagerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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!)
Expand Down
2 changes: 1 addition & 1 deletion Sources/ParallaxPagerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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!)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Protocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 12 additions & 7 deletions Sources/Tabs/TabsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,22 @@ fileprivate class TabView: UIView {
}

@objc private func tabClicked() {
tabsView.clickedTab(at: index)
tabsView.clickedTab(at: index, origin: .click)
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

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!
Expand Down Expand Up @@ -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]

Expand All @@ -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)
}
}

Expand All @@ -213,7 +218,7 @@ extension TabsView: PagerTab {
}

public func setSelectedTab(at index: Int) {
setSelected(index: index)
setSelected(index: index, origin: .other)
}
}

Expand Down

0 comments on commit 34afe1d

Please sign in to comment.