是的,您可以轻松地继承UIWebView
并实现
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
这样:
// ViewController.h
@interface APWebView : UIWebView
@end
@interface APViewController : UIViewController <UIGestureRecognizerDelegate>
{
IBOutlet APWebView *_webview;
}
@end
// ViewController.m
@implementation APWebView
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
UISwipeGestureRecognizer *SwipeRecognizerLeft =
[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeDetected:)];
SwipeRecognizerLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[self addGestureRecognizer:SwipeRecognizerLeft];
UISwipeGestureRecognizer *SwipeRecognizerRight =
[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeDetected:)];
SwipeRecognizerRight.direction = UISwipeGestureRecognizerDirectionRight;
[self addGestureRecognizer:SwipeRecognizerRight];
return self;
}
- (void) SwipeDetected:(UISwipeGestureRecognizer*)gesture
{
if ( gesture.direction == UISwipeGestureRecognizerDirectionLeft ) NSLog(@"LEFT");
else NSLog(@"RIGHT");
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
return YES;
}
@end
@implementation APViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[ _webview loadRequest:
[NSURLRequest requestWithURL:
[NSURL URLWithString:
@"http://www.google.it"]] ];
}
@end
在您的 Xib(或情节提要)上添加 UIWebView 并分配子类:
在您的控制台日志中,您应该看到:
2013-10-16 09:51:33.861 SwipeLR[14936:a0b] LEFT
2013-10-16 09:51:34.377 SwipeLR[14936:a0b] RIGHT
2013-10-16 09:51:35.009 SwipeLR[14936:a0b] LEFT
[...]
希望这可以帮助。