1

我写了一个自定义 ViewController 有一个如下的 webview:

@interface WebpageController : UIViewController<UIWebViewDelegate> {
    UIWebView* webView;
//....
}

它的构造函数:

-(id)init {

    if (self = [super init]) {
    webView=[[UIWebView alloc]init];
    webView.delegate=self;
        webView.scalesPageToFit = YES;
        webView.multipleTouchEnabled = YES;
//.....
}

我有 3 个功能来放大、缩小和 loadHTMLString 到下面的 webView:

//this is load HTML String function
// param: content --> HTML conten by inside <body> </body>
 -(void) loadWebViewWithContent:(NSString*) content{

    NSString * header = @"<meta name='viewport' content='width=device-width;initial-scale=1.0; user-scalable= yes;'/>";

    NSString * html = [NSString stringWithFormat:@"<html> 
                                                      <head>%@ </head> 
                                                      <body> %@ </body>
                                                   </html>",header,content];
    [webView loadHTMLString:html baseURL:nil];

}

//这些是放大和缩小功能

-(void) zoomIn {
    for (UIScrollView *scroll in [self.webView subviews]) {
        //Set the zoom level.
        if ([scroll respondsToSelector:@selector(setZoomScale:animated:)]) {
            NSLog(@"set ZoomScale work");

            [scroll setZoomScale:2.0f animated:YES];
            NSLog(@"zoomScale of SCROLLVIEW= %f",(scroll.zoomScale));

        }
    }

}
-(void) zoomOut {

    for (UIScrollView *scroll in [self.webView subviews]) {
        //Set the zoom level.

        if ([scroll respondsToSelector:@selector(setZoomScale:animated:)]) {

            NSLog(@"set ZoomScale work");
            [scroll setZoomScale:0.5f animated:YES];
            NSLog(@"zoomScale of SCROLLVIEW= %f",(scroll.zoomScale));

        }

    }

}

在主 UI 中,我设置了当用户点击 ZoomIn 按钮 --> webView zoomIn 通过调用 zoomIn 函数(对于 zoomOut 相同)

当我在 iPhone 5.0 模拟器上测试时一切正常(我使用 xCode 4.2)。您可以捏合以放大、缩小(也可以使用 zoomIn、zoomOut 按钮)。但是当在 Iphone 4.3 和 iPad(4.3 和 5.0)上测试时——> 没有任何事情发生——> LOG 显示:

* *设置缩放比例工作

滚动视图的缩放比例= 1.00; ---> 我也设置了另一个值,但 zoomScale 始终 = 1.00 --> 没有变化**

我的代码有什么问题??我很困惑。请帮助我。预先感谢。

4

2 回答 2

1

这是用于滚动视图缩放方法..

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView 
{
    return contentView;
}

-(void)scrollViewDidZoom:(UIScrollView *)scrllView 
{
    CGRect rectZoom = [self centeredFrameForScrollView:scrllView andUIView:contentView]; 
    contentView.frame = rectZoom; //[self centeredFrameForScrollView:scrllView andUIView:contentView];
}

-(CGRect)centeredFrameForScrollView:(UIScrollView *)scroll andUIView:(UIView *)rView 
{
    CGSize boundsSize = scroll.bounds.size;
    CGRect frameToCenter = rView.frame;

    // center horizontally
    if (frameToCenter.size.width < boundsSize.width) 
    {
        frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
    }
    else 
    {
        frameToCenter.origin.x = 0;
    }

    // center vertically
    if (frameToCenter.size.height < boundsSize.height) 
    {
        frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
    }
    else 
    {
        frameToCenter.origin.y = 0;
    }
    frameToCenter = CGRectMake(frameToCenter.origin.x, frameToCenter.origin.y, frameToCenter.size.width, frameToCenter.size.height);
    return frameToCenter;
}

如果不工作,请告知...

于 2012-08-17T14:46:25.997 回答
0

最好始终使用 self."iVar name"。

@interface WebpageController : UIViewController<UIWebViewDelegate>
@property (strong, nonatomic) UIWebView *webview;
@end
@implementation
  @synthesize webView;
@end

然后像 self.webView 一样引用它。

于 2012-08-10T14:24:39.790 回答