CHIPageControlPaprika.swift 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //
  2. // CHIPageControlPaprika.swift.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. import Darwin
  27. open class CHIPageControlPaprika: CHIBasePageControl {
  28. fileprivate var diameter: CGFloat {
  29. return radius * 2
  30. }
  31. fileprivate var elements = [CHILayer]()
  32. fileprivate var frames = [CGRect]()
  33. fileprivate var min: CGRect?
  34. fileprivate var max: CGRect?
  35. required public init?(coder aDecoder: NSCoder) {
  36. super.init(coder: aDecoder)
  37. }
  38. public override init(frame: CGRect) {
  39. super.init(frame: frame)
  40. }
  41. override func updateNumberOfPages(_ count: Int) {
  42. elements.forEach { $0.removeFromSuperlayer() }
  43. elements.forEach() { $0.removeFromSuperlayer() }
  44. elements = [CHILayer]()
  45. elements = (0..<count).map {_ in
  46. let layer = CHILayer()
  47. self.layer.addSublayer(layer)
  48. return layer
  49. }
  50. setNeedsLayout()
  51. self.invalidateIntrinsicContentSize()
  52. }
  53. override open func layoutSubviews() {
  54. super.layoutSubviews()
  55. let floatCount = CGFloat(elements.count)
  56. let x = (self.bounds.size.width - self.diameter*floatCount - self.padding*(floatCount-1))*0.5
  57. let y = (self.bounds.size.height - self.diameter)*0.5
  58. var frame = CGRect(x: x, y: y, width: self.diameter, height: self.diameter)
  59. elements.enumerated().forEach() { index, layer in
  60. layer.backgroundColor = self.tintColor(position: index).withAlphaComponent(self.inactiveTransparency).cgColor
  61. if self.borderWidth > 0 {
  62. layer.borderWidth = self.borderWidth
  63. layer.borderColor = self.tintColor(position: index).cgColor
  64. }
  65. layer.cornerRadius = self.radius
  66. layer.frame = frame
  67. frame.origin.x += self.diameter + self.padding
  68. }
  69. if let active = elements.first {
  70. active.backgroundColor = (self.currentPageTintColor ?? self.tintColor)?.cgColor
  71. active.borderWidth = 0
  72. }
  73. min = elements.first?.frame
  74. max = elements.last?.frame
  75. self.frames = elements.map { $0.frame }
  76. update(for: progress)
  77. }
  78. override func update(for progress: Double) {
  79. guard let min = self.min,
  80. let max = self.max,
  81. numberOfPages > 1 else {
  82. return
  83. }
  84. var progress = progress
  85. if progress < 0 {
  86. progress = 0
  87. }
  88. let total = Double(numberOfPages - 1)
  89. if progress > total {
  90. progress = total
  91. }
  92. let page = Int(progress)
  93. for (index, _) in self.frames.enumerated() {
  94. if page > index {
  95. self.elements[index+1].frame = self.frames[index]
  96. } else if page < index {
  97. self.elements[index].frame = self.frames[index]
  98. }
  99. }
  100. let dist = max.origin.x - min.origin.x
  101. let percent = CGFloat(progress / total)
  102. let offset = dist * percent
  103. guard let active = elements.first else { return }
  104. let x = min.origin.x + offset
  105. let spacePerItem = (dist+diameter+padding)/CGFloat(numberOfPages)
  106. let r = (spacePerItem)/2
  107. let yDirection: CGFloat = page%2 == 1 ? 1 : -1
  108. active.frame.origin.x = x
  109. let xBetweenPoints = x - CGFloat(page)*spacePerItem - min.origin.x
  110. let y = sqrt(pow(Double(r), 2) - pow(fabs(Double(r)-Double(xBetweenPoints)), 2))
  111. active.frame.origin.y = (y.isNaN ? 0 : CGFloat(y)*yDirection) + min.origin.y
  112. let index = page + 1
  113. guard elements.indices.contains(index) else {
  114. return
  115. }
  116. let element = elements[index]
  117. guard frames.indices.contains(page), frames.indices.contains(page + 1) else { return }
  118. let prev = frames[page]
  119. let prevColor = tintColor(position: page)
  120. let current = frames[page + 1]
  121. let currentColor = tintColor(position: page + 1)
  122. let elementTotal: CGFloat = current.origin.x - prev.origin.x
  123. let elementProgress: CGFloat = current.origin.x - active.frame.origin.x
  124. let elementPercent = (elementTotal - elementProgress) / elementTotal
  125. element.borderColor = blend(color1: currentColor, color2: prevColor, progress: elementPercent).cgColor
  126. element.frame = prev
  127. element.frame.origin.x += elementProgress
  128. element.frame.origin.y = 2*min.origin.y - active.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. }