UIScrollView zoomToRect没有缩放到给定的rect(从UITouch CGPoint创建) [英] UIScrollView zoomToRect not zooming to given rect (created from UITouch CGPoint)

查看:329
本文介绍了UIScrollView zoomToRect没有缩放到给定的rect(从UITouch CGPoint创建)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序有一个带有一个子视图的UIScrollView。子视图是一个扩展的UIView,它使用drawLayer事件中的图层将PDF页面打印到自身。

My application has a UIScrollView with one subview. The subview is an extended UIView which prints a PDF page to itself using layers in the drawLayer event.

使用内置的pinching缩放效果很好。 setZoomScale也可以按预期工作。

Zooming using the built in pinching works great. setZoomScale also works as expected.

我一直在努力使用zoomToRect函数。我发现了一个在线示例,它从给定的CGPoint生成CGRect zoomRect变量。

I have been struggling with the zoomToRect function. I found an example online which makes a CGRect zoomRect variable from a given CGPoint.

在touchesEnded函数中,如果有双击并且它们一直缩小,我想放大我创建的PDFUIView,好像它们正在捏用它们双击的夹点中心。

In the touchesEnded function, if there was a double tap and they are all the way zoomed out, I want to zoom in to that PDFUIView I created as though they were pinching out with the center of the pinch where they double tapped.

假设我将UITouch变量传递给我的函数,如果它们双击则使用zoomToRect。

So assume that I pass the UITouch variable to my function which utilizes zoomToRect if they double tap.

我开始使用我在苹果网站上找到的以下功能:

I started with the following function I found on apples site:

http://developer.apple.com/iphone/library/documentation/WindowsViews/Conceptual/UIScrollView_pg/ZoomZoom /ZoomZoom.html

以下是我的UIScrollView扩展类的修改版本:

The following is a modified version for my UIScrollView extended class:

- (void)zoomToCenter:(float)scale withCenter:(CGPoint)center {     

    CGRect zoomRect;
    zoomRect.size.height = self.frame.size.height / scale;
    zoomRect.size.width  = self.frame.size.width  / scale;

    zoomRect.origin.x = center.x - (zoomRect.size.width  / 2.0);
    zoomRect.origin.y = center.y - (zoomRect.size.height / 2.0); 

    //return zoomRect;

    [self zoomToRect:zoomRect animated:YES];

} 

当我这样做时,UIScrollView似乎使用zoomRect的右上边缘而不是中心。

When I do this, the UIScrollView seems to zoom using the bottom right edge of the zoomRect above and not the center.

如果我像这样制作UIView

If I make UIView like this

UIView *v = [[UIView alloc] initWithFrame:zoomRect]; 
[v setBackgroundColor:[UIView redColor]];
[self addSubview:v];

红色框显示触点不在中心。

The red box shows up with the touch point dead in the center.

请注意:我正在从我的电脑上写这篇文章,我记得在我的Mac上用两部分分开,所以假设这画了一个矩形,触点在中心。如果UIView离开中心但是缩小到正确的位置就会很好。

Please note: I am writing this from my PC, I recall messing around with the divided by two part on my Mac, so just assume that this draws a rect with the touch point in the center. If the UIView drew off center but zoomed to the right spot it would be all good.

但是,当它预先形成zoomToRect它似乎使用右下角时会发生什么关闭放大结果左上角的zoomRect。

However, what happens is when it preforms the zoomToRect it seems to use the bottom right off the zoomRect at the top left of the zoomed in results.

另外,我注意到,根据我点击UIScrollView的位置,它会锚定到不同的位置。几乎看起来中间有一个十字架,它以某种方式反映了点,好像中间的任何一个点是负反射,中间的任何地方都是正反射?

Also, I noticed that depending on where I click on the UIScrollView, it anchors to diffrent spots. It almost seems like there is a cross down the middle and it's reflecting the points somehow as though anywhere left of the middle is a negative reflection and anywhere right of the middle is a positive reflection?

这看起来很复杂,不应该只是缩放到UIView能够绘制的矩形吗?

This seems to complicated, shouldn't it just zoom to the rect that was drawn as the UIView was able to draw?

我用了很多研究弄清楚如何创建一个高质量的PDF,所以我假设使用CALayer可能会抛弃坐标系?但是对于UIScrollView,它应该将其视为具有768x985尺寸的视图。

I used a lot of research to figure out how to create a PDF that scales in high quality, so I am assuming that using the CALayer may be throwing off the coordinate system? But to the UIScrollView it should just treat it as a view with 768x985 dimensions.

这有点高级,请假设创建zoomRect的代码都很好。 UIView中的CALayer有更深层次的内容,位于UIScrollView ....

This is sort of advanced, please assume the code for creating the zoomRect is all good. There is something deeper with the CALayer in the UIView which is in the UIScrollView....

推荐答案

   Declare BOOL isZoom; in .h


     -(void)handleDoubleTap:(UIGestureRecognizer *)recognizer {  
            if(isZoom){
                CGPoint Pointview=[recognizer locationInView:self];
                CGFloat newZoomscal=3.0;

                newZoomscal=MIN(newZoomscal, self.maximumZoomScale);

                CGSize scrollViewSize=self.bounds.size;

                CGFloat w=scrollViewSize.width/newZoomscal;
                CGFloat h=scrollViewSize.height /newZoomscal;
                CGFloat x= Pointview.x-(w/2.0);
                CGFloat y = Pointview.y-(h/2.0);

                CGRect rectTozoom=CGRectMake(x, y, w, h);
                [self zoomToRect:rectTozoom animated:YES]; 

                [self setZoomScale:3.0 animated:YES]; 
                isZoom=NO;
            }
            else{ 
                [self setZoomScale:1.0 animated:YES]; 
                isZoom=YES;
            }
        } 

这篇关于UIScrollView zoomToRect没有缩放到给定的rect(从UITouch CGPoint创建)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