以两个手指之间的点为中心缩放 UIImageView

Scale UIImageView centered on the point between the two fingers

我使用 CGAffineTransformScale 实现了 UIImageView 的缩放。 我会让缩放以两根手指之间的点为中心。

我没有成功使用我的 UIImageView 的锚点来居中缩放。

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

    }
}

编辑

第1步,捏之前我的看法:

第2步,缩放前调用pinch

只要我捏住 UIImageView(上面代码中的 transitionView),图像就会移动。我不明白为什么我会丢失框架位置。

我将滚动视图的偏移量添加到新的锚点。完美运行!

 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)