使用Swift中的UIScrollView从用户的触摸点放大图像

时间:2016-08-09 16:03:30

标签: ios swift optimization uiscrollview uiimage

以下Swift代码可管理UIImageUIScrollView的放大和缩小。

双击时,图像会缩放到中央并缩小到中心。

问题:

需要进行哪些代码更改才能将放大点设置为用户在屏幕上触摸的图像区域的中心?

(例如,如果用户双击图像的左上角,图像将相应地缩放到图像的左上角。)

class ScrollViewController: UIViewController, UIScrollViewDelegate {
    var scrollView: UIScrollView!
    var imageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()

        imageView = UIImageView(image: UIImage(named: "image.png"))

        scrollView = UIScrollView(frame: view.bounds)
        scrollView.backgroundColor = UIColor.blackColor()
        scrollView.contentSize = imageView.bounds.size
        scrollView.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]

        scrollView.contentOffset = CGPoint(x: 1000, y: 450)

        scrollView.addSubview(imageView)
        view.addSubview(scrollView)

        scrollView.delegate = self
        setZoomScale()
        setupGestureRecognizer()
    }

    func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
        return imageView
    }

    override func viewWillLayoutSubviews() {
        setZoomScale()
    }

    func setZoomScale() {
        let imageViewSize = imageView.bounds.size
        let scrollViewSize = scrollView.bounds.size
        let widthScale = scrollViewSize.width / imageViewSize.width
        let heightScale = scrollViewSize.height / imageViewSize.height

        scrollView.minimumZoomScale = min(widthScale, heightScale)
        scrollView.zoomScale = 1.0
    }

    func scrollViewDidZoom(scrollView: UIScrollView) {
        let imageViewSize = imageView.frame.size
        let scrollViewSize = scrollView.bounds.size
        let verticalPadding = imageViewSize.height < scrollViewSize.height ? (scrollViewSize.height - imageViewSize.height) / 2 : 0
        let horizontalPadding = imageViewSize.width < scrollViewSize.width ? (scrollViewSize.width - imageViewSize.width) / 2 : 0

        scrollView.contentInset = UIEdgeInsets(top: verticalPadding, left: horizontalPadding, bottom: verticalPadding, right: horizontalPadding)
    }

    func setupGestureRecognizer() {
        let doubleTap = UITapGestureRecognizer(target: self, action: "handleDoubleTap:")
        doubleTap.numberOfTapsRequired = 2
        scrollView.addGestureRecognizer(doubleTap)
    }

    func handleDoubleTap(recognizer: UITapGestureRecognizer) {
        if (scrollView.zoomScale > scrollView.minimumZoomScale) {
            scrollView.setZoomScale(scrollView.minimumZoomScale, animated: true)
        } else {
            scrollView.setZoomScale(scrollView.maximumZoomScale, animated: true)
        }
    }
}

2 个答案:

答案 0 :(得分:5)

根据this post。它对我很好。

func handleDoubleTap(recognizer: UITapGestureRecognizer) {
    if (scrollView.zoomScale > scrollView.minimumZoomScale) {
        scrollView.setZoomScale(scrollView.minimumZoomScale, animated: true)
    } else {
        let touchPoint = recognizer.locationInView(view)
        let scrollViewSize = scrollView.bounds.size

        let width = scrollViewSize.width / scrollView.maximumZoomScale
        let height = scrollViewSize.height / scrollView.maximumZoomScale 
        let x = touchPoint.x - (width/2.0)
        let y = touchPoint.y - (height/2.0)

        let rect = CGRectMake(x, y, width, height)
        scrollView.zoomToRect(rect, animated: true)
    }
}

答案 1 :(得分:1)

这是一个Swift 2.3函数,用于在可缩放的UIScrollView中返回触摸的位置:

  • UIScrollView内置UIImageView
  • 是否缩放 - scrollView.zoomScale
  • imageView.contentMode = .ScaleAspectFit
  • 图像可以比imageView和scrollView
  • 更大,更高
  • 任何形状的图像,肖像,风景或正方形

imageView.contentMode = .ScaleAspectFit

//import AVFoundation // needed for AVMakeRectWithAspectRatioInsideRect()

func getLocationOfTouchInImageInScrollView(paintLocation:CGPoint)->CGPoint {

    let imageSize = imageViewInScrollView.image!.size
    let imageFrame = scrollView.frame
    let imageRect = AVMakeRectWithAspectRatioInsideRect(imageSize, imageFrame)
    let imageHeightToViewHeight = max(imageSize.height, imageSize.width) / imageFrame.size.height

    let px = (max(0, min(imageSize.width, ((paintLocation.x - imageRect.origin.x) * imageHeightToViewHeight))))
    let py = (max(0, min(imageSize.height, ((paintLocation.y - imageRect.origin.y ) * imageHeightToViewHeight))))
    var imageTouchPoint = CGPointMake(CGFloat(px), CGFloat(py))

    return imageTouchPoint
}

由于我无法弄清楚imageHeightToViewHeight变量,所以我已经花了很多时间来写这个,因为还有其他一些有趣的事情发生了。