refactor pagerOptions

This commit is contained in:
Martin Barreto 2016-01-28 18:33:38 -03:00
parent 5616059712
commit a088f9fcdd
11 changed files with 78 additions and 42 deletions

View File

@ -68,9 +68,12 @@ class BarExampleViewController: BarPagerTabStripViewController {
override func reloadPagerTabStripView() {
isReload = true
pagerOptions = rand() % 2 == 0 ? pagerOptions.union(.SkipIntermediateViewControllers) : (pagerOptions.remove(.SkipIntermediateViewControllers) ?? pagerOptions)
pagerOptions = rand() % 2 == 0 ? pagerOptions.union(.IsProgressiveIndicator) : (pagerOptions.remove(.IsProgressiveIndicator) ?? pagerOptions)
pagerOptions = rand() % 2 == 0 ? pagerOptions.union(.IsElasticIndicatorLimit) : (pagerOptions.remove(.IsElasticIndicatorLimit) ?? pagerOptions)
if rand() % 2 == 0 {
pagerBehaviour = .Progressive(skipIntermediteViewControllers: rand() % 2 == 0 , elasticIndicatorLimit: rand() % 2 == 0 )
}
else {
pagerBehaviour = .Common(skipIntermediteViewControllers: rand() % 2 == 0)
}
super.reloadPagerTabStripView()
}
}

View File

