gpt4 book ai didi

iphone - 如何在 iPhone 中检测网页 View 页面是否已到达

转载 作者:行者123 更新时间:2023-12-03 21:16:52 26 4
gpt4 key购买 nike

我是 iPhone 开发人员的新手,

我制作了 epub 阅读器并在我的 webview 中加载了 epub 的每个页面,当我在纵向模式 下看到我的应用程序时,滚动不存在,因为我已经使用过,

webview.scalesPageToFit=TRUE;

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

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

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

提前致谢!

编辑写在did load中:

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

最佳答案

在ios 5.0及更高版本中,您可以访问UIWebView的 scrollView属性。如果您将该 ScrollView 的委托(delegate)设置为您的对象,您可以找出它何时到达顶部或底部。只需确保在更改之前保留对先前 ScrollView 委托(delegate)的引用即可。

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

您的 _defaultDelegate 声明将为

id <UIScrollViewDelegate> _defaultDelegate;

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

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

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

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

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

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

不要忘记为每个手势识别器设置委托(delegate),并检查其他 UIGestureRecognizerDelegate 方法。 View 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;
}

关于iphone - 如何在 iPhone 中检测网页 View 页面是否已到达,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11184179/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com