-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathDynamicModal.swift
More file actions
263 lines (210 loc) · 10.3 KB
/
PathDynamicModal.swift
File metadata and controls
263 lines (210 loc) · 10.3 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
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
//
// PathDynamicModal.swift
// PathDynamicModal-Demo
//
// Created by Ryo Aoyama on 2/9/15.
// Copyright (c) 2015 Ryo Aoyama. All rights reserved.
//
import UIKit
import SAConfettiView
class PathDynamicModal: NSObject, UIGestureRecognizerDelegate {
/* internal properties */
var backgroundColor: UIColor = UIColor.blackColor() {
didSet {
self.backgroundView.backgroundColor = backgroundColor.colorWithAlphaComponent(self.backgroundAlpha)
}
}
var backgroundAlpha: CGFloat = 0.7 {
didSet {
self.backgroundView.backgroundColor = self.backgroundColor.colorWithAlphaComponent(backgroundAlpha)
}
}
var showMagnitude: CGFloat = 250.0
var closeMagnitude: CGFloat = 170.0
var closeByTapBackground: Bool = true
var closeBySwipeBackground: Bool = true
var showedHandler: (() -> Void)?
var closedHandler: (() -> Void)?
/* private properties */
private weak var view: UIView!
private var contentView: UIView! = UIView()
private var backgroundView: ModalRetainView! = ModalRetainView()
private var animator: UIDynamicAnimator!
private var push: UIPushBehavior?
private var tap: UITapGestureRecognizer!
private var pan: UIPanGestureRecognizer!
private var originalTranslatesAutoresizingMaskIntoConstraints: Bool! = true
private var originalCenter: CGPoint! = CGPointZero
/* internal functions */
class func show(modalView view: UIView, inView: UIView, showConfetti: Bool) -> PathDynamicModal {
let modal = PathDynamicModal(showConfetti: showConfetti)
modal.show(modalView: view, inView: inView)
return modal
}
func show(modalView view: UIView, inView: UIView) {
self.view = view
self.view.layer.cornerRadius = 10.0
self.backgroundView.modal = self
self.backgroundView.center = inView.center
self.configureContentView(view: view)
inView.addSubview(self.backgroundView)
self.fadeBackgroundView(fromAlpha: 0, toAlpha: 1.0, completion: nil)
self.applyShowBehaviors()
}
func closeWithLeansLeft() {
self.close(horizontalOffset: -2.0)
}
func closeWithLeansRight() {
self.close(horizontalOffset: 2.0)
}
func closeWithStraight() {
self.close(horizontalOffset: 0)
}
func closeWithLeansRandom() {
let rand = (30.0 - CGFloat(arc4random_uniform(60))) / 10.0
self.close(horizontalOffset: rand)
}
init(showConfetti: Bool) {
super.init()
self.setup(showConfetti)
}
/* private functions */
var confettiView: SAConfettiView?
var showConfetti = false
private func setup(showConfetti: Bool) {
self.animator = UIDynamicAnimator(referenceView: self.backgroundView)
self.tap = UITapGestureRecognizer(target: self, action: #selector(PathDynamicModal.handleBackgroundView(_:)))
self.tap.delegate = self
self.pan = UIPanGestureRecognizer(target: self, action: #selector(PathDynamicModal.panBackgroundView(_:)))
self.backgroundView.frame = UIScreen.mainScreen().bounds
self.backgroundView.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.7)
self.backgroundView.addGestureRecognizer(self.tap)
self.backgroundView.addGestureRecognizer(self.pan)
if showConfetti {
self.confettiView = SAConfettiView(frame: self.backgroundView.bounds)
self.backgroundView.addSubview(confettiView!)
}
}
private func close(horizontalOffset h: CGFloat) {
self.applyCloseBehaviors(horizontalOffset: h)
self.fadeBackgroundView(fromAlpha: 1.0, toAlpha: 0, completion: { (flag) -> () in
self.backgroundView.removeFromSuperview()
self.backgroundView.modal = nil
self.view.center = self.originalCenter
self.view.translatesAutoresizingMaskIntoConstraints = self.originalTranslatesAutoresizingMaskIntoConstraints
self.closedHandler?()
}
)
}
private func applyShowBehaviors() {
self.animator.removeAllBehaviors()
self.push = UIPushBehavior(items: [self.contentView], mode: .Instantaneous)
self.push!.setTargetOffsetFromCenter(UIOffset(horizontal: -1.0, vertical: 0), forItem: self.contentView)
self.push!.pushDirection = CGVectorMake(0, 1.0)
self.push!.magnitude = self.showMagnitude
self.push!.action = {[weak self] in
if let sSelf = self {
if sSelf.contentView.center.y >= sSelf.backgroundView.center.y - 30.0 {
sSelf.fixOnCenter()
}
}
}
self.animator.addBehavior(self.push!)
}
private func applyCloseBehaviors(horizontalOffset h: CGFloat) {
self.animator.removeAllBehaviors()
self.push = UIPushBehavior(items: [self.contentView], mode: .Instantaneous)
self.push!.setTargetOffsetFromCenter(UIOffset(horizontal: h, vertical: 0), forItem: self.contentView)
self.push!.pushDirection = CGVectorMake(0, 1.0)
self.push!.magnitude = self.closeMagnitude
self.animator.addBehavior(self.push!)
}
private func fixOnCenter() {
self.animator.removeAllBehaviors()
UIView.animateWithDuration(0.08, delay: 0, options: .BeginFromCurrentState, animations: { () -> Void in
self.contentView.center.y = self.backgroundView.center.y + 15.0
self.contentView.transform = CGAffineTransformMakeRotation(CGFloat(M_PI / 90.0))
}, completion: { (flag) -> Void in
UIView.animateWithDuration(0.12, delay: 0, options: [.BeginFromCurrentState, .CurveEaseInOut], animations: { () -> Void in
self.contentView.center = self.backgroundView.center
self.contentView.transform = CGAffineTransformIdentity
}, completion: { (flag) -> Void in
self.showedHandler?()
self.confettiView?.startConfetti()
return
})
})
}
private func configureContentView(view view: UIView) {
self.originalCenter = view.center
self.originalTranslatesAutoresizingMaskIntoConstraints = view.translatesAutoresizingMaskIntoConstraints
view.translatesAutoresizingMaskIntoConstraints = true
self.contentView.frame = view.bounds
self.contentView.center = self.backgroundView.center
self.contentView.frame.origin.y = -self.contentView.bounds.height
view.frame.origin = CGPointZero
self.contentView.addSubview(view)
self.backgroundView.addSubview(self.contentView)
}
private func fadeBackgroundView(fromAlpha fromAlpha: CGFloat, toAlpha: CGFloat, completion: (Bool -> ())?) {
UIView.animateWithDuration(0.4, delay: 0, options: .BeginFromCurrentState, animations: { () -> Void in
self.backgroundView.backgroundColor = self.backgroundColor.colorWithAlphaComponent(self.backgroundAlpha * fromAlpha)
self.backgroundView.backgroundColor = self.backgroundColor.colorWithAlphaComponent(self.backgroundAlpha * toAlpha)
}) { (flag) -> Void in
completion?(flag)
return
}
}
private func tapPointXPercentage(pointX pointX: CGFloat) -> CGFloat {
/* return |-1 0 1| */
let width = CGRectGetWidth(self.backgroundView.bounds)
if pointX >= width / 2.0 {
return (2.0 - width / pointX)
}else {
return -(2.0 - width / (width - (pointX + 1)))
}
}
func handleBackgroundView(sender: UITapGestureRecognizer) {
if self.closeByTapBackground {
let locationX = sender.locationInView(self.backgroundView).x
let offsetH: CGFloat = 3.0 * self.tapPointXPercentage(pointX: locationX)
self.close(horizontalOffset: offsetH)
}
}
func panBackgroundView(sender: UIPanGestureRecognizer) {
if self.closeBySwipeBackground {
let location = sender.locationInView(self.backgroundView)
let pan = sender.translationInView(self.backgroundView)
let velocityY = sender.velocityInView(self.backgroundView).y
let isDownGesuture = self.contentView.center.y >= self.backgroundView.center.y
let panYAmountOnBackground = pan.y / CGRectGetHeight(self.backgroundView.bounds)
let angle = (CGFloat(M_PI) / 180.0) * self.tapPointXPercentage(pointX: location.x) * (8.0 * panYAmountOnBackground)
switch sender.state {
case .Changed:
self.contentView.transform = CGAffineTransformMakeRotation(isDownGesuture ? angle : 0)
self.contentView.center.y = self.backgroundView.center.y + (isDownGesuture ? pan.y : pan.y / 10.0)
self.backgroundView.backgroundColor = self.backgroundColor.colorWithAlphaComponent(self.backgroundAlpha * (isDownGesuture ? 1.0 - panYAmountOnBackground : 1.0))
case .Cancelled, .Ended:
if self.contentView.center.y >= CGRectGetHeight(self.backgroundView.bounds)
|| velocityY >= 200.0 {
self.close(horizontalOffset: 0)
}else {
UIView.animateWithDuration(0.3, delay: 0, options: [.BeginFromCurrentState, .CurveEaseOut], animations: { () -> Void in
self.contentView.transform = CGAffineTransformIdentity
self.contentView.center = self.backgroundView.center
self.backgroundView.backgroundColor = self.backgroundColor.colorWithAlphaComponent(self.backgroundAlpha)
}, completion: nil)
}
default:
break
}
}
}
/* delegate functions */
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
return gestureRecognizer.view == touch.view ? true : false
}
}
private class ModalRetainView: UIView {
private var modal: PathDynamicModal?
}