5

Ive managed to get my app to zoom in my image if I double tap it, but it dosen't zoom in where I double tapped! I want the image to centre the coordinate that I double tapped!

My code:

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

Mangy92
  • 621
  • 1
  • 10
  • 25

3 Answers3

9

here is a snippet from my old project.
Add the UITapGestureRecognizer somewhere in your init-method:

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

This get's fired upon double tap somewhere 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)];
}
  • 3
    Works GREAT. What seems important to me at this point: This code comes from a UIScrollView Class. If anyone would like to use ScrollView as an Outlet: The "self." arguments need to be extended with the ScrollView name, as well as "content" (which represents the UIImageView). – filou Mar 06 '14 at 23:28
  • Not sure how to make this work with code that zooms out if already zoomed in. –  Jan 23 '22 at 06:23
9

Swift 3.0 version that zooms on double tap:

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))
    }
}
Avt
  • 16,927
  • 4
  • 52
  • 72
4

You are looking for -locationInView:. It will give you the point in the specified view where the touch happened. At that point you can adjust the view to make that point the center.

Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117
  • Thanks! I would really love it if you could write a the code I should use! If it isn't too much work! :) – Mangy92 May 25 '12 at 18:59
  • Well that get's complicated. You will get the coordinates within the specified view. If you specify the image view, you need to know the relationship between image view and the controller's view. If you specify the controller's view then you will need to know where the image view is at within the controller's view. – Jeffery Thomas May 25 '12 at 19:03
  • I don't understand what to do, sorry :( – Mangy92 May 25 '12 at 19:30