@@ -30,6 +30,8 @@ @interface RootViewController () <UIWebViewDelegate, UIAlertViewDelegate>
30
30
31
31
@property (nonatomic , strong ) UIWebView *webView;
32
32
33
+ @property (nonatomic , weak ) UIPanGestureRecognizer *panGestureRecognizer;
34
+
33
35
- (void )showFailLoadWarning ;
34
36
35
37
- (void )loadStartPage ;
@@ -38,6 +40,8 @@ - (void)failRefresh;
38
40
39
41
- (void )configure ;
40
42
43
+ - (void )panGestureReceived : (UIPanGestureRecognizer *)panGestureRecognizer ;
44
+
41
45
@end
42
46
43
47
@implementation RootViewController
@@ -138,6 +142,12 @@ - (void)viewDidLoad
138
142
self.webView .autoresizingMask = (UIViewAutoresizingFlexibleWidth |
139
143
UIViewAutoresizingFlexibleHeight);
140
144
[self .view addSubview: self .webView];
145
+
146
+ // Init & add pan gesture recognizer.
147
+
148
+ UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc ] initWithTarget: self action: @selector (panGestureReceived: )];
149
+ self.panGestureRecognizer = panGestureRecognizer;
150
+ [self .webView addGestureRecognizer: panGestureRecognizer];
141
151
}
142
152
143
153
- (void )viewDidAppear : (BOOL )animated
@@ -155,6 +165,29 @@ - (void)didReceiveMemoryWarning
155
165
// Dispose of any resources that can be recreated.
156
166
}
157
167
168
+ #pragma mark - Gesture recognizer
169
+
170
+ - (void )panGestureReceived : (UIPanGestureRecognizer *)panGestureRecognizer
171
+ {
172
+ if (panGestureRecognizer == self.panGestureRecognizer ) {
173
+ UIGestureRecognizerState state = panGestureRecognizer.state ;
174
+ CGPoint translation = [panGestureRecognizer translationInView: panGestureRecognizer.view];
175
+ if (state == UIGestureRecognizerStateEnded &&
176
+ ABS (translation.x / translation.y ) > 3 .0f ) {
177
+ // Horizontal swipe.
178
+ if (translation.x > 0 &&
179
+ self.webView .canGoBack ) {
180
+ // Go back.
181
+ [self .webView goBack ];
182
+ } else if (translation.x < 0 &&
183
+ self.webView .canGoForward ) {
184
+ // Go forward.
185
+ [self .webView goForward ];
186
+ }
187
+ }
188
+ }
189
+ }
190
+
158
191
#pragma mark - Web view delegate
159
192
160
193
- (BOOL )webView : (UIWebView *)webView shouldStartLoadWithRequest : (NSURLRequest *)request navigationType : (UIWebViewNavigationType)navigationType
0 commit comments