我目前在 MainViewController 中有一个 Web View ,我允许用户向左和向右滑动手势,以便在其 url 历史记录中来回移动(滑动手势调用UIWebView 类)。虽然功能齐全,但我希望通过让滑动手势在旧的和最近查看的 webViews/webSites 之间平滑过渡来改善用户的体验(类似于 ScrollView 中页面之间的过渡体验)。但是,我不确定继续进行的最佳方法...Apple 专门将此注释放在其 UIWebView Class Reference Page 上。 :
重要提示:您不应在 UIScrollView 对象中嵌入 UIWebView 或 UITableView 对象。如果这样做,可能会导致意外行为,因为两个对象的触摸事件可能会混合并错误处理。
如何在我的应用中实现此类功能并改善应用的用户体验?
提前致谢!
最佳答案
是的,
您可以轻松地继承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(或 Storyboard)上添加 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
[...]
希望这对您有所帮助。
关于iPhone:在 UIWebView 之间水平滚动以在 URL 历史记录中后退和前进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19396911/