CHIPageControlFresno.swift 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //
  2. // CHIPageControlFresno.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 CHIPageControlFresno: CHIBasePageControl {
  27. fileprivate var diameter: CGFloat {
  28. return radius * 2
  29. }
  30. fileprivate var elements = [CHILayer]()
  31. fileprivate var frames = [CGRect]()
  32. fileprivate var min: CGRect?
  33. fileprivate var max: CGRect?
  34. required public init?(coder aDecoder: NSCoder) {
  35. super.init(coder: aDecoder)
  36. }
  37. public override init(frame: CGRect) {
  38. super.init(frame: frame)
  39. }
  40. override func updateNumberOfPages(_ count: Int) {
  41. elements.forEach { $0.removeFromSuperlayer() }
  42. elements = [CHILayer]()
  43. elements = (0..<count).map {_ in
  44. let layer = CHILayer()
  45. self.layer.addSublayer(layer)
  46. return layer
  47. }
  48. setNeedsLayout()
  49. self.invalidateIntrinsicContentSize()
  50. }
  51. override open func layoutSubviews() {
  52. super.layoutSubviews()
  53. let floatCount = CGFloat(elements.count)
  54. let x = (self.bounds.size.width - self.diameter*floatCount - self.padding*(floatCount-1))*0.5
  55. let y = (self.bounds.size.height - self.diameter)*0.5
  56. var frame = CGRect(x: x, y: y, width: self.diameter, height: self.diameter)
  57. elements.enumerated().forEach() { index, layer in
  58. layer.backgroundColor = self.tintColor(position: index).withAlphaComponent(self.inactiveTransparency).cgColor
  59. if self.borderWidth > 0 {
  60. layer.borderWidth = self.borderWidth
  61. layer.borderColor = self.tintColor(position: index).cgColor
  62. }
  63. layer.cornerRadius = self.radius
  64. layer.frame = frame
  65. frame.origin.x += self.diameter + self.padding
  66. }
  67. if let active = elements.first {
  68. active.backgroundColor = (self.currentPageTintColor ?? self.tintColor)?.cgColor
  69. active.borderWidth = 0
  70. }
  71. min = elements.first?.frame
  72. max = elements.last?.frame
  73. self.frames = elements.map { $0.frame }
  74. update(for: progress)
  75. }
  76. override func update(for progress: Double) {
  77. guard let min = self.min,
  78. let max = self.max,
  79. progress >= 0 && progress <= Double(numberOfPages - 1),
  80. numberOfPages > 1 else {
  81. return
  82. }
  83. let total = Double(numberOfPages - 1)
  84. let dist = max.origin.x - min.origin.x
  85. let percent = CGFloat(progress / total)
  86. let page = Int(progress)
  87. for (index, _) in self.frames.enumerated() {
  88. if page > index {
  89. self.elements[index+1].frame = self.frames[index]
  90. } else if page < index {
  91. self.elements[index].frame = self.frames[index]
  92. }
  93. }
  94. let offset = dist * percent
  95. guard let active = elements.first else { return }
  96. active.frame.origin.x = min.origin.x + offset
  97. let index = page + 1
  98. guard elements.indices.contains(index) else {
  99. if frames.indices.contains(page) {
  100. active.frame = frames[page]
  101. }
  102. return
  103. }
  104. let element = elements[index]
  105. guard frames.indices.contains(index - 1), frames.indices.contains(index) else { return }
  106. let prev = frames[index - 1]
  107. let prevColor = tintColor(position: index - 1)
  108. let current = frames[index]
  109. let currentColor = tintColor(position: index)
  110. let elementTotal: CGFloat = current.origin.x - prev.origin.x
  111. let elementProgress: CGFloat = current.origin.x - active.frame.origin.x
  112. let elementPercent = (elementTotal - elementProgress) / elementTotal
  113. // x: input, a: input min, b: input max, c: output min, d: output max
  114. // returns mapped value x from (a,b) to (c, d)
  115. let linearTransform = { (x: CGFloat, a: CGFloat, b: CGFloat, c: CGFloat, d: CGFloat) -> CGFloat in
  116. return (x - a) / (b - a) * (d - c) + c
  117. }
  118. element.frame = prev
  119. element.frame.origin.x = linearTransform(elementPercent, 1.0, 0.0, prev.origin.x, current.origin.x)
  120. element.backgroundColor = blend(color1: currentColor, color2: prevColor, progress: elementPercent).withAlphaComponent(self.inactiveTransparency).cgColor
  121. if elementPercent <= 0.5 {
  122. let originY = linearTransform(elementPercent, 0.0, 0.5, 0, self.radius + self.padding)
  123. element.frame.origin.y = (page % 2 == 0 ? originY : -originY) + min.origin.y
  124. } else {
  125. let originY = linearTransform(elementPercent, 0.5, 1.0, self.radius + self.padding, 0)
  126. element.frame.origin.y = (page % 2 == 0 ? originY : -originY) + min.origin.y
  127. }
  128. active.frame.origin.y = 2*min.origin.y - element.frame.origin.y
  129. }
  130. override open var intrinsicContentSize: CGSize {
  131. return sizeThatFits(CGSize.zero)
  132. }
  133. override open func sizeThatFits(_ size: CGSize) -> CGSize {
  134. return CGSize(width: CGFloat(elements.count) * self.diameter + CGFloat(elements.count - 1) * self.padding,
  135. height: self.diameter)
  136. }
  137. override open func didTouch(gesture: UITapGestureRecognizer) {
  138. let point = gesture.location(ofTouch: 0, in: self)
  139. if var touchIndex = elements.enumerated().first(where: { $0.element.hitTest(point) != nil })?.offset {
  140. let intProgress = Int(progress)
  141. if intProgress > 0 {
  142. if touchIndex == 0 {
  143. touchIndex = intProgress
  144. } else if touchIndex <= intProgress {
  145. touchIndex -= 1
  146. }
  147. }
  148. delegate?.didTouch(pager: self, index: touchIndex)
  149. }
  150. }
  151. }