GradientStartPoint.swift 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. let (name, params) = AnimationType.extractNameAndParams(from: string)
  25. switch name {
  26. case "top":
  27. self = .top
  28. case "topright":
  29. self = .topRight
  30. case "right":
  31. self = .right
  32. case "bottomright":
  33. self = .bottomRight
  34. case "bottom":
  35. self = .bottom
  36. case "bottomleft":
  37. self = .bottomLeft
  38. case "left":
  39. self = .left
  40. case "topleft":
  41. self = .topLeft
  42. case "custom":
  43. self = .custom(start: CGPoint(x: params[safe: 0]?.toDouble() ?? 0,
  44. y: params[safe: 1]?.toDouble() ?? 0),
  45. end: CGPoint(x: params[safe: 2]?.toDouble() ?? 0,
  46. y: params[safe: 3]?.toDouble() ?? 0))
  47. default:
  48. self = .none
  49. }
  50. }
  51. }
  52. extension GradientStartPoint {
  53. var startPoint: CGPoint {
  54. switch self {
  55. case .top:
  56. return CGPoint(x: 0.5, y: 0)
  57. case .topRight:
  58. return CGPoint(x: 1, y: 0)
  59. case .right:
  60. return CGPoint(x: 1, y: 0.5)
  61. case .bottomRight:
  62. return CGPoint(x: 1, y: 1)
  63. case .bottom:
  64. return CGPoint(x: 0.5, y: 1)
  65. case .bottomLeft:
  66. return CGPoint(x: 0, y: 1)
  67. case .left:
  68. return CGPoint(x: 0, y: 0.5)
  69. case .topLeft:
  70. return CGPoint(x: 0, y: 0)
  71. case let .custom(start, _):
  72. return start
  73. case .none:
  74. return .zero
  75. }
  76. }
  77. var endPoint: CGPoint {
  78. switch self {
  79. case .top:
  80. return CGPoint(x: 0.5, y: 1)
  81. case .topRight:
  82. return CGPoint(x: 0, y: 1)
  83. case .right:
  84. return CGPoint(x: 0, y: 0.5)
  85. case .bottomRight:
  86. return CGPoint(x: 0, y: 0)
  87. case .bottom:
  88. return CGPoint(x: 0.5, y: 0)
  89. case .bottomLeft:
  90. return CGPoint(x: 1, y: 0)
  91. case .left:
  92. return CGPoint(x: 1, y: 0.5)
  93. case .topLeft:
  94. return CGPoint(x: 1, y: 1)
  95. case let .custom(_, end):
  96. return end
  97. case .none:
  98. return .zero
  99. }
  100. }
  101. var points: (CGPoint, CGPoint) {
  102. return (startPoint, endPoint)
  103. }
  104. }