- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 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/
我是一名优秀的程序员,十分优秀!