如何检测UIWebView的缩放比例

发布于 2024-10-26 22:50:18 字数 1817 浏览 1 评论 0原文

我想在用户放大时增加 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放低过去 2024-11-02 22:50:18

我已经为你的代码做得很好,做了以下修改:

你只需获取scrollview类点,然后就可以获取scrollview.contentSize.width,当你放大时它可以改变,但scrollview.zoomScale始终保持1.so有使用 contensize.width 来计算比例。

@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);

}

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.

@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);

}
橘寄 2024-11-02 22:50:18

我不认为有一个正确的方法可以从公开的 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 a UIScrollView which can be given a delegate. Then when zooming changes, you (the delegate) get a callback called scrollViewDidZoom:.

In order to find the scroll view, you can get the subviews collection from the web view and iterate to see which one isKindOfClass:[UIScrollView class].

Now, UIWebView also conforms to UIScrollViewDelegate. So that may mean you are changing the web view's implementation, and that could be a Bad Idea. Test well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文