iPhone:在UIWebViews之间水平滚动以在URL历史记录中返回和转发

时间:2013-10-16 06:50:21

标签: iphone ios ios6 uiwebview uiscrollview

我目前在MainViewController中有一个Web View,我允许用户左右滑动手势,以便在他们的url历史记录中来回移动(滑动手势调用'goBack'和'goForward'实例方法UIWebView类)。虽然功能齐全,但我希望通过在旧的和最近查看的webViews / webSites之间平滑过渡滑动手势来改善用户的体验(类似于在Scroll View中的页面之间转换的体验)。但是,我不确定继续进行的最佳方式...... Apple特意将此注释放在UIWebView Class Reference Page上:

重要说明:您不应在UIScrollView对象中嵌入UIWebView或UITableView对象。如果这样做,可能会导致意外行为,因为两个对象的触摸事件可能会混淆和错误处理。

如何在我的应用中实现此类功能并改善应用的用户体验?

提前致谢!

1 个答案:

答案 0 :(得分:1)

是, 很容易你可以继承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并分配子类:

enter image description here

在您的控制台日志中,您应该看到:

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
[...]

希望这有帮助。