Guest User

Untitled

a guest
Jun 20th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. import UIKit
  2.  
  3. enum ScrollDirection {
  4. case left
  5. case right
  6. }
  7.  
  8. class PagerController: UIViewController {
  9.  
  10. var pager: PagerView? {
  11. return view as? PagerView
  12. }
  13.  
  14. var pagesCount = 5 {
  15. didSet {
  16. pager?.pagesCount = min(5, pagesCount)
  17. if pagesCount > 5 {
  18. pager?.mode = .scroll
  19. } else {
  20. pager?.mode = .noScroll
  21. }
  22. }
  23. }
  24.  
  25. var current = 0 {
  26. didSet {
  27. guard let pager = pager, current != oldValue else {return}
  28.  
  29. guard pager.mode != .noScroll else {
  30. pager.current = current
  31. return
  32. }
  33.  
  34. let direction: ScrollDirection = current < oldValue ? .right : .left
  35.  
  36. if direction == .left {
  37. if pager.current == 3, current < pagesCount - 1 {
  38. pager.animate(direction) {
  39. pager.current = self.current < self.pagesCount - 1 ? 3 : 4
  40. }
  41. } else {
  42. pager.current = min(4, pager.current + 1)
  43. }
  44. } else if direction == .right {
  45. if pager.current == 1, current > 0 {
  46. pager.animate(.right) {
  47. pager.current = self.current > 0 ? 1 : 0
  48. }
  49. } else {
  50. pager.current = max(0, pager.current - 1)
  51. }
  52. }
  53. }
  54. }
  55.  
  56. override func viewDidLoad() {
  57. super.viewDidLoad()
  58. }
  59. }
  60.  
  61. class PagerView: UIView {
  62.  
  63. enum PagerMode {
  64. case noScroll
  65. case scroll
  66. case leftScroll
  67. case rightScroll
  68. }
  69.  
  70. var mode: PagerMode = .noScroll {
  71. didSet {
  72. switch mode {
  73. case .leftScroll, .rightScroll:
  74. setNeedsDisplay()
  75. default:
  76. ()
  77. }
  78.  
  79. }
  80. }
  81.  
  82. var pagesCount = 5
  83.  
  84. var current = 0 {
  85. didSet {
  86. setNeedsDisplay()
  87. }
  88. }
  89.  
  90. private let sizes: [[Int]] = [[6,4,3,2,1], [4,6,4,3,2], [3,4,6,4,3], [2,3,4,6,4], [1,2,3,4,6]]
  91.  
  92. private func pointSize(forIndex index: Int) -> Int {
  93. guard mode != .noScroll else {return 6}
  94. guard index > -1, index < 5 else {return 4}
  95. return sizes[current][index]
  96. }
  97.  
  98. override func draw(_ rect: CGRect) {
  99. var path = UIBezierPath()
  100. path.lineWidth = 1
  101.  
  102. for i in -1..<pagesCount+1 {
  103.  
  104. if i == -1, mode != .rightScroll {continue}
  105. if i == pagesCount, mode != .leftScroll {continue}
  106. if i == 0, mode == .leftScroll {continue}
  107. if i == pagesCount - 1, mode == .rightScroll {continue}
  108.  
  109. if i == current {
  110. UIColor.orange.setStroke()
  111. UIColor.orange.setFill()
  112. } else {
  113. UIColor.lightGray.setStroke()
  114. UIColor.lightGray.setFill()
  115. }
  116.  
  117. let size = CGFloat(pointSize(forIndex: i))
  118. path = UIBezierPath(ovalIn: CGRect(x: ((rect.width / 2) - 27) + CGFloat(i*12) + (6 - size)/2, y: (rect.height-size)/2, width: size, height: size))
  119. path.stroke()
  120. path.fill()
  121. }
  122. }
  123.  
  124. func animate(_ direction: ScrollDirection, completion: @escaping () -> Void) {
  125. mode = direction == .left ? .leftScroll : .rightScroll
  126. current = direction == .left ? 4 : 0
  127. UIView.animate(withDuration: 0.3, animations: {
  128. self.transform = CGAffineTransform(translationX: direction == .left ? -12 : 12, y: 0)
  129. }, completion: { _ in
  130. self.transform = .identity
  131. completion()
  132. self.mode = .scroll
  133. })
  134. }
  135. }
Add Comment
Please, Sign In to add comment