-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIView
61 lines (45 loc) · 1.49 KB
/
UIView
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
import UIKit
weak var viewTopConstraint: NSLayoutConstraint!
weak var visualEffectView: UIVisualEffectView!
weak var cardView: UIView!
var initialTouchPoint: CGPoint = CGPoint(x: 0,y: 0)
func update(_ translation: CGPoint, origin: CGPoint) {
let offSetY = translation.y
cardView.frame.origin.y = offSetY
let multiplier = 1 - (translation.y / 2000)
visualEffectView.alpha = multiplier
}
func cancel(_ origin: CGPoint) {
let animator = UIViewPropertyAnimator(duration: 0.6, dampingRatio: 0.6) {
visualEffectView.alpha = 1
cardView.alpha = 1.0
cardView.center = origin
}
animator.startAnimation()
}
func finish() {
let animator = UIViewPropertyAnimator(duration: 0.9, dampingRatio: 0.9) {
visualEffectView.effect = nil
}
animator.startAnimation()
}
func panGestureAction(_ sender: UIPanGestureRecognizer) {
let originalCardPosition = cardView.center
let translation = sender.translation(in: cardView)
let origin = sender.location(in: cardView)
switch sender.state {
case .changed:
if translation.y > 50 { update(translation, origin: origin) }
case .ended:
let translation = sender.translation(in: cardView)
if translation.y > 100 {
finish()
} else {
cardView.alpha = 1.0
cancel(originalCardPosition)
}
case .cancelled:
cancel(originalCardPosition)
default: break
}
}