1
votes

I implemented a pinch zoom of an UIImageView, using CGAffineTransformScale. I would have the zoom centered on the point between the two fingers.

I don't succeed to use the anchorpoint of my UIImageView to center the zoom.

func handlePinchGesture(gesture: UIPinchGestureRecognizer) {

     if(gesture.state == .Began) {

        self.transitionView = UIImageView(image: self.imageView.image)

        if let view = transitionView {
             view.frame = CGRectMake(0, self.topImageViewConstraint.constant, self.imageView.frame.width, self.imageView.frame.height)
             self.view.addSubview(transitionView)
         }

         let locationInView:CGPoint = gesture.locationInView(self.transitionView)
         let locationInSuperview:CGPoint = gesture.locationInView(self.transitionView.superview)

         self.transitionView.layer.anchorPoint = CGPointMake(locationInView.x / self.transitionView.bounds.size.width, locationInView.y / self.transitionView.bounds.size.height)
         self.transitionView.center = locationInSuperview

    }

    if(gesture.state == .Began || gesture.state == .Changed) {

        self.transitionView.transform = CGAffineTransformScale(self.transitionView.transform, gesture.scale, gesture.scale)
        gesture.scale = 1

    }
}

EDIT

Step 1, my view before the pinch:

image1

Step 2, when the pinch is called before the scale

image2

The image is shifted as soon as I pinch the UIImageView (transitionView in my code above). I don't understand why I lose the frame position.

1

1 Answers

1
votes

I added the offset of my scrollview to the new anchorPoint. Works perfectly!

 let newAnchorPoint:CGPoint = CGPoint(x:locationInView.x , y: locationInView.y+self.scrollView.contentOffset.y)
 self.transitionView.layer.anchorPoint = CGPointMake(newAnchorPoint.x / self.transitionView.bounds.size.width, newAnchorPoint.y / self.transitionView.bounds.size.height)
 self.transitionView.center = CGPointMake(locationInSuperview.x , locationInSuperview.y)