GradientStartPoint.swift 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // Created by Jake Lin on 12/2/15.
  3. // Copyright © 2015 IBAnimatable. All rights reserved.
  4. //
  5. import UIKit
  6. public enum GradientStartPoint: IBEnum {
  7. case top
  8. case topRight
  9. case right
  10. case bottomRight
  11. case bottom
  12. case bottomLeft
  13. case left
  14. case topLeft
  15. case custom(start: CGPoint, end: CGPoint)
  16. case none
  17. }
  18. extension GradientStartPoint {
  19. public init(string: String?) {
  20. guard let string = string else {
  21. self = .none
  22. return
  23. }
  24. guard let (name, params) = string.extractNameAndParams() else {
  25. self = .none
  26. return
  27. }
  28. switch name {
  29. case "top":
  30. self = .top
  31. case "topright":
  32. self = .topRight
  33. case "right":
  34. self = .right
  35. case "bottomright":
  36. self = .bottomRight
  37. case "bottom":
  38. self = .bottom
  39. case "bottomleft":
  40. self = .bottomLeft
  41. case "left":
  42. self = .left
  43. case "topleft":
  44. self = .topLeft
  45. case "custom":
  46. self = .custom(start: CGPoint(x: params.toDouble(0) ?? 0,
  47. y: params.toDouble(1) ?? 0),
  48. end: CGPoint(x: params.toDouble(2) ?? 0,
  49. y: params.toDouble(3) ?? 0))
  50. default:
  51. self = .none
  52. }
  53. }
  54. }
  55. extension GradientStartPoint {
  56. var startPoint: CGPoint {
  57. switch self {
  58. case .top:
  59. return CGPoint(x: 0.5, y: 0)
  60. case .topRight:
  61. return CGPoint(x: 1, y: 0)
  62. case .right:
  63. return CGPoint(x: 1, y: 0.5)
  64. case .bottomRight:
  65. return CGPoint(x: 1, y: 1)
  66. case .bottom:
  67. return CGPoint(x: 0.5, y: 1)
  68. case .bottomLeft:
  69. return CGPoint(x: 0, y: 1)
  70. case .left:
  71. return CGPoint(x: 0, y: 0.5)
  72. case .topLeft:
  73. return CGPoint(x: 0, y: 0)
  74. case let .custom(start, _):
  75. return start
  76. case .none:
  77. return .zero
  78. }
  79. }
  80. var endPoint: CGPoint {
  81. switch self {
  82. case .top:
  83. return CGPoint(x: 0.5, y: 1)
  84. case .topRight:
  85. return CGPoint(x: 0, y: 1)
  86. case .right:
  87. return CGPoint(x: 0, y: 0.5)
  88. case .bottomRight:
  89. return CGPoint(x: 0, y: 0)
  90. case .bottom:
  91. return CGPoint(x: 0.5, y: 0)
  92. case .bottomLeft:
  93. return CGPoint(x: 1, y: 0)
  94. case .left:
  95. return CGPoint(x: 1, y: 0.5)
  96. case .topLeft:
  97. return CGPoint(x: 1, y: 1)
  98. case let .custom(_, end):
  99. return end
  100. case .none:
  101. return .zero
  102. }
  103. }
  104. var points: (CGPoint, CGPoint) {
  105. return (startPoint, endPoint)
  106. }
  107. }