-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNavDismissViewController.m
127 lines (90 loc) · 3.32 KB
/
NavDismissViewController.m
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
//
// NavDismissViewController.m
//
// Created by Curcio J Sobrinho on 24/09/14.
//
//
#import "NavDismissViewController.h"
@interface NavDismissViewController (){
UITapGestureRecognizer * tapBehindGesture;
}
@end
@implementation NavDismissViewController
-(BOOL) shouldAutorotate {
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
return YES;
}
return NO;
}
-(NSUInteger)supportedInterfaceOrientations{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
return UIInterfaceOrientationMaskAll;
}
return (UIInterfaceOrientationMaskPortrait);
}
- (void)viewDidLoad
{
[super viewDidLoad];
if ([[UIDevice currentDevice] userInterfaceIdiom] != UIUserInterfaceIdiomPad) {
[[UIApplication sharedApplication] setStatusBarHidden:NO];
}
// if you need to set the size
if (self.mySize.width>0) {
self.preferredContentSize = CGSizeMake(self.mySize.width,self.mySize.height);
}
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
// this is the magic
if(!tapBehindGesture) {
tapBehindGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBehindDetected:)];
[tapBehindGesture setNumberOfTapsRequired:1];
[tapBehindGesture setCancelsTouchesInView:NO]; //So the user can still interact with controls in the modal view
[tapBehindGesture setDelegate:self];
}
UIView *view = [[self.viewControllers lastObject] view];
[view.window addGestureRecognizer:tapBehindGesture];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
// remove the magic
UIView *view = [[self.viewControllers lastObject] view];
[view.window removeGestureRecognizer:tapBehindGesture];
}
- (void)tapBehindDetected:(UITapGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateEnded)
{
UIView *view = [[self.viewControllers lastObject] view];
CGPoint location = [sender locationInView:nil]; //Passing nil gives us coordinates in the window
if (UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
location = CGPointMake(location.y, location.x);
}
//Convert tap location into the local view's coordinate system. If outside, dismiss the view.
if (![view pointInside:[view convertPoint:location fromView:view.window] withEvent:nil])
{
if(view) {
[self dismissViewControllerAnimated:YES completion:nil];
}
}
}
}
#pragma mark - UIGestureRecognizer Delegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
return YES;
}
@end