如何将图像缩放到双击的位置?

Man*_*y92 4 iphone center zoom uiscrollview ios

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

我的代码:

在.h文件中:

 - (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer;
Run Code Online (Sandbox Code Playgroud)

在.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]; 
 }
Run Code Online (Sandbox Code Playgroud)

接下来我该怎么办?

提前致谢!

/一个菜鸟

小智 8

这是我的旧项目的一个片段.
添加UITapGestureRecognizer你的init方法中的某个地方:

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

在您视图中的某个位置双击时会触发此触发器.

- (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)];
}
Run Code Online (Sandbox Code Playgroud)

  • 工作很棒.在这一点上对我来说似乎很重要:此代码来自UIScrollView类.如果有人想使用ScrollView作为Outlet:"自我".需要使用ScrollView名称以及"内容"(代表UIImageView)扩展参数. (2认同)

Avt*_*Avt 6

Swift 3.0版本可以轻松缩放:

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

...

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))
    }
}
Run Code Online (Sandbox Code Playgroud)


Jef*_*mas 4

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