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

标签 ios swift uiimageview pinchzoom scaletransform

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

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

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步,捏之前我的看法:

image1

第2步,缩放前调用pinch时

image2

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

最佳答案

我将 ScrollView 的偏移量添加到新的 anchor 。完美运行!

 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)

关于ios - 以两个手指之间的点为中心缩放 UIImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35152385/

相关文章:

ios - didSelect mapView 注释无法正常工作

ios - 是否可以只构建一次 Pod?

ios - 快速网络服务通信

ios - 防止 CGAffineTransform 递增应用

ios - 如何使用 Nib 文件在 UIViewController 中添加两个 UIImageView?

ios - 如何在 iOS 应用程序中使用 kml 文件在 map View 上显示数据库中的数据

ios - XCTest 在 travis 尝试运行测试时崩溃

Swift二维字典错误

swift - 解析 : Match query with query to pointer

ios - 效果如何找出它所附加的 UIImageView 的显示大小?