如何检测UIWebView的缩放比例
我想在用户放大时增加 UIWebView 的高度(就像 iPhone 中的默认邮件应用程序)。 因此,我尝试使用下面的代码在 Web 视图中查找滚动视图,并添加 UIScrollViewDelegate
以使用 scrollViewDidZoom
检测缩放比例,以在此方法上增加 Web 视图的高度。
// 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);
}
问题是我无法放大/缩小网络视图 但 scrollViewDisEndZooming
方法上的 NSLog
显示
规模1.0
当我结束缩放时 和 scrollViewDidZoom
方法没有显示任何内容。 我想检测 webview 的缩放比例以计算其在 scrollViewDidZoom
上的高度。
我做错了什么? 请帮忙。
提前致谢。
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);
}
The problem is I cannot zoom in/out on the webview
but NSLog
on scrollViewDisEndZooming
method showed
scale 1.0
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.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经为你的代码做得很好,做了以下修改:
你只需获取scrollview类点,然后就可以获取scrollview.contentSize.width,当你放大时它可以改变,但scrollview.zoomScale始终保持1.so有使用 contensize.width 来计算比例。
I have done well for your code up,do following modificaitons:
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.
我不认为有一个正确的方法可以从公开的 API 中做到这一点,但是有一个 hacky 方法。
UIWebView
包含一个UIScrollView
,可以给它一个委托。然后,当缩放更改时,您(委托)会收到一个名为scrollViewDidZoom:
。为了找到滚动视图,您可以从 Web 视图中获取
subviews
集合,并迭代查看是哪一个isKindOfClass:[UIScrollView class]
。现在,
UIWebView
也符合UIScrollViewDelegate
。因此,这可能意味着您正在更改 Web 视图的实现,这可能是一个坏主意。好好测试一下。I don't think there's a proper way to do it from the exposed APIs, but there is a hacky way.
A
UIWebView
contains aUIScrollView
which can be given a delegate. Then when zooming changes, you (the delegate) get a callback calledscrollViewDidZoom:
.In order to find the scroll view, you can get the
subviews
collection from the web view and iterate to see which oneisKindOfClass:[UIScrollView class]
.Now,
UIWebView
also conforms toUIScrollViewDelegate
. So that may mean you are changing the web view's implementation, and that could be a Bad Idea. Test well.