如何检测UIWebView的缩放比例 [英] How to detect zoom scale of UIWebView
问题描述
我想在用户放大时增加 UIWebView
的高度(如iPhone中的默认邮件应用)。
所以,我尝试了下面的代码在webview中查找scrollview并添加 UIScrollViewDelegate
以使用 scrollViewDidZoom
检测缩放比例以增加此方法的webview高度。
I want to increase height of UIWebView
while user zoom in (like default mail app in iPhone).
So, I have tried the code below to find scrollview in webview and add UIScrollViewDelegate
to use scrollViewDidZoom
for detecting zoom scale to increase height of webview on this method.
// MessageAppDelegate.h
@interface MessageAppDelegate : NSObject <UIApplicationDelegate,UIWebViewDelegate,UIScrollViewDelegate> {
UIWebView *webview;
UIScrollView *scrollview;
}
//MessageAppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
[self.window makeKeyAndVisible];
webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 150, 320, 100)];
webview.delegate = self;
webview.scalesPageToFit = YES;
webview.userInteractionEnabled = YES;
[webview loadHTMLString:@"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=6.0 user-scalable=yes\">test test" baseURL:nil];
[self.window addSubview:webview];
// Find scrollview in webview
for (UIView *view in webview.subviews) {
if ([view isKindOfClass:[UIScrollView class]]) {
// Get UIScrollView object
scrollview = (UIScrollView *) view;
scrollview.delegate = self;
}
}
return YES;
}
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale{
NSLog(@"scale %f",scale);
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView{
NSLog(@"scrollViewDidZoom %f",scrollview.zoomScale);
}
问题是我无法在webview上放大/缩小
但是 NSLog
on scrollViewDisEndZooming
方法显示
The problem is I cannot zoom in/out on the webview
but NSLog
on scrollViewDisEndZooming
method showed
比例1.0
scale 1.0
当我结束缩放
和 scrollViewDidZoom
方法没有显示任何内容。
我想检测webview的缩放比例,以便在 scrollViewDidZoom
上计算它的高度。
when I ended zooming
and scrollViewDidZoom
method didn't show anything.
I want to detect zoom scale of webview for calculating its height on scrollViewDidZoom
.
我是什么做错了吗?
请帮忙。
What I've done wrong? Please help.
提前致谢。
推荐答案
我已经为你的代码做得很好,做了以下修改:
I have done well for your code up,do following modificaitons:
你只需得到scrollview类点,然后就可以得到scrollview.contentSize.width,它可以改变当你放大,但scrollview.zoomScale始终保持1.所以必须使用contensize.width来计算比例。
you just get the scrollview class point ,then can get the scrollview.contentSize.width,which can change when you zoom in out,but scrollview.zoomScale always remains 1.so have to use contensize.width to calculate scale.
@interface dictViewController : UIViewController<UIWebViewDelegate,UITextFieldDelegate,UIGestureRecognizerDelegate,UIScrollViewDelegate>{
UIScrollView *scrollview;
......................
//scrollview.delegate = self;
for (UIView *view in webViewM.subviews) {
if ([view isKindOfClass:[UIScrollView class]]) {
// Get UIScrollView object
scrollview = (UIScrollView *) view;
//scrollview.delegate = self;
}
}
/*
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale{
//NSLog(@"scale zooming %f",scale);
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView{
//NSLog(@"scrollViewDidZoom %f",scrollview.zoomScale);
}*/
- (void)swipeRightAction:(id)ignored
{
NSLog(@"wid=%f", scrollview.contentSize.width);
NSLog(@"zoomscale=%f", scrollview.zoomScale);
}
这篇关于如何检测UIWebView的缩放比例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!