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:
Step 2, when the pinch is called before the scale
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.