This repository was archived by the owner on Jul 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 380
Expand file tree
/
Copy pathCropOverlay.swift
More file actions
218 lines (177 loc) · 9.07 KB
/
CropOverlay.swift
File metadata and controls
218 lines (177 loc) · 9.07 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//
// CropOverlay.swift
// ALCameraViewController
//
// Created by Alex Littlejohn on 2015/06/30.
// Copyright (c) 2015 zero. All rights reserved.
//
import UIKit
internal class CropOverlay: UIView {
var outerLines = [UIView]()
var horizontalLines = [UIView]()
var verticalLines = [UIView]()
var topLeftCornerLines = [UIView]()
var topRightCornerLines = [UIView]()
var bottomLeftCornerLines = [UIView]()
var bottomRightCornerLines = [UIView]()
var cornerButtons = [UIButton]()
let cornerLineDepth: CGFloat = 3
let cornerLineWidth: CGFloat = 22.5
var cornerButtonWidth: CGFloat {
return self.cornerLineWidth * 2
}
let lineWidth: CGFloat = 1
let outterGapRatio: CGFloat = 1/3
var outterGap: CGFloat {
return self.cornerButtonWidth * self.outterGapRatio
}
var isSquarable: Bool = false
var isResizable: Bool = false
var isMovable: Bool = false
var minimumSize: CGSize = CGSize.zero
internal override init(frame: CGRect) {
super.init(frame: frame)
createLines()
}
internal required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
createLines()
}
override func layoutSubviews() {
for i in 0..<outerLines.count {
let line = outerLines[i]
var lineFrame: CGRect
switch (i) {
case 0:
lineFrame = CGRect(x: outterGap, y: outterGap, width: bounds.width - outterGap * 2, height: lineWidth)
break
case 1:
lineFrame = CGRect(x: bounds.width - lineWidth - outterGap, y: outterGap, width: lineWidth, height: bounds.height - outterGap * 2)
break
case 2:
lineFrame = CGRect(x: outterGap, y: bounds.height - lineWidth - outterGap, width: bounds.width - outterGap * 2, height: lineWidth)
break
case 3:
lineFrame = CGRect(x: outterGap, y: outterGap, width: lineWidth, height: bounds.height - outterGap * 2)
break
default:
lineFrame = CGRect.zero
break
}
line.frame = lineFrame
}
let corners = [topLeftCornerLines, topRightCornerLines, bottomLeftCornerLines, bottomRightCornerLines]
for i in 0..<corners.count {
let corner = corners[i]
var horizontalFrame: CGRect
var verticalFrame: CGRect
var buttonFrame: CGRect
let buttonSize = CGSize(width: cornerButtonWidth, height: cornerButtonWidth)
switch (i) {
case 0: // Top Left
verticalFrame = CGRect(x: outterGap, y: outterGap, width: cornerLineDepth, height: cornerLineWidth)
horizontalFrame = CGRect(x: outterGap, y: outterGap, width: cornerLineWidth, height: cornerLineDepth)
buttonFrame = CGRect(origin: CGPoint(x: 0, y: 0), size: buttonSize)
case 1: // Top Right
verticalFrame = CGRect(x: bounds.width - cornerLineDepth - outterGap, y: outterGap, width: cornerLineDepth, height: cornerLineWidth)
horizontalFrame = CGRect(x: bounds.width - cornerLineWidth - outterGap, y: outterGap, width: cornerLineWidth, height: cornerLineDepth)
buttonFrame = CGRect(origin: CGPoint(x: bounds.width - cornerButtonWidth, y: 0), size: buttonSize)
case 2: // Bottom Left
verticalFrame = CGRect(x: outterGap, y: bounds.height - cornerLineWidth - outterGap, width: cornerLineDepth, height: cornerLineWidth)
horizontalFrame = CGRect(x: outterGap, y: bounds.height - cornerLineDepth - outterGap, width: cornerLineWidth, height: cornerLineDepth)
buttonFrame = CGRect(origin: CGPoint(x: 0, y: bounds.height - cornerButtonWidth), size: buttonSize)
case 3: // Bottom Right
verticalFrame = CGRect(x: bounds.width - cornerLineDepth - outterGap, y: bounds.height - cornerLineWidth - outterGap, width: cornerLineDepth, height: cornerLineWidth)
horizontalFrame = CGRect(x: bounds.width - cornerLineWidth - outterGap, y: bounds.height - cornerLineDepth - outterGap, width: cornerLineWidth, height: cornerLineDepth)
buttonFrame = CGRect(origin: CGPoint(x: bounds.width - cornerButtonWidth, y: bounds.height - cornerButtonWidth), size: buttonSize)
default:
verticalFrame = CGRect.zero
horizontalFrame = CGRect.zero
buttonFrame = CGRect.zero
}
corner[0].frame = verticalFrame
corner[1].frame = horizontalFrame
cornerButtons[i].frame = buttonFrame
}
let lineThickness = lineWidth / UIScreen.main.scale
let vPadding = (bounds.height - outterGap * 2 - (lineThickness * CGFloat(horizontalLines.count))) / CGFloat(horizontalLines.count + 1)
let hPadding = (bounds.width - outterGap * 2 - (lineThickness * CGFloat(verticalLines.count))) / CGFloat(verticalLines.count + 1)
for i in 0..<horizontalLines.count {
let hLine = horizontalLines[i]
let vLine = verticalLines[i]
let vSpacing = (vPadding * CGFloat(i + 1)) + (lineThickness * CGFloat(i))
let hSpacing = (hPadding * CGFloat(i + 1)) + (lineThickness * CGFloat(i))
hLine.frame = CGRect(x: outterGap, y: vSpacing + outterGap, width: bounds.width - outterGap * 2, height: lineThickness)
vLine.frame = CGRect(x: hSpacing + outterGap, y: outterGap, width: lineThickness, height: bounds.height - outterGap * 2)
}
}
func createLines() {
outerLines = [createLine(), createLine(), createLine(), createLine()]
horizontalLines = [createLine(), createLine()]
verticalLines = [createLine(), createLine()]
topLeftCornerLines = [createLine(), createLine()]
topRightCornerLines = [createLine(), createLine()]
bottomLeftCornerLines = [createLine(), createLine()]
bottomRightCornerLines = [createLine(), createLine()]
cornerButtons = [createButton(), createButton(), createButton(), createButton()]
let dragGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(moveCropOverlay))
addGestureRecognizer(dragGestureRecognizer)
}
func createLine() -> UIView {
let line = UIView()
line.backgroundColor = UIColor.white
addSubview(line)
return line
}
func createButton() -> UIButton {
let button = UIButton()
button.backgroundColor = UIColor.clear
let dragGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(moveCropOverlay))
button.addGestureRecognizer(dragGestureRecognizer)
addSubview(button)
return button
}
@objc func moveCropOverlay(gestureRecognizer: UIPanGestureRecognizer) {
if isResizable, let button = gestureRecognizer.view as? UIButton {
if gestureRecognizer.state == .began || gestureRecognizer.state == .changed {
let translation = gestureRecognizer.translation(in: self)
var newFrame: CGRect
switch button {
case cornerButtons[0]: // Top Left
newFrame = CGRect(x: frame.origin.x + translation.x, y: frame.origin.y + translation.y, width: frame.size.width - translation.x, height: frame.size.height - translation.y)
case cornerButtons[1]: // Top Right
newFrame = CGRect(x: frame.origin.x, y: frame.origin.y + translation.y, width: frame.size.width + translation.x, height: frame.size.height - translation.y)
case cornerButtons[2]: // Bottom Left
newFrame = CGRect(x: frame.origin.x + translation.x, y: frame.origin.y, width: frame.size.width - translation.x, height: frame.size.height + translation.y)
case cornerButtons[3]: // Bottom Right
newFrame = CGRect(x: frame.origin.x, y: frame.origin.y, width: frame.size.width + translation.x, height: frame.size.height + translation.y)
default:
newFrame = CGRect.zero
}
var minimumFrame = CGRect(x: newFrame.origin.x, y: newFrame.origin.y, width: max(newFrame.size.width, minimumSize.width + 2 * outterGap), height: max(newFrame.size.height, minimumSize.height + 2 * outterGap))
if isSquarable {
minimumFrame = CGRect(x: newFrame.origin.x, y: newFrame.origin.y, width: max(minimumFrame.size.height, minimumFrame.size.width), height: max(minimumFrame.size.height, minimumFrame.size.width))
}
frame = minimumFrame
layoutSubviews()
gestureRecognizer.setTranslation(CGPoint.zero, in: self)
}
} else if isMovable {
if gestureRecognizer.state == .began || gestureRecognizer.state == .changed {
let translation = gestureRecognizer.translation(in: self)
gestureRecognizer.view!.center = CGPoint(x: gestureRecognizer.view!.center.x + translation.x, y: gestureRecognizer.view!.center.y + translation.y)
gestureRecognizer.setTranslation(CGPoint(x: 0, y: 0), in: self)
}
}
}
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
let view = super.hitTest(point, with: event)
if !isMovable && isResizable && view != nil {
let isButton = cornerButtons.reduce(false) { $1.hitTest(convert(point, to: $1), with: event) != nil || $0 }
if !isButton {
return nil
}
}
return view
}
}