swift ScrollView中的图像

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift ScrollView中的图像相关的知识,希望对你有一定的参考价值。

         var scrollView : UIScrollView!
         var imageView : UIImageView!
        
        // inside viewDidload initialise the components
        scrollView = UIScrollView(frame: view.frame)
        scrollView.delegate = self
        imageView = UIImageView(frame: view.frame)
        imageView.contentMode = .scaleAspectFit
        scrollView.backgroundColor = UIColor.red
        view.addSubview(scrollView)
        scrollView.addSubview(imageView)
        imageView.image = UIImage(named: "jonSnow")
    
        scrollView.alwaysBounceVertical = false
        scrollView.alwaysBounceHorizontal = false
        scrollView.showsVerticalScrollIndicator = true
        scrollView.flashScrollIndicators()
        
        scrollView.minimumZoomScale = 1.0
        scrollView.maximumZoomScale = 4.0

        let doubleTapGest = UITapGestureRecognizer(target: self, action: #selector(handleDoubleTapScrollView(recognizer:)))
        doubleTapGest.numberOfTapsRequired = 2
        scrollView.addGestureRecognizer
        
        
        /* this function helps the double tap /*
        
         @objc func handleDoubleTapScrollView(recognizer: UITapGestureRecognizer) {
        if scrollView.zoomScale == 1 {
            scrollView.zoom(to: zoomRectForScale(scale: scrollView.maximumZoomScale, center: recognizer.location(in: recognizer.view)), animated: true)
        } else {
            scrollView.setZoomScale(1, animated: true)
        }
    }
     

    func zoomRectForScale(scale: CGFloat, center: CGPoint) -> CGRect {
        var zoomRect = CGRect.zero
        zoomRect.size.height = imageView.frame.size.height / scale
        zoomRect.size.width  = imageView.frame.size.width  / scale
        let newCenter = imageView.convert(center, from: scrollView)
        zoomRect.origin.x = newCenter.x - (zoomRect.size.width / 2.0)
        zoomRect.origin.y = newCenter.y - (zoomRect.size.height / 2.0)
        return zoomRect
    }
 /* this function helps the zooming of the image /*
    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return self.imageView
    }
    

以上是关于swift ScrollView中的图像的主要内容,如果未能解决你的问题,请参考以下文章

Swift 中一个垂直 ScrollView 中的多个水平 ScrollView?

Swift - ScrollView 中的 StackView 不滚动

使用 AutoLayout 以编程方式将 UILabel 添加到 ScrollView (swift)

Scrollview 只显示我在 swift 3 中使用情节提要创建的视图中的最后一个数组元素?

iOS Swift ScrollView、UITableView、滚动区域

(Swift) ScrollView 中的 StackView 不起作用