forked from injap2017/StickerView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StickerView.swift
435 lines (389 loc) · 16.3 KB
/
StickerView.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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//
// StickerView.swift
// StickerView
//
// Created by Edgar Sia on 1/12/18.
// Copyright © 2018 Edgar Sia. All rights reserved.
//
import UIKit
enum StickerViewHandler:Int {
case close = 0
case rotate
case flip
}
enum StickerViewPosition:Int {
case topLeft = 0
case topRight
case bottomLeft
case bottomRight
}
@inline(__always) func CGRectGetCenter(_ rect:CGRect) -> CGPoint {
return CGPoint(x: rect.midX, y: rect.midY)
}
@inline(__always) func CGRectScale(_ rect:CGRect, wScale:CGFloat, hScale:CGFloat) -> CGRect {
return CGRect(x: rect.origin.x, y: rect.origin.y, width: rect.size.width * wScale, height: rect.size.height * hScale)
}
@inline(__always) func CGAffineTransformGetAngle(_ t:CGAffineTransform) -> CGFloat {
return atan2(t.b, t.a)
}
@inline(__always) func CGPointGetDistance(point1:CGPoint, point2:CGPoint) -> CGFloat {
let fx = point2.x - point1.x
let fy = point2.y - point1.y
return sqrt(fx * fx + fy * fy)
}
@objc protocol StickerViewDelegate {
@objc func stickerViewDidBeginMoving(_ stickerView: StickerView)
@objc func stickerViewDidChangeMoving(_ stickerView: StickerView)
@objc func stickerViewDidEndMoving(_ stickerView: StickerView)
@objc func stickerViewDidBeginRotating(_ stickerView: StickerView)
@objc func stickerViewDidChangeRotating(_ stickerView: StickerView)
@objc func stickerViewDidEndRotating(_ stickerView: StickerView)
@objc func stickerViewDidClose(_ stickerView: StickerView)
@objc func stickerViewDidTap(_ stickerView: StickerView)
}
class StickerView: UIView {
var delegate: StickerViewDelegate!
/// The contentView inside the sticker view.
var contentView:UIView!
/// Enable the close handler or not. Default value is YES.
var enableClose:Bool = true {
didSet {
if self.showEditingHandlers {
self.setEnableClose(self.enableClose)
}
}
}
/// Enable the rotate/resize handler or not. Default value is YES.
var enableRotate:Bool = true{
didSet {
if self.showEditingHandlers {
self.setEnableRotate(self.enableRotate)
}
}
}
/// Enable the flip handler or not. Default value is YES.
var enableFlip:Bool = true
/// Show close and rotate/resize handlers or not. Default value is YES.
var showEditingHandlers:Bool = true {
didSet {
if self.showEditingHandlers {
self.setEnableClose(self.enableClose)
self.setEnableRotate(self.enableRotate)
self.setEnableFlip(self.enableFlip)
self.contentView?.layer.borderWidth = 1
}
else {
self.setEnableClose(false)
self.setEnableRotate(false)
self.setEnableFlip(false)
self.contentView?.layer.borderWidth = 0
}
}
}
/// Minimum value for the shorter side while resizing. Default value will be used if not set.
private var _minimumSize:NSInteger = 0
var minimumSize:NSInteger {
set {
_minimumSize = max(newValue, self.defaultMinimumSize)
}
get {
return _minimumSize
}
}
/// Color of the outline border. Default: brown color.
private var _outlineBorderColor:UIColor = .clear
var outlineBorderColor:UIColor {
set {
_outlineBorderColor = newValue
self.contentView?.layer.borderColor = _outlineBorderColor.cgColor
}
get {
return _outlineBorderColor
}
}
/// A convenient property for you to store extra information.
var userInfo:Any?
/**
* Initialize a sticker view. This is the designated initializer.
*
* @param contentView The contentView inside the sticker view.
* You can access it via the `contentView` property.
*
* @return The sticker view.
*/
init(contentView: UIView) {
self.defaultInset = 11
self.defaultMinimumSize = 4 * self.defaultInset
var frame = contentView.frame
frame = CGRect(x: 0, y: 0, width: frame.size.width + CGFloat(self.defaultInset) * 2, height: frame.size.height + CGFloat(self.defaultInset) * 2)
super.init(frame: frame)
self.backgroundColor = UIColor.clear
self.addGestureRecognizer(self.moveGesture)
self.addGestureRecognizer(self.tapGesture)
// Setup content view
self.contentView = contentView
self.contentView.center = CGRectGetCenter(self.bounds)
self.contentView.isUserInteractionEnabled = false
self.contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.contentView.layer.allowsEdgeAntialiasing = true
self.addSubview(self.contentView)
// Setup editing handlers
self.setPosition(.topLeft, forHandler: .close)
self.addSubview(self.closeImageView)
self.setPosition(.bottomRight, forHandler: .rotate)
self.addSubview(self.rotateImageView)
self.setPosition(.bottomLeft, forHandler: .flip)
self.addSubview(self.flipImageView)
self.showEditingHandlers = true
self.enableClose = true
self.enableRotate = true
self.enableFlip = true
self.minimumSize = self.defaultMinimumSize
self.outlineBorderColor = .brown
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
/**
* Use image to customize each editing handler.
* It is your responsibility to set image for every editing handler.
*
* @param image The image to be used.
* @param handler The editing handler.
*/
func setImage(_ image:UIImage, forHandler handler:StickerViewHandler) {
switch handler {
case .close:
self.closeImageView.image = image
case .rotate:
self.rotateImageView.image = image
case .flip:
self.flipImageView.image = image
}
}
/**
* Customize each editing handler's position.
* If not set, default position will be used.
* @note It is your responsibility not to set duplicated position.
*
* @param position The position for the handler.
* @param handler The editing handler.
*/
func setPosition(_ position:StickerViewPosition, forHandler handler:StickerViewHandler) {
let origin = self.contentView.frame.origin
let size = self.contentView.frame.size
var handlerView:UIImageView?
switch handler {
case .close:
handlerView = self.closeImageView
case .rotate:
handlerView = self.rotateImageView
case .flip:
handlerView = self.flipImageView
}
switch position {
case .topLeft:
handlerView?.center = origin
handlerView?.autoresizingMask = [.flexibleRightMargin, .flexibleBottomMargin]
case .topRight:
handlerView?.center = CGPoint(x: origin.x + size.width, y: origin.y)
handlerView?.autoresizingMask = [.flexibleLeftMargin, .flexibleBottomMargin]
case .bottomLeft:
handlerView?.center = CGPoint(x: origin.x, y: origin.y + size.height)
handlerView?.autoresizingMask = [.flexibleRightMargin, .flexibleTopMargin]
case .bottomRight:
handlerView?.center = CGPoint(x: origin.x + size.width, y: origin.y + size.height)
handlerView?.autoresizingMask = [.flexibleLeftMargin, .flexibleTopMargin]
}
handlerView?.tag = position.rawValue
}
/**
* Customize handler's size
*
* @param size Handler's size
*/
func setHandlerSize(_ size:Int) {
if size <= 0 {
return
}
self.defaultInset = NSInteger(round(Float(size) / 2))
self.defaultMinimumSize = 4 * self.defaultInset
self.minimumSize = max(self.minimumSize, self.defaultMinimumSize)
let originalCenter = self.center
let originalTransform = self.transform
var frame = self.contentView.frame
frame = CGRect(x: 0, y: 0, width: frame.size.width + CGFloat(self.defaultInset) * 2, height: frame.size.height + CGFloat(self.defaultInset) * 2)
self.contentView.removeFromSuperview()
self.transform = CGAffineTransform.identity
self.frame = frame
self.contentView.center = CGRectGetCenter(self.bounds)
self.addSubview(self.contentView)
self.sendSubview(toBack: self.contentView)
let handlerFrame = CGRect(x: 0, y: 0, width: self.defaultInset * 2, height: self.defaultInset * 2)
self.closeImageView.frame = handlerFrame
self.setPosition(StickerViewPosition(rawValue: self.closeImageView.tag)!, forHandler: .close)
self.rotateImageView.frame = handlerFrame
self.setPosition(StickerViewPosition(rawValue: self.rotateImageView.tag)!, forHandler: .rotate)
self.flipImageView.frame = handlerFrame
self.setPosition(StickerViewPosition(rawValue: self.flipImageView.tag)!, forHandler: .flip)
self.center = originalCenter
self.transform = originalTransform
}
/**
* Default value
*/
private var defaultInset:NSInteger
private var defaultMinimumSize:NSInteger
/**
* Variables for moving view
*/
private var beginningPoint = CGPoint.zero
private var beginningCenter = CGPoint.zero
/**
* Variables for rotating and resizing view
*/
private var initialBounds = CGRect.zero
private var initialDistance:CGFloat = 0
private var deltaAngle:CGFloat = 0
private lazy var moveGesture = {
return UIPanGestureRecognizer(target: self, action: #selector(handleMoveGesture(_:)))
}()
private lazy var rotateImageView:UIImageView = {
let rotateImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: self.defaultInset * 2, height: self.defaultInset * 2))
rotateImageView.contentMode = UIViewContentMode.scaleAspectFit
rotateImageView.backgroundColor = UIColor.clear
rotateImageView.isUserInteractionEnabled = true
rotateImageView.addGestureRecognizer(self.rotateGesture)
return rotateImageView
}()
private lazy var rotateGesture = {
return UIPanGestureRecognizer(target: self, action: #selector(handleRotateGesture(_:)))
}()
private lazy var closeImageView:UIImageView = {
let closeImageview = UIImageView(frame: CGRect(x: 0, y: 0, width: self.defaultInset * 2, height: self.defaultInset * 2))
closeImageview.contentMode = UIViewContentMode.scaleAspectFit
closeImageview.backgroundColor = UIColor.clear
closeImageview.isUserInteractionEnabled = true
closeImageview.addGestureRecognizer(self.closeGesture)
return closeImageview
}()
private lazy var closeGesture = {
return UITapGestureRecognizer(target: self, action: #selector(handleCloseGesture(_:)))
}()
private lazy var flipImageView:UIImageView = {
let flipImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: self.defaultInset * 2, height: self.defaultInset * 2))
flipImageView.contentMode = UIViewContentMode.scaleAspectFit
flipImageView.backgroundColor = UIColor.clear
flipImageView.isUserInteractionEnabled = true
flipImageView.addGestureRecognizer(self.flipGesture)
return flipImageView
}()
private lazy var flipGesture = {
return UITapGestureRecognizer(target: self, action: #selector(handleFlipGesture(_:)))
}()
private lazy var tapGesture = {
return UITapGestureRecognizer(target: self, action: #selector(handleTapGesture(_:)))
}()
// MARK: - Gesture Handlers
@objc
func handleMoveGesture(_ recognizer: UIPanGestureRecognizer) {
let touchLocation = recognizer.location(in: self.superview)
switch recognizer.state {
case .began:
self.beginningPoint = touchLocation
self.beginningCenter = self.center
if let delegate = self.delegate {
delegate.stickerViewDidBeginMoving(self)
}
case .changed:
self.center = CGPoint(x: self.beginningCenter.x + (touchLocation.x - self.beginningPoint.x), y: self.beginningCenter.y + (touchLocation.y - self.beginningPoint.y))
if let delegate = self.delegate {
delegate.stickerViewDidChangeMoving(self)
}
case .ended:
self.center = CGPoint(x: self.beginningCenter.x + (touchLocation.x - self.beginningPoint.x), y: self.beginningCenter.y + (touchLocation.y - self.beginningPoint.y))
if let delegate = self.delegate {
delegate.stickerViewDidEndMoving(self)
}
default:
break
}
}
@objc
func handleRotateGesture(_ recognizer: UIPanGestureRecognizer) {
let touchLocation = recognizer.location(in: self.superview)
let center = self.center
switch recognizer.state {
case .began:
self.deltaAngle = CGFloat(atan2f(Float(touchLocation.y - center.y), Float(touchLocation.x - center.x))) - CGAffineTransformGetAngle(self.transform)
self.initialBounds = self.bounds
self.initialDistance = CGPointGetDistance(point1: center, point2: touchLocation)
if let delegate = self.delegate {
delegate.stickerViewDidBeginRotating(self)
}
case .changed:
let angle = atan2f(Float(touchLocation.y - center.y), Float(touchLocation.x - center.x))
let angleDiff = Float(self.deltaAngle) - angle
self.transform = CGAffineTransform(rotationAngle: CGFloat(-angleDiff))
var scale = CGPointGetDistance(point1: center, point2: touchLocation) / self.initialDistance
let minimumScale = CGFloat(self.minimumSize) / min(self.initialBounds.size.width, self.initialBounds.size.height)
scale = max(scale, minimumScale)
let scaledBounds = CGRectScale(self.initialBounds, wScale: scale, hScale: scale)
self.bounds = scaledBounds
self.setNeedsDisplay()
if let delegate = self.delegate {
delegate.stickerViewDidChangeRotating(self)
}
case .ended:
if let delegate = self.delegate {
delegate.stickerViewDidEndRotating(self)
}
default:
break
}
}
@objc
func handleCloseGesture(_ recognizer: UITapGestureRecognizer) {
if let delegate = self.delegate {
delegate.stickerViewDidClose(self)
}
self.removeFromSuperview()
}
@objc
func handleFlipGesture(_ recognizer: UITapGestureRecognizer) {
UIView.animate(withDuration: 0.3) {
self.contentView.transform = self.contentView.transform.scaledBy(x: -1, y: 1)
}
}
@objc
func handleTapGesture(_ recognizer: UITapGestureRecognizer) {
if let delegate = self.delegate {
delegate.stickerViewDidTap(self)
}
}
// MARK: - Private Methods
private func setEnableClose(_ enableClose:Bool) {
self.closeImageView.isHidden = !enableClose
self.closeImageView.isUserInteractionEnabled = enableClose
}
private func setEnableRotate(_ enableRotate:Bool) {
self.rotateImageView.isHidden = !enableRotate
self.rotateImageView.isUserInteractionEnabled = enableRotate
}
private func setEnableFlip(_ enableFlip:Bool) {
self.flipImageView.isHidden = !enableFlip
self.flipImageView.isUserInteractionEnabled = enableFlip
}
}
extension StickerView: UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool {
/**
* ref: http://stackoverflow.com/questions/19095165/should-superviews-gesture-cancel-subviews-gesture-in-ios-7/
*
* The `gestureRecognizer` would be either closeGestureRecognizer or rotateGestureRecognizer,
* `otherGestureRecognizer` should work only when `gestureRecognizer` is failed.
* So, we always return YES here.
*/
return true
}
}