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.

91 lines
3.3 KiB

6 years ago
  1. // BarView.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. open class BarView: UIView {
  26. open lazy var selectedBar: UIView = { [unowned self] in
  27. let selectedBar = UIView(frame: CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height))
  28. return selectedBar
  29. }()
  30. var optionsCount = 1 {
  31. willSet(newOptionsCount) {
  32. if newOptionsCount <= selectedIndex {
  33. selectedIndex = optionsCount - 1
  34. }
  35. }
  36. }
  37. var selectedIndex = 0
  38. required public init?(coder aDecoder: NSCoder) {
  39. super.init(coder: aDecoder)
  40. addSubview(selectedBar)
  41. }
  42. override init(frame: CGRect) {
  43. super.init(frame: frame)
  44. addSubview(selectedBar)
  45. }
  46. // MARK: - Helpers
  47. private func updateSelectedBarPosition(with animation: Bool) {
  48. var frame = selectedBar.frame
  49. frame.size.width = self.frame.size.width / CGFloat(optionsCount)
  50. frame.origin.x = frame.size.width * CGFloat(selectedIndex)
  51. if animation {
  52. UIView.animate(withDuration: 0.3, animations: { [weak self] in
  53. self?.selectedBar.frame = frame
  54. })
  55. } else {
  56. selectedBar.frame = frame
  57. }
  58. }
  59. open func moveTo(index: Int, animated: Bool) {
  60. selectedIndex = index
  61. updateSelectedBarPosition(with: animated)
  62. }
  63. open func move(fromIndex: Int, toIndex: Int, progressPercentage: CGFloat) {
  64. selectedIndex = (progressPercentage > 0.5) ? toIndex : fromIndex
  65. var newFrame = selectedBar.frame
  66. newFrame.size.width = frame.size.width / CGFloat(optionsCount)
  67. var fromFrame = newFrame
  68. fromFrame.origin.x = newFrame.size.width * CGFloat(fromIndex)
  69. var toFrame = newFrame
  70. toFrame.origin.x = toFrame.size.width * CGFloat(toIndex)
  71. var targetFrame = fromFrame
  72. targetFrame.origin.x += (toFrame.origin.x - targetFrame.origin.x) * CGFloat(progressPercentage)
  73. selectedBar.frame = targetFrame
  74. }
  75. open override func layoutSubviews() {
  76. super.layoutSubviews()
  77. updateSelectedBarPosition(with: false)
  78. }
  79. }