有人知道如何阻止UIWebView垂直反弹吗?我的意思是,当用户触摸他们的iphone屏幕,向下拖动他们的手指时,web视图在我加载的网页上方显示一个空白点?
我研究了以下可能的解决方案,但没有一个适合我:
http://www.iphonedevsdk.com/forum/iphone-sdk-development/996-turn-off-scrolling-bounces-uiwebview.html
http://forums.macrumors.com/showthread.php?t=619534
我如何停止一个UIScrollView水平反弹?
我很恼火地发现UIWebView不是一个滚动视图,所以我做了一个自定义子类来获得web视图的滚动视图。这个子类包含一个滚动视图,所以你可以自定义你的web视图的行为。这门课的妙语是:
@class CustomWebView : UIWebview
...
- (id) initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
// WebViews are subclass of NSObject and not UIScrollView and therefore don't allow customization.
// However, a UIWebView is a UIScrollViewDelegate, so it must CONTAIN a ScrollView somewhere.
// To use a web view like a scroll view, let's traverse the view hierarchy to find the scroll view inside the web view.
for (UIView* v in self.subviews){
if ([v isKindOfClass:[UIScrollView class]]){
_scrollView = (UIScrollView*)v;
break;
}
}
return self;
}
然后,当你创建一个自定义web视图时,你可以禁用反弹:
customWebView.scrollView.bounces = NO; //(or customWebView.scrollView.alwaysBounceVertically = NO)
这是一个伟大的通用方法,使web视图具有自定义滚动行为。这里有几件事需要注意:
as with any view, you'll also need to override -(id)initWithCoder: if you use it in Interface Builder
when you initially create a web view, its content size is always the same as the size of the view's frame. After you scroll the web, the content size represents the size of the actual web contents inside the view. To get around this, I did something hacky - calling -setContentOffset:CGPointMake(0,1)animated:YES to force an unnoticeable change that will set the proper content size of the web view.
我尝试了一种稍微不同的方法来防止UIWebView对象的滚动和反弹:添加一个手势识别器来覆盖其他手势。
看起来,UIWebView或它的scroller子视图使用它自己的平移手势识别器来检测用户滚动。但根据苹果的文档,有一种合法的方法可以覆盖一个手势识别器。UIGestureRecognizerDelegate协议有一个方法gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: -它允许控制任何碰撞手势识别器的行为。
所以,我所做的是
视图控制器的viewDidLoad方法:
// Install a pan gesture recognizer // We ignore all the touches except the first and try to prevent other pan gestures
// by registering this object as the recognizer's delegate
UIPanGestureRecognizer *recognizer;
recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanFrom:)];
recognizer.delegate = self;
recognizer.maximumNumberOfTouches = 1;
[self.view addGestureRecognizer:recognizer];
self.panGestureFixer = recognizer;
[recognizer release];
然后,手势重写方法:
// Control gestures precedence
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
// Prevent all panning gestures (which do nothing but scroll webViews, something we want to disable in
// the most painless way)
if ([otherGestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]])
{
// Just disable every other pan gesture recognizer right away
otherGestureRecognizer.enabled = FALSE;
}
return NO;
}
当然,这个委托方法在实际应用中可能会更复杂——我们可以选择性地禁用其他识别器,分析otherGestureRecognizer。视图,并根据它是什么视图做出决策。
最后,为了完整起见,我们将方法注册为pan处理程序:
- (void)handlePanFrom:(UIPanGestureRecognizer *)recognizer
{
// do nothing as of yet
}
如果我们只想取消网页视图的滚动和反弹,它可以是空的,或者它可以包含我们自己的代码来实现我们真正想要的平移运动和动画……
到目前为止,我只是在尝试所有这些东西,它似乎或多或少地像我想要的那样工作。不过,我还没有尝试向iStore提交任何应用程序。但我相信到目前为止我还没有使用任何无证件的东西……如果有人有其他发现,请通知我。