【问题标题】:making an image the full size of a scrollview使图像成为滚动视图的全尺寸
【发布时间】:2015-06-04 16:01:42
【问题描述】:

我已在我的 scrollView 中将图像设置为背景,因此我可以放大它周围的平移,但是,当图像完全缩小时,它会在图像的左侧和右侧创建白色边框,我只能假设是滚动视图的边距。

有什么办法可以去掉这些,让图像填满屏幕?

编辑:这是一张图片来说明我的意思,灰色背景两侧的白条是滚动视图的边缘,我希望灰色背景完全填满屏幕,除了 iAd 条。 http://i.stack.imgur.com/qX0Kk.png

非常感谢!

// 1
        let image = UIImage(named: "Photo1")!
        imageView = UIImageView(image: image)
        imageView.frame = CGRect(origin: CGPointMake(0, 0), size:image.size)
        
        // 2
        scrollView.addSubview(imageView)
        scrollView.contentSize = image.size
        
        // 3
        var doubleTapRecognizer = UITapGestureRecognizer(target: self, action: "scrollViewDoubleTapped:")
        doubleTapRecognizer.numberOfTapsRequired = 2
        doubleTapRecognizer.numberOfTouchesRequired = 1
        scrollView.addGestureRecognizer(doubleTapRecognizer)
        
        // 4
        let scrollViewFrame = scrollView.frame
        let scaleWidth = scrollViewFrame.size.width / scrollView.contentSize.width
        let scaleHeight = scrollViewFrame.size.height / scrollView.contentSize.height
        let minScale = min(scaleWidth, scaleHeight);
        scrollView.minimumZoomScale = minScale;
        
        // 5
        scrollView.maximumZoomScale = 1.0
        scrollView.zoomScale = minScale;
        
        // 6
        centerScrollViewContents()    }
    
    func centerScrollViewContents() {
        let boundsSize = scrollView.bounds.size
        var contentsFrame = imageView.frame
        
        if contentsFrame.size.width < boundsSize.width {
            contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0
        } else {
            contentsFrame.origin.x = 0.0
        }
        
        if contentsFrame.size.height < boundsSize.height {
            contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0
        } else {
            contentsFrame.origin.y = 0.0
        }
        
        imageView.frame = contentsFrame
        
    }
    
    func scrollViewDoubleTapped(recognizer: UITapGestureRecognizer) {
        // 1
        let pointInView = recognizer.locationInView(imageView)
        
        // 2
        var newZoomScale = scrollView.zoomScale * 1.5
        newZoomScale = min(newZoomScale, scrollView.maximumZoomScale)
        
        // 3
        let scrollViewSize = scrollView.bounds.size
        let w = scrollViewSize.width / newZoomScale
        let h = scrollViewSize.height / newZoomScale
        let x = pointInView.x - (w / 2.0)
        let y = pointInView.y - (h / 2.0)
        
        let rectToZoomTo = CGRectMake(x, y, w, h);
        
        // 4
        scrollView.zoomToRect(rectToZoomTo, animated: true)
    }
    
    func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
        return imageView
    }
    
    func scrollViewDidZoom(scrollView: UIScrollView) {
        centerScrollViewContents()
    }

【问题讨论】:

    标签: swift uiscrollview uiimage


    【解决方案1】:

    您将最小缩放比例设置为宽度和高度比例的最小值。这通常是需要的,因为它允许您查看整个图像 (AspectFit)。听起来您想要 AspectFill,但要做到这一点,您需要使用最大宽度和高度比例。

    let minScale = max(scaleWidth, scaleHeight);
    

    【讨论】:

      猜你喜欢
      • 2021-12-31
      • 2013-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多