forked from SwipeCellKit/SwipeCellKit
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathExtensions.swift
81 lines (67 loc) · 2.55 KB
/
Extensions.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
//
// Extensions.swift
//
// Created by Jeremy Koch
// Copyright © 2017 Jeremy Koch. All rights reserved.
//
import UIKit
extension UITableView {
var swipeCells: [SwipeTableViewCell] {
return visibleCells.compactMap({ $0 as? SwipeTableViewCell })
}
func hideSwipeCell() {
swipeCells.forEach { $0.hideSwipe(animated: true) }
}
}
extension UICollectionView {
var swipeCells: [SwipeCollectionViewCell] {
return visibleCells.compactMap({ $0 as? SwipeCollectionViewCell })
}
func hideSwipeCell() {
swipeCells.forEach { $0.hideSwipe(animated: true) }
}
func setGestureEnabled(_ enabled: Bool) {
gestureRecognizers?.forEach {
guard $0 != panGestureRecognizer else { return }
$0.isEnabled = enabled
}
}
}
extension UIScrollView {
var swipeables: [Swipeable] {
switch self {
case let tableView as UITableView:
return tableView.swipeCells
case let collectionView as UICollectionView:
return collectionView.swipeCells
default:
return []
}
}
func hideSwipeables() {
switch self {
case let tableView as UITableView:
tableView.hideSwipeCell()
case let collectionView as UICollectionView:
collectionView.hideSwipeCell()
default:
return
}
}
}
extension UIPanGestureRecognizer {
func elasticTranslation(in view: UIView?, withLimit limit: CGSize, fromOriginalCenter center: CGPoint, applyingRatio ratio: CGFloat = 0.20) -> CGPoint {
let translation = self.translation(in: view)
guard let sourceView = self.view else {
return translation
}
let updatedCenter = CGPoint(x: center.x + translation.x, y: center.y + translation.y)
let distanceFromCenter = CGSize(width: abs(updatedCenter.x - sourceView.bounds.midX),
height: abs(updatedCenter.y - sourceView.bounds.midY))
let inverseRatio = 1.0 - ratio
let scale: (x: CGFloat, y: CGFloat) = (updatedCenter.x < sourceView.bounds.midX ? -1 : 1, updatedCenter.y < sourceView.bounds.midY ? -1 : 1)
let x = updatedCenter.x - (distanceFromCenter.width > limit.width ? inverseRatio * (distanceFromCenter.width - limit.width) * scale.x : 0)
let y = updatedCenter.y - (distanceFromCenter.height > limit.height ? inverseRatio * (distanceFromCenter.height - limit.height) * scale.y : 0)
return CGPoint(x: x, y: y)
}
}