CHIPageControlChimayo.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // CHIPageControlChimayo.swift
  3. // CHIPageControl ( https://github.com/ChiliLabs/CHIPageControl )
  4. //
  5. // Copyright (c) 2017 Chili ( http://chi.lv )
  6. //
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in
  16. // all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. // THE SOFTWARE.
  25. import UIKit
  26. open class CHIPageControlChimayo: CHIBasePageControl {
  27. fileprivate var diameter: CGFloat {
  28. return radius * 2
  29. }
  30. fileprivate var inactive = [CHILayer]()
  31. required public init?(coder aDecoder: NSCoder) {
  32. super.init(coder: aDecoder)
  33. }
  34. public override init(frame: CGRect) {
  35. super.init(frame: frame)
  36. }
  37. override open func awakeFromNib() {
  38. super.awakeFromNib()
  39. self.update(for: self.progress)
  40. }
  41. override func updateNumberOfPages(_ count: Int) {
  42. inactive.forEach { $0.removeFromSuperlayer() }
  43. inactive = [CHILayer]()
  44. inactive = (0..<count).map {_ in
  45. let layer = CHILayer()
  46. self.layer.addSublayer(layer)
  47. return layer
  48. }
  49. setNeedsLayout()
  50. self.invalidateIntrinsicContentSize()
  51. }
  52. override open func layoutSubviews() {
  53. super.layoutSubviews()
  54. let floatCount = CGFloat(inactive.count)
  55. let x = (self.bounds.size.width - self.diameter*floatCount - self.padding*(floatCount-1))*0.5
  56. let y = (self.bounds.size.height - self.diameter)*0.5
  57. var frame = CGRect(x: x, y: y, width: self.diameter, height: self.diameter)
  58. inactive.enumerated().forEach() { index, layer in
  59. layer.cornerRadius = self.radius
  60. layer.frame = frame
  61. frame.origin.x += self.diameter + self.padding
  62. layer.backgroundColor = self.tintColor(position: index).cgColor
  63. }
  64. update(for: progress)
  65. }
  66. override func update(for progress: Double) {
  67. guard progress >= 0 && progress <= Double(numberOfPages - 1),
  68. numberOfPages > 1 else { return }
  69. let rect = CGRect(x: 0, y: 0, width: self.diameter, height: self.diameter).insetBy(dx: 1, dy: 1)
  70. let left = floor(progress)
  71. let page = Int(progress)
  72. let move = rect.width / 2
  73. let rightInset = move * CGFloat(progress - left)
  74. let rightRect = rect.insetBy(dx: rightInset, dy: rightInset)
  75. let leftInset = (1 - CGFloat(progress - left)) * move
  76. let leftRect = rect.insetBy(dx: leftInset, dy: leftInset)
  77. let mask = { (index: Int, layer: CHILayer) in
  78. let mask = CAShapeLayer()
  79. mask.fillRule = CAShapeLayerFillRule.evenOdd
  80. let bounds = UIBezierPath(rect: layer.bounds)
  81. switch index {
  82. case page:
  83. bounds.append(UIBezierPath(ovalIn: leftRect))
  84. case page + 1:
  85. bounds.append(UIBezierPath(ovalIn: rightRect))
  86. default:
  87. bounds.append(UIBezierPath(ovalIn: rect))
  88. }
  89. mask.path = bounds.cgPath
  90. mask.frame = layer.bounds
  91. layer.mask = mask
  92. }
  93. for (index, layer) in inactive.enumerated() {
  94. mask(index, layer)
  95. }
  96. }
  97. override open var intrinsicContentSize: CGSize {
  98. return sizeThatFits(CGSize.zero)
  99. }
  100. override open func sizeThatFits(_ size: CGSize) -> CGSize {
  101. return CGSize(width: CGFloat(inactive.count) * self.diameter + CGFloat(inactive.count - 1) * self.padding,
  102. height: self.diameter)
  103. }
  104. override open func didTouch(gesture: UITapGestureRecognizer) {
  105. let point = gesture.location(ofTouch: 0, in: self)
  106. if let touchIndex = inactive.enumerated().first(where: { $0.element.hitTest(point) != nil })?.offset {
  107. delegate?.didTouch(pager: self, index: touchIndex)
  108. }
  109. }
  110. }