@ -32,10 +32,6 @@ class ButtonBarExampleViewController: ButtonBarPagerTabStripViewController {
override func viewDidLoad() {
super.viewDidLoad()
if pagerOptions.contains(.IsProgressiveIndicator) {
pagerOptions = pagerOptions.remove(.IsProgressiveIndicator)!
}
buttonBarView.selectedBar.backgroundColor = .orangeColor()
buttonBarView.backgroundColor = UIColor(red: 7/255, green: 185/255, blue: 155/255, alpha: 1)
}
@ -71,8 +67,12 @@ class ButtonBarExampleViewController: ButtonBarPagerTabStripViewController {
override func reloadPagerTabStripView() {
isReload = true
pagerOptions = rand() % 2 == 0 ? pagerOptions.union(.IsProgressiveIndicator) : (pagerOptions.remove(.IsProgressiveIndicator) ?? pagerOptions)
pagerOptions = rand() % 2 == 0 ? pagerOptions.union(.IsElasticIndicatorLimit) : (pagerOptions.remove(.IsElasticIndicatorLimit) ?? pagerOptions)
if rand() % 2 == 0 {
pagerBehaviour = .Progressive(skipIntermediteViewControllers: rand() % 2 == 0 , elasticIndicatorLimit: rand() % 2 == 0 )
}
else {
pagerBehaviour = .Common(skipIntermediteViewControllers: rand() % 2 == 0)
}
super.reloadPagerTabStripView()
}
}

View File

@ -35,10 +35,6 @@ class NavButtonBarExampleViewController: ButtonBarPagerTabStripViewController {
//-
super.viewDidLoad()
if pagerOptions.contains(.IsProgressiveIndicator) {
pagerOptions = pagerOptions.remove(.IsProgressiveIndicator)!
}
buttonBarView.removeFromSuperview()
navigationController?.navigationBar.addSubview(buttonBarView)
@ -92,8 +88,12 @@ class NavButtonBarExampleViewController: ButtonBarPagerTabStripViewController {
override func reloadPagerTabStripView() {
isReload = true
pagerOptions = rand() % 2 == 0 ? pagerOptions.union(.IsProgressiveIndicator) : (pagerOptions.remove(.IsProgressiveIndicator) ?? pagerOptions)
pagerOptions = rand() % 2 == 0 ? pagerOptions.union(.IsElasticIndicatorLimit) : (pagerOptions.remove(.IsElasticIndicatorLimit) ?? pagerOptions)
if rand() % 2 == 0 {
pagerBehaviour = .Progressive(skipIntermediteViewControllers: rand() % 2 == 0 , elasticIndicatorLimit: rand() % 2 == 0 )
}
else {
pagerBehaviour = .Common(skipIntermediteViewControllers: rand() % 2 == 0)
}
super.reloadPagerTabStripView()
}

View File

@ -70,7 +70,7 @@ class ReloadExampleViewController: UIViewController {
return bool ? "YES" : "NO"
}
titleLabel.text = "Progressive = \(stringFromBool(pagerTabStripViewController.pagerOptions.contains(.IsProgressiveIndicator))) ElasticLimit = \(stringFromBool(pagerTabStripViewController.pagerOptions.contains(.IsElasticIndicatorLimit)))"
titleLabel.text = "Progressive = \(stringFromBool(pagerTabStripViewController.pagerBehaviour.isProgressiveIndicator)) ElasticLimit = \(stringFromBool(pagerTabStripViewController.pagerBehaviour.isElasticIndicatorLimit))"
(navigationItem.titleView as? UILabel)?.text = titleLabel.text
navigationItem.titleView?.sizeToFit()

View File

@ -63,7 +63,8 @@ class SegmentedExampleViewController: SegmentedPagerTabStripViewController {
@IBAction func reloadTapped(sender: UIBarButtonItem) {
isReload = true
pagerOptions = rand() % 2 == 0 ? pagerOptions.union(.SkipIntermediateViewControllers) : (pagerOptions.remove(.SkipIntermediateViewControllers) ?? pagerOptions)
pagerBehaviour = .Common(skipIntermediteViewControllers: rand() % 2 == 0)
pagerBehaviour.skipIntermediateViewControllers
reloadPagerTabStripView()
}
}

View File

@ -28,13 +28,6 @@ import XLPagerTabStrip
class TwitterExampleViewController: TwitterPagerTabStripViewController {
var isReload = false
override func viewDidLoad() {
super.viewDidLoad()
pagerOptions = pagerOptions.union(.IsProgressiveIndicator)
pagerOptions = pagerOptions.union(.IsElasticIndicatorLimit)
}
override func childViewControllersForPagerTabStripViewController(pagerTabStripController: PagerTabStripViewController) -> [UIViewController] {
let child_1 = TableChildExampleViewController(style: .Plain, itemInfo: "TableView")

View File

@ -57,6 +57,8 @@ class YoutubeExampleViewController: BaseButtonBarPagerTabStripViewController<You
settings.style.buttonBarLeftContentInset = 20
settings.style.buttonBarRightContentInset = 20
changeCurrentIndexProgressive = { [weak self] (oldCell: YoutubeIconCell?, newCell: YoutubeIconCell?, progressPercentage: CGFloat, changeCurrentIndex: Bool, animated: Bool) -> Void in
guard changeCurrentIndex == true else { return }
oldCell?.iconImage.tintColor = self?.unselectedIconColor

View File

@ -207,7 +207,7 @@ public class BaseButtonBarPagerTabStripViewController<ButtonBarCellType : UIColl
let oldCell = buttonBarView.cellForItemAtIndexPath(NSIndexPath(forItem: currentIndex, inSection: 0)) as! ButtonBarCellType
let newCell = buttonBarView.cellForItemAtIndexPath(NSIndexPath(forItem: indexPath.item, inSection: 0)) as! ButtonBarCellType
if pagerOptions.contains(.IsProgressiveIndicator) {
if pagerBehaviour.isProgressiveIndicator {
if let changeCurrentIndexProgressive = changeCurrentIndexProgressive {
changeCurrentIndexProgressive(oldCell: oldCell, newCell: newCell, progressPercentage: 1, changeCurrentIndex: true, animated: true)
}
@ -235,7 +235,7 @@ public class BaseButtonBarPagerTabStripViewController<ButtonBarCellType : UIColl
configureCell(cell, childInfo: childInfo)
if pagerOptions.contains(.IsProgressiveIndicator) {
if pagerBehaviour.isProgressiveIndicator {
if let changeCurrentIndexProgressive = changeCurrentIndexProgressive {
changeCurrentIndexProgressive(oldCell: currentIndex == indexPath.item ? nil : cell, newCell: currentIndex == indexPath.item ? cell : nil, progressPercentage: 1, changeCurrentIndex: true, animated: false)
}

View File

@ -242,7 +242,7 @@ public class ButtonBarPagerTabStripViewController: PagerTabStripViewController,
let oldCell = buttonBarView.cellForItemAtIndexPath(NSIndexPath(forItem: currentIndex, inSection: 0)) as! ButtonBarViewCell
let newCell = buttonBarView.cellForItemAtIndexPath(NSIndexPath(forItem: indexPath.item, inSection: 0)) as! ButtonBarViewCell
if pagerOptions.contains(.IsProgressiveIndicator) {
if pagerBehaviour.isProgressiveIndicator {
if let changeCurrentIndexProgressive = changeCurrentIndexProgressive {
changeCurrentIndexProgressive(oldCell: oldCell, newCell: newCell, progressPercentage: 1, changeCurrentIndex: true, animated: true)
}
@ -282,7 +282,7 @@ public class ButtonBarPagerTabStripViewController: PagerTabStripViewController,
configureCell(cell, childInfo: childInfo)
if pagerOptions.contains(.IsProgressiveIndicator) {
if pagerBehaviour.isProgressiveIndicator {
if let changeCurrentIndexProgressive = changeCurrentIndexProgressive {
changeCurrentIndexProgressive(oldCell: currentIndex == indexPath.item ? nil : cell, newCell: currentIndex == indexPath.item ? cell : nil, progressPercentage: 1, changeCurrentIndex: true, animated: false)
}

View File

@ -24,18 +24,55 @@
import Foundation
public struct PagerTabStripOptions : OptionSetType {
//public struct PagerTabStripOptions : OptionSetType {
//
// private enum PagerTabStripOption : Int {
// case SkipIntermediateViewControllers = 1, IsProgressiveIndicator = 2, IsElasticIndicatorLimit = 4
// }
//
// public let rawValue: Int
// public init(rawValue: Int) { self.rawValue = rawValue }
// private init(_ option:PagerTabStripOption){ self.rawValue = option.rawValue }
//
// public static let SkipIntermediateViewControllers = PagerTabStripOptions(.SkipIntermediateViewControllers)
// public static let IsProgressiveIndicator = PagerTabStripOptions(.IsProgressiveIndicator)
// public static let IsElasticIndicatorLimit = PagerTabStripOptions(.IsElasticIndicatorLimit)
//
//}
public enum PagerTabStripBehaviour {
case Common(skipIntermediteViewControllers: Bool)
case Progressive(skipIntermediteViewControllers: Bool, elasticIndicatorLimit: Bool)
private enum PagerTabStripOption : Int {
case SkipIntermediateViewControllers = 1, IsProgressiveIndicator = 2, IsElasticIndicatorLimit = 4
public var skipIntermediateViewControllers: Bool {
switch self {
case .Common(let skipIntermediteControllers):
return skipIntermediteControllers
case .Progressive(let skipIntermediteControllers, _):
return skipIntermediteControllers
}
}
public let rawValue: Int
public init(rawValue: Int) { self.rawValue = rawValue }
private init(_ option:PagerTabStripOption){ self.rawValue = option.rawValue }
public static let SkipIntermediateViewControllers = PagerTabStripOptions(.SkipIntermediateViewControllers)
public static let IsProgressiveIndicator = PagerTabStripOptions(.IsProgressiveIndicator)
public static let IsElasticIndicatorLimit = PagerTabStripOptions(.IsElasticIndicatorLimit)
public var isProgressiveIndicator: Bool {
switch self {
case .Common(_):
return false
case .Progressive(_, _):
return true
}
}
public var isElasticIndicatorLimit: Bool {
switch self {
case .Common(_):
return false
case .Progressive(_, let elasticIndicatorLimit):
return elasticIndicatorLimit
}
}
}

View File

@ -62,7 +62,7 @@ public class PagerTabStripViewController: UIViewController, UIScrollViewDelegate
public weak var delegate: PagerTabStripViewControllerDelegate?
public weak var datasource: PagerTabStripViewControllerDataSource?
public var pagerOptions = PagerTabStripOptions.SkipIntermediateViewControllers.union(.IsProgressiveIndicator).union(.IsElasticIndicatorLimit)
public var pagerBehaviour = PagerTabStripBehaviour.Progressive(skipIntermediteViewControllers: true, elasticIndicatorLimit: true)
public private(set) var viewControllers = [UIViewController]()
public private(set) var currentIndex = 0
@ -131,7 +131,7 @@ public class PagerTabStripViewController: UIViewController, UIScrollViewDelegate
currentIndex = index
return
}
if animated && pagerOptions.contains(.SkipIntermediateViewControllers) && abs(currentIndex - index) > 1 {
if animated && pagerBehaviour.skipIntermediateViewControllers && abs(currentIndex - index) > 1 {
var tmpViewControllers = viewControllers
let currentChildVC = viewControllers[currentIndex]
let fromIndex = currentIndex < index ? index - 1 : index + 1
@ -253,7 +253,7 @@ public class PagerTabStripViewController: UIViewController, UIScrollViewDelegate
currentIndex = newCurrentIndex
let changeCurrentIndex = newCurrentIndex != oldCurrentIndex
if let progressiveDeledate = self as? PagerTabStripViewControllerIsProgressiveDelegate where pagerOptions.contains(.IsProgressiveIndicator) {
if let progressiveDeledate = self as? PagerTabStripViewControllerIsProgressiveDelegate where pagerBehaviour.isProgressiveIndicator {
let (fromIndex, toIndex, scrollPercentage) = progressiveIndicatorData(virtualPage)
try! progressiveDeledate.pagerTabStripViewController(self, updateIndicatorFromIndex: fromIndex, toIndex: toIndex, withProgressPercentage: scrollPercentage, indexWasChanged: changeCurrentIndex)
@ -355,7 +355,7 @@ public class PagerTabStripViewController: UIViewController, UIScrollViewDelegate
}
}
}
let scrollPercentage = pagerOptions.contains(.IsElasticIndicatorLimit) ? self.scrollPercentage : ((toIndex < 0 || toIndex >= count) ? 0.0 : self.scrollPercentage)
let scrollPercentage = pagerBehaviour.isElasticIndicatorLimit ? self.scrollPercentage : ((toIndex < 0 || toIndex >= count) ? 0.0 : self.scrollPercentage)
return (fromIndex, toIndex, scrollPercentage)
}