2

我是iPhone开发人员的新手,

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

webview.scalesPageToFit=TRUE;

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

有什么方法可以告诉 webview 已完成滚动?

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

提前致谢 !

EDIT 写入确实加载:

    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];
    }
}
4

1 回答 1

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 - 知道 UIScrollView 是否到达顶部或底部

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

- (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;
}
于 2012-06-25T07:50:39.337 回答