How to get an image to enlarge when I knocked twice?

I managed to get my application to zoom in if I double-tap it, but it won’t zoom in where I tapped twice! I want the image to center the coordinate that I tapped twice!

My code is:

in the .h file:

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

in the .m file:

  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]; } 

What should I do next?

Thanks in advance!

/ A noob

+4
source share
3 answers

here is a snippet of my old project.
Add a UITapGestureRecognizer to your init method:

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

This works when you double-tap in your view.

 - (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)]; } 
+8
source

You are looking for -locationInView: This will give you a point in the indicated view where the touch occurred. At this point, you can customize the view to make this point the center.

+4
source

Swift 3.0 version , which is scaled by double-clicking:

 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)) } } 
+3
source

Source: https://habr.com/ru/post/1414442/


All Articles