-
Notifications
You must be signed in to change notification settings - Fork 190
/
Position.swift
75 lines (62 loc) · 1.51 KB
/
Position.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
import UIKit
@objc public class Position: NSObject {
public var left: CGFloat = 0.0
public var top: CGFloat = 0.0
public var right: CGFloat {
get {
return 1.0 - left
}
set {
left = 1.0 - newValue
}
}
public var bottom: CGFloat {
get {
return 1.0 - top
}
set {
top = 1.0 - newValue
}
}
public init(left: CGFloat, top: CGFloat) {
super.init()
self.left = left
self.top = top
}
public init(left: CGFloat, bottom: CGFloat) {
super.init()
self.left = left
self.bottom = bottom
}
public init(right: CGFloat, top: CGFloat) {
super.init()
self.right = right
self.top = top
}
public init(right: CGFloat, bottom: CGFloat) {
super.init()
self.right = right
self.bottom = bottom
}
public var positionCopy: Position {
return Position(left: left, top: top)
}
public var horizontalMirror: Position {
return Position(left: right, top: top)
}
public func originInFrame(_ frame: CGRect) -> CGPoint {
return CGPoint(x: xInFrame(frame), y: yInFrame(frame))
}
public func xInFrame(_ frame: CGRect) -> CGFloat {
let margin = frame.width * left
return frame.minX + margin
}
public func yInFrame(_ frame: CGRect) -> CGFloat {
let margin = frame.height * top
return frame.minY + margin
}
public func isEqualTo(position: Position, epsilon: CGFloat = 0.0001) -> Bool {
let dx = left - position.left, dy = top - position.top
return (dx * dx + dy * dy) < epsilon
}
}