【问题标题】:How to detect web view page end is reached in iPhone如何在 iPhone 中检测到网页视图页面结束
【发布时间】:2012-06-25 06:07:26
【问题描述】:

我是 iPhone 开发人员的新手,

我制作了 epub 阅读器并在我的 webview 中加载了 epub 的每一页,当我在 portrait mode 中看到我的应用程序时,滚动不存在,因为我用过,

webview.scalesPageToFit=TRUE;

但是当我看到我的应用程序在横向模式页面适合但它发生滚动时,所以我想在滚动结束时添加swiperight gesture,以便用户可以导航到下一页,执行rightswipe

是否有任何方法可以告诉 webview 已完成滚动?

_简而言之如何检测UIWebView到达顶部或底部?_

提前致谢!

编辑 写在确实加载:

    swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self  action:@selector(swipeRightAction:)];
    swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
    swipeRight.delegate = (id<UIGestureRecognizerDelegate>)self;
    [_webview addGestureRecognizer:swipeRight];

    swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeftAction:)];
    swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    swipeLeft.delegate = (id<UIGestureRecognizerDelegate>)self;
    [_webview addGestureRecognizer:swipeLeft];

然后在加载每个页面时,我会检查方向,如果它是纵向的,然后再添加两个向上和向下的手势,如果是横向的,然后移除向上和向下的手势。

if([UIApplication sharedApplication].statusBarOrientation==UIInterfaceOrientationPortrait || [UIApplication sharedApplication].statusBarOrientation==UIInterfaceOrientationPortraitUpsideDown) {

        swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self  action:@selector(swipeLeftAction:)];
        swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
        swipeUp.delegate = (id<UIGestureRecognizerDelegate>)self;
        [_webview addGestureRecognizer:swipeUp];

        swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self  action:@selector(swipeRightAction:)];
        swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
        swipeDown.delegate = (id<UIGestureRecognizerDelegate>)self;
        [_webview addGestureRecognizer:swipeDown];

        counterPort++;
        counterLand=0;
      }
    }

    if (counterLand==0) {

        if([UIApplication sharedApplication].statusBarOrientation==UIInterfaceOrientationLandscapeLeft || [UIApplication sharedApplication].statusBarOrientation==UIInterfaceOrientationLandscapeRight) {

            [_webview removeGestureRecognizer:swipeDown];
            [swipeDown release];
            swipeDown=nil;

            [_webview removeGestureRecognizer:swipeUp]; 
            [swipeUp release];
            swipeUp=nil;

            counterPort=0;
            counterLand++;
        }

根据您的建议:

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    float scrollViewHeight = scrollView.frame.size.height;
    float scrollContentSizeHeight = scrollView.contentSize.height;
    float scrollOffset = scrollView.contentOffset.y;

    if (scrollOffset == 0)
    {
        swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self  action:@selector(swipeLeftAction:)];
        swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
        swipeUp.delegate = (id<UIGestureRecognizerDelegate>)self;
        [_webview addGestureRecognizer:swipeUp];  
    }
    else if (scrollOffset + scrollViewHeight == scrollContentSizeHeight)
    {
        swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self  action:@selector(swipeRightAction:)];
        swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
        swipeDown.delegate = (id<UIGestureRecognizerDelegate>)self;
        [_webview addGestureRecognizer:swipeDown];
    }
}

【问题讨论】:

    标签: iphone ipad uiwebview uiscrollview epub


    【解决方案1】:

    在 ios 5.0 及更高版本中,您可以访问 UIWebView's scrollView 属性。如果您将该滚动视图的委托设置为您的对象,您可以找出它何时到达顶部或底部。只需确保在更改之前持有对先前滚动视图委托的引用。

     _defaultDelegate=[webview.scrollView.delegate retain];
     webview.scrollView.delegate=myController;
    

    您的_defaultDelegate 声明将是

    id <UIScrollViewDelegate> _defaultDelegate;
    

    现在,在 myController 的 scrollViewDidScroll: 实现中,您可以确定滚动到了页面的末尾还是开头。

    棘手的是你必须实现其他 UIScrollViewDelegate 方法并传递给默认委托,以便 webview 不会失去其默认行为。所以,你像这样实现

    - (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView {
        [_defaultDelegate scrollViewDidScrollToTop:scrollView];
    }
    

    请参阅此答案以了解 scrollView 是滚动到顶部还是底部: iPhone - knowing if a UIScrollView reached the top or bottom

    编辑:阅读您的代码后,我认为您不应该像那样添加和删除手势识别器。创建 4 个手势识别器,涵盖所有可能的滑动方向,并将其添加到 viewDidLoad 的 web 视图中。您可以通过实现该方法来控制手势识别器是否应响应:

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
    

    不要忘记为每个手势识别器设置委托,并检查其他 UIGestureRecognizerDelegate 方法。视图控制器具有您也可以访问的属性 interfaceOrientation。

    要解决滚动问题,请尝试使用 BOOL iVar 来指示您是否位于代码的底部。

    -(void)scrollViewDidScroll:(UIScrollView *)scrollView{
        float scrollViewHeight = scrollView.frame.size.height;
        float scrollContentSizeHeight = scrollView.contentSize.height;
        float scrollOffset = scrollView.contentOffset.y;
        _atBottom=NO;
        if (scrollOffset == 0)
        {
            swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self  action:@selector(swipeLeftAction:)];
            swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
            swipeUp.delegate = (id<UIGestureRecognizerDelegate>)self;
            [_webview addGestureRecognizer:swipeUp];  
        }
        else if (scrollOffset + scrollViewHeight == scrollContentSizeHeight)
        {
            swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self  action:@selector(swipeRightAction:)];
            swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
            swipeDown.delegate = (id<UIGestureRecognizerDelegate>)self;
            [_webview addGestureRecognizer:swipeDown];
            _atBottom=YES;
        }
    }
    

    并实现 UIGestureRecognizerDelegate 方法

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
        return _atBottom;
    }
    

    【讨论】:

    • 老兄,你是个好人,它工作正常。我尝试了你在链接中给我的答案,但我的问题是当我滚动到底部 webview 理解它是向下手势时,它会自动导航我到新页面。我想导航到一个新页面,但是当用户第二次而不是第一次做向下手势时。
    • @Krunal 你在添加滑动手势识别器吗?你能告诉我你的代码吗?
    • 是的,我正在添加滑动手势识别器,请参阅我的编辑,纵向支持所有手势向上、向下、向左、向右,横向仅支持左右两个手势,但现在我也想要横向的所有 4 个手势。
    • @Krunal 看看我的编辑,看看它是否有助于你让它工作。
    • 你能看看这个问题吗:stackoverflow.com/questions/11187274/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-03
    • 2017-12-30
    • 1970-01-01
    • 2016-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多