5

如果我双击它,我已经设法让我的应用程序放大我的图像,但它并没有放大我双击的地方!我希望图像以我双击的坐标为中心!

我的代码:

在 .h 文件中:

 - (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer;

在 .m 文件中:

    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];

[doubleTap setNumberOfTapsRequired:2];

[self.scroll addGestureRecognizer:doubleTap];

- (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer {  

    if(self.scroll.zoomScale > scroll.minimumZoomScale)
        [self.scroll setZoomScale:scroll.minimumZoomScale animated:YES]; 
    else 
        [self.scroll setZoomScale:1.6 animated:YES]; 
 }

接下来我该怎么办?

提前致谢!

/一个菜鸟

4

3 回答 3

9

这是我旧项目的一个片段。在您的-method中
添加UITapGestureRecognizer某处:init

UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleTapRecognized:)];
doubleTap.numberOfTapsRequired = 2;
[self addGestureRecognizer:doubleTap];
[doubleTap release];

在您视图中的某处双击时会触发此操作。

- (void)doubleTapRecognized:(UITapGestureRecognizer*)recognizer {
    [self zoomToPoint:[recognizer locationInView:content]];
}

- (void)zoomToPoint:(CGPoint)location {
    float zoomScaleBefore = self.zoomScale;

    if (location.x<=0) location.x = 1;
    if (location.y<=0) location.y = 1;
    if (location.x>=content.bounds.size.width) location.x = content.bounds.size.width-1;
    if (location.y>=content.bounds.size.height) location.y = content.bounds.size.height-1;

    float percentX = location.x/content.bounds.size.width;
    float percentY = location.y/content.bounds.size.height;


    [self setZoomScale:10.0 animated:YES];

    float pox = (percentX*self.contentSize.width)-(self.frame.size.width/2);
    float poy = (percentY*self.contentSize.height)-(self.frame.size.height/2);

    if (pox<=0) pox = 1;
    if (poy<=0) poy = 1;

    [self scrollRectToVisible:
     CGRectMake(pox, poy, self.frame.size.width, self.frame.size.height)
                     animated:(self.zoomScale == zoomScaleBefore)];
}
于 2014-03-06T22:55:01.280 回答
7

双击放大的Swift 3.0版本:

let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(onDoubleTap(gestureRecognizer:)))
tapRecognizer.numberOfTapsRequired = 2
view.addGestureRecognizer(tapRecognizer)

...

func onDoubleTap(gestureRecognizer: UITapGestureRecognizer) {
    let scale = min(scrollView.zoomScale * 2, scrollView.maximumZoomScale)

    if scale != scrollView.zoomScale {
        let point = gestureRecognizer.location(in: imageView)

        let scrollSize = scrollView.frame.size
        let size = CGSize(width: scrollSize.width / scale,
                          height: scrollSize.height / scale)
        let origin = CGPoint(x: point.x - size.width / 2,
                             y: point.y - size.height / 2)
        scrollView.zoom(to:CGRect(origin: origin, size: size), animated: true)
        print(CGRect(origin: origin, size: size))
    }
}
于 2016-11-10T12:03:59.477 回答
4

您正在寻找-locationInView:. 它会在指定视图中为您提供触摸发生的点。此时,您可以调整视图以使该点成为中心。

于 2012-05-25T18:57:24.090 回答