Iphone 以编程方式缩放UIWebView而不使用手势-中心点计算不正确

Iphone 以编程方式缩放UIWebView而不使用手势-中心点计算不正确,iphone,ipad,ios4,uiwebview,uiscrollview,Iphone,Ipad,Ios4,Uiwebview,Uiscrollview,问题 我的iPad应用程序中有一个UIWebView,我需要通过编程进行缩放,但不需要使用手势/点击。应用程序。有一个“+”和“-”按钮,用于按用户定义的增量放大和缩小(这是一个针对视力受损者的应用程序) 这个+/-缩放按钮功能以前在我的应用程序运行时100%正常。在UIScrollView(而不是WebView)中使用UIImageView。ImageView是缩放的实际视图,中心点是为ScrollView的zoomToRect方法计算的 我现在有了一个WebView,我知道它包含一个Scro

问题

我的iPad应用程序中有一个UIWebView,我需要通过编程进行缩放,但不需要使用手势/点击。应用程序。有一个“+”和“-”按钮,用于按用户定义的增量放大和缩小(这是一个针对视力受损者的应用程序)

这个+/-缩放按钮功能以前在我的应用程序运行时100%正常。在UIScrollView(而不是WebView)中使用UIImageView。ImageView是缩放的实际视图,中心点是为ScrollView的zoomToRect方法计算的

我现在有了一个WebView,我知道它包含一个ScrollView作为子视图。我尝试调整以前使用ImageView/ScrollView组合的代码,以缩放WebView的ScrollView,但它不再正确计算zoomToRect:的中心点

发生了什么:

WebView根据缩放级别正确缩放,但中心点总是错误的。由于某些原因,每次屏幕总是在左上角放大

另一个奇怪的问题是,在第一次放大后,您无法在网络视图中滚动屏幕的可见部分。若您尝试这样做,它会显示一些超出当前可见区域边界的内容,但会立即恢复

我尝试过的:

我正在尝试缩放UIWebView的滚动视图

我创建了一个指向ScrollView的指针,并将“self”设置为它的委托。然后我设置了各种变量,如scrSize(要缩放的视图的大小)和ZoomHandler(解释如下):

为了覆盖WebView的默认缩放限制,我将此Javascript注入webViewDidFinishLoad:方法中加载的网页:

function increaseMaxZoomFactor() {
  var element = document.createElement('meta');
  element.name = "viewport";
  element.content = "maximum-scale=10 minimum-scale=1 initial-scale=1 user-scalable=yes width=device-width height=device-height;"
  var head = document.getElementsByTagName('head')[0];
  head.appendChild(element);
}
中心点代码:

此代码用于计算要传递到zoomToRect:的中心点。当我在ScrollView中缩放ImageView时,它100%工作正常

-(IBAction)zoomOut {
    float newScale = [scrollView zoomScale] / ZOOM_STEP;
    if( [scrollView zoomScale] > MINIMUM_SCALE) {
        [self handleZoomWith:newScale andZoomType: FALSE];
    }
}

-(IBAction)zoomIn {
    float newScale = [scrollView zoomScale] * ZOOM_STEP;
    if( [scrollView zoomScale] < MAXIMUM_SCALE){
        [self handleZoomWith:newScale andZoomType: TRUE];
    }
}

-(void)handleZoomWith: (float) newScale andZoomType:(BOOL) isZoomIn {
    CGPoint newOrigin = [zoomHandler getNewOriginFromViewLocation: [scrollView contentOffset] 
                                                        viewSize: scrSize andZoomType: isZoomIn];
    CGRect zoomRect = [self zoomRectForScale:newScale withCenter:newOrigin];
    [scrollView zoomToRect:zoomRect animated:YES];
}

- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center {
    CGRect zoomRect;

    //  At a zoom scale of 1.0, it would be the size of the scrollView's bounds.
    //  As the zoom scale decreases, so more content is visible, the size of the rect grows.
    zoomRect.size.height = [WebView frame].size.height / scale;
    zoomRect.size.width  = [WebView frame].size.width  / scale;

    // Choose an origin so as to get the right center.
    zoomRect.origin.x = center.x / scale;
    zoomRect.origin.y = center.y / scale;

    return zoomRect;
}

/**
 Determine the origin [THIS IS INSIDE ZOOMHANDLER]
 */
