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