You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
3.9 KiB

6 years ago
  1. // BarPagerTabStripViewController.swift
  2. // XLPagerTabStrip ( https://github.com/xmartlabs/XLPagerTabStrip )
  3. //
  4. // Copyright (c) 2017 Xmartlabs ( http://xmartlabs.com )
  5. //
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. import Foundation
  25. import UIKit
  26. public struct BarPagerTabStripSettings {
  27. public struct Style {
  28. public var barBackgroundColor: UIColor?
  29. public var selectedBarBackgroundColor: UIColor?
  30. public var barHeight: CGFloat = 5 // barHeight is ony set up when the bar is created programatically and not using storyboards or xib files.
  31. }
  32. public var style = Style()
  33. }
  34. open class BarPagerTabStripViewController: PagerTabStripViewController, PagerTabStripDataSource, PagerTabStripIsProgressiveDelegate {
  35. public var settings = BarPagerTabStripSettings()
  36. @IBOutlet weak public var barView: BarView!
  37. required public init?(coder aDecoder: NSCoder) {
  38. super.init(coder: aDecoder)
  39. delegate = self
  40. datasource = self
  41. }
  42. public override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
  43. super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
  44. delegate = self
  45. datasource = self
  46. }
  47. open override func viewDidLoad() {
  48. super.viewDidLoad()
  49. barView = barView ?? {
  50. let barView = BarView(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: settings.style.barHeight))
  51. barView.autoresizingMask = .flexibleWidth
  52. barView.backgroundColor = .black
  53. barView.selectedBar.backgroundColor = .white
  54. return barView
  55. }()
  56. barView.backgroundColor = settings.style.barBackgroundColor ?? barView.backgroundColor
  57. barView.selectedBar.backgroundColor = settings.style.selectedBarBackgroundColor ?? barView.selectedBar.backgroundColor
  58. }
  59. open override func viewWillAppear(_ animated: Bool) {
  60. super.viewWillAppear(animated)
  61. if barView.superview == nil {
  62. view.addSubview(barView)
  63. }
  64. barView.optionsCount = viewControllers.count
  65. barView.moveTo(index: currentIndex, animated: false)
  66. }
  67. open override func reloadPagerTabStripView() {
  68. super.reloadPagerTabStripView()
  69. barView.optionsCount = viewControllers.count
  70. if isViewLoaded {
  71. barView.moveTo(index: currentIndex, animated: false)
  72. }
  73. }
  74. // MARK: - PagerTabStripDelegate
  75. open func updateIndicator(for viewController: PagerTabStripViewController, fromIndex: Int, toIndex: Int, withProgressPercentage progressPercentage: CGFloat, indexWasChanged: Bool) {
  76. barView.move(fromIndex: fromIndex, toIndex: toIndex, progressPercentage: progressPercentage)
  77. }
  78. open func updateIndicator(for viewController: PagerTabStripViewController, fromIndex: Int, toIndex: Int) {
  79. barView.moveTo(index: toIndex, animated: true)
  80. }
  81. }