-(CGPoint) getNewOriginFromViewLocation: (CGPoint) oldOrigin viewSize: (CGPoint) viewSize andZoomType:(BOOL) isZoomIn {

    // Calculate original center (add the half of the width/height of the screen)
    float oldCenterX = oldOrigin.x + (viewSize.x / 2);
    float oldCenterY = oldOrigin.y + (viewSize.y / 2);

    // Xalculate the new center
    CGPoint newCenter;
    if(isZoomIn) {
        newCenter = CGPointMake(oldCenterX * zoomLevel, oldCenterY * zoomLevel);
    } else {
        newCenter = CGPointMake(oldCenterX / zoomLevel, oldCenterY / zoomLevel);
    }

    // Calculate the new origin (deduct the half of the width/height of the screen)
    float newOriginX = newCenter.x - (viewSize.x / 2);
    float newOriginY = newCenter.y - (viewSize.y / 2);

    return CGPointMake(newOriginX, newOriginY);
}
-(iAction)zoomOut{
float newScale=[scrollView zoomScale]/ZOOM\u步骤;
如果([scrollView zoomScale]>最小刻度){
[自动处理的ZoomWith:newScale和ZoomType:FALSE];
}
}
-(IBAction)zoomIn{
float newScale=[scrollView zoomScale]*缩放步骤;
如果([scrollView zoomScale]<最大刻度){
[自动处理ZoomWith:newScale和ZoomType:TRUE];
}
}
-(void)handleZoomWith:(float)newScale和ZoomType:(BOOL)isZoomIn{
CGPoint newOrigin=[zoomHandler getNewOriginFromViewLocation:[scrollView contentOffset]
viewSize:scrSize和zoomtype:isZoomIn];
CGRect zoomRect=[self-zoomRectForScale:newScale with center:newOrigin];
[滚动视图zoomToRect:zoomRect动画:是];
}
-(CGRect)缩放比例:(浮点)带中心的缩放比例:(CGPoint)中心{
CGRect-zoomRect;
//缩放比例为1.0时,它将是scrollView边界的大小。
//随着缩放比例的减小,更多的内容可见,矩形的大小也随之增大。
zoomRect.size.height=[WebView框架].size.height/scale;
zoomRect.size.width=[WebView框架].size.width/scale;
//选择一个原点以获得正确的中心。
zoomRect.origin.x=中心.x/比例;
zoomRect.origin.y=中心.y/比例;
返回zoomRect;
}
/**
确定原点[这在ZOOMHANDLER内]
*/
-(CGPoint)getNewOriginFromViewLocation:(CGPoint)oldOrigin viewSize:(CGPoint)viewSize和ZoomType:(BOOL)isZoomIn{
//计算原始中心(加上屏幕宽度/高度的一半)
浮动oldCenterX=oldOrigin.x+(viewSize.x/2);
浮动oldCenterY=oldOrigin.y+(viewSize.y/2);
//计算新中心
新中心;
如果(isZoomIn){
newCenter=CGPointMake(oldCenterX*zoomLevel,oldCenterY*zoomLevel);
}否则{
newCenter=CGPointMake(oldCenterX/zoomLevel,oldCenterY/zoomLevel);
}
//计算新原点(扣除屏幕宽度/高度的一半)
float newOriginX=newCenter.x-(viewSize.x/2);
float newOriginY=newCenter.y-(viewSize.y/2);
返回CGPointMake(newOriginX,newOriginY);
}
有人知道中心点计算不正确的原因吗?任何帮助都将不胜感激;这件事我已经坚持了一个星期了

谢谢,
Alex

您是否尝试过使用JavaScript而不是UIWebView的scrollview实现缩放

我相信您可以通过调用以下命令缩放网络视图:

[webView stringByEvaluatingJavaScriptFromString:@“document.body.style.zoom=5.0;”(使用您的
缩放步骤
作为值)

您还可以使用JS计算浏览器窗口的中心:

posY = getScreenCenterY();
posX = getScreenCenterX();

function getScreenCenterY() {
    var y = 0;
    y = getScrollOffset()+(getInnerHeight()/2);
    return(y);
}

function getScreenCenterX() {
    return(document.body.clientWidth/2);
}

function getInnerHeight() {
    var y;
    if (self.innerHeight) // all except Explorer
    {
        y = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight)
    // Explorer 6 Strict Mode
    {
        y = document.documentElement.clientHeight;
    }
    else if (document.body) // other Explorers
    {
        y = document.body.clientHeight;
    }
    return(y);
}

function getScrollOffset() {
    var y;
    if (self.pageYOffset) // all except Explorer
    {
        y = self.pageYOffset;
    }
    else if (document.documentElement &&
document.documentElement.scrollTop) // Explorer 6 Strict
    {
        y = document.documentElement.scrollTop;
    }
    else if (document.body) // all other Explorers
    {
        y = document.body.scrollTop;
    }
    return(y);
}

(来源:)

我刚看到你的回复!现在对解决方案的需求已经远去,但知道如何解决它肯定仍然有帮助。谢谢
posY = getScreenCenterY();
posX = getScreenCenterX();

function getScreenCenterY() {
    var y = 0;
    y = getScrollOffset()+(getInnerHeight()/2);
    return(y);
}

function getScreenCenterX() {
    return(document.body.clientWidth/2);
}

function getInnerHeight() {
    var y;
    if (self.innerHeight) // all except Explorer
    {
        y = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight)
    // Explorer 6 Strict Mode
    {
        y = document.documentElement.clientHeight;
    }
    else if (document.body) // other Explorers
    {
        y = document.body.clientHeight;
    }
    return(y);
}

function getScrollOffset() {
    var y;
    if (self.pageYOffset) // all except Explorer
    {
        y = self.pageYOffset;
    }
    else if (document.documentElement &&
document.documentElement.scrollTop) // Explorer 6 Strict
    {
        y = document.documentElement.scrollTop;
    }
    else if (document.body) // all other Explorers
    {
        y = document.body.scrollTop;
    }
    return(y);
}
for (UIScrollView *scroll in [myPDFView subviews]) {
  //Set the zoom level.
  [scroll setZoomScale:2.5f animated:YES];
}