-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUIViewController+Interactive.m
More file actions
104 lines (77 loc) · 3.21 KB
/
UIViewController+Interactive.m
File metadata and controls
104 lines (77 loc) · 3.21 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
//
// UIViewController+Interactive.m
// woaoo-basketball
//
// Created by Benson Tommy on 2019/7/4.
//
#import "UIViewController+Interactive.h"
static CGFloat minimumVelocitySpeed = 300.f;
static CGFloat animationDuration = 0.2f;
@interface UIViewController ()<UIGestureRecognizerDelegate>
@end
@implementation UIViewController (Interactive)
- (void)presentedToViewController:(UIViewController *)vc animated:(BOOL)animated completion:(void (^)(void))completion {
// 防止底部黑屏出现
self.modalPresentationStyle = UIModalPresentationOverFullScreen;
[vc presentViewController:self animated:animated completion:completion];
}
- (void)addDismissGestureWithDirection:(UIInteractiveDirection)direction {
if (direction == UIInteractiveDirectionHorizontal) {
UIScreenEdgePanGestureRecognizer *edgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(onEdgePan:)];
edgePan.edges = UIRectEdgeLeft;
edgePan.delegate = self;
[self.view addGestureRecognizer:edgePan];
} else {
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(onDrag:)];
pan.delegate = self;
[self.view addGestureRecognizer:pan];
}
}
- (void)onEdgePan:(UIScreenEdgePanGestureRecognizer *)sender {
CGFloat percentThreshold = 0.5f;
CGPoint translation = [sender translationInView:self.view];
CGFloat newY = self.view.y + (translation.x * self.view.height / self.view.width);
if (newY < 0) {
newY = 0;
}
self.view.y = newY;
CGFloat progress = newY / self.view.height;
if (sender.state == UIGestureRecognizerStateEnded) {
CGPoint velocity = [sender velocityInView:self.view];
if (velocity.x > minimumVelocitySpeed || progress > percentThreshold) {
[self dismissViewControllerAnimated:YES completion:nil];
} else {
[UIView animateWithDuration:animationDuration animations:^{
self.view.y = 0;
}];
}
}
[sender setTranslation:CGPointZero inView:self.view];
}
- (void)onDrag:(UIPanGestureRecognizer *)sender {
CGFloat percentThreshold = 0.35f;
CGPoint translation = [sender translationInView:self.view];
CGFloat newY = self.view.y + translation.y;
if (newY < 0) {
newY = 0;
}
self.view.y = newY;
CGFloat progress = newY / self.view.height;
if (sender.state == UIGestureRecognizerStateEnded) {
CGPoint velocity = [sender velocityInView:self.view];
if (velocity.y > minimumVelocitySpeed || progress > percentThreshold) {
[self dismissViewControllerAnimated:YES completion:nil];
} else {
[UIView animateWithDuration:animationDuration animations:^{
self.view.y = 0;
}];
}
}
[sender setTranslation:CGPointZero inView:self.view];
}
#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
// 同时支持横屏和竖屏手势
return YES;
}
@end