-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPullableView.swift
More file actions
99 lines (80 loc) · 3.07 KB
/
Copy pathPullableView.swift
File metadata and controls
99 lines (80 loc) · 3.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
//
// PullableView.swift
// PullView
//
// Created by Daniel Vebman on 12/26/16.
// Copyright © 2016 Brinklet. All rights reserved.
//
import Foundation
import UIKit
/**
A view with elastic properties that can be interacted with by the user.
`isPullable` is set to true by default.
Most applications will call for use as a container.
Do not override `touchesBegan`, `touchesMoved`, or `touchesEnded` for a `PullableView`; doing so will break it.
*/
class PullableView: UIView {
private var initCenter = CGPoint.zero
private var initTouchPos = CGPoint.zero
/**
The reciprocal of the slope of the derivative of the curve describing the displacement.
A higher `pullFactor` allows for less pulling.
`pullFactor` should be greater than 1. The default `pullFactor` is 4.
*/
public var pullFactor: CGFloat = 4
/// Values closer to 1 means less oscillation for the animation. The default damping is 0.5.
public var damping = 0.5
/// If the view can be pulled.
public var isPullable: Bool {
get {
return layer.value(forKey: Keys.isPullable) as? Bool ?? false
}
set(value) {
if value { isUserInteractionEnabled = true }
layer.setValue(value, forKey: Keys.isPullable)
}
}
override init(frame: CGRect) {
super.init(frame: frame)
isPullable = true
}
/**
Initializes and returns a newly allocted `PullView` object with the specified frame rectangle and pullable status.
- Parameter frame: The frame rectangle for the PullView.
- Parameter pullable: Whether or not the PullView is initially pullable.
*/
init(frame: CGRect, pullable: Bool) {
super.init(frame: frame)
isPullable = pullable
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if isPullable {
initCenter = center
initTouchPos = touches.first!.location(in: superview)
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if isPullable {
let loc = touches.first!.location(in: self.superview)
let xOffset = loc.x - initTouchPos.x
let yOffset = loc.y - initTouchPos.y
let xOffsetD = xOffset / pullFactor
let yOffsetD = yOffset / pullFactor
center.x = xOffsetD + initCenter.x
center.y = yOffsetD + initCenter.y
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if isPullable {
UIView.animate(withDuration: 0.25, delay: 0, usingSpringWithDamping: damping, initialSpringVelocity: 0.5, options: UIViewAnimationOptions.allowUserInteraction, animations: {
self.center = self.initCenter
}, completion: nil)
}
}
}
struct Keys {
static let isPullable = "isPullable"
}