如何在iPhone中检测Web视图页面结束

时间:2012-06-25 06:07:26

标签: iphone ipad uiwebview uiscrollview epub

我是iPhone开发人员的新手,

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

webview.scalesPageToFit=TRUE;

但是当我看到我在横向模式页面中的应用程序正确匹配但是它在滚动时发生,所以我想在滚动结束时添加swiperight gesture以便用户可以导航到下一页,执行{{ 1}}。

有没有告诉webview完成滚动的方法?

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

提前致谢!

修改 写的是负载:

rightswipe

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

    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++;
        }

1 个答案:

答案 0 :(得分:1)

在ios 5.0及更高版本中,您可以访问UIWebView's scrollView属性。如果将scrollView的委托设置为对象,则可以找到它到达顶部或底部的时间。只需确保在更改之前保留对前一个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中的webview。您可以通过实施方法来控制手势识别器是否应该响应:

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

不要忘记为每个手势识别器设置委托,也要检查其他UIGestureRecognizerDelegate方法。视图Controller有一个属性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;
}