UIScrollView 缩放 uiview 到纵横填充 [英] UIScrollView zoom uiview to aspect fill

查看:32
本文介绍了UIScrollView 缩放 uiview 到纵横填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 UIScrollView 中有一个图像,我试图让它默认为纵横填充,下面的代码按照第一张附加图像中列出的方式缩放它.但这不是我所期望的,如何正确扩展?

I have an image in the UIScrollView and I'm trying to make it aspect fill by default, the code below scale it as listed on the first attached image. But it's not what I expect, how to scale it properly?

    func aspectFill() {
        let imageSize = imageZoomView.bounds.size

        let xScale = bounds.width / imageSize.width
        let yScale = bounds.height / imageSize.height

        zoomScale = max(xScale, yScale)
    }

但预期的结果是

推荐答案

垂直和横向图像方向的解决方案:

Solution for both vertical and landscape image orientations:

let widthRatio = sourseImageSize.width / sourseImageSize.height
let height = scrollView.bounds.width / widthRatio
let zoomScaleH = scrollView.bounds.height / height
    
let heightRatio = sourseImageSize.height / sourseImageSize.width
let width = scrollView.bounds.height / heightRatio
let zoomScaleW = scrollView.bounds.width / width

let scaleResult = max(zoomScaleH, zoomScaleW)

scrollView.setZoomScale(scaleResult, animated: false)

这篇关于UIScrollView 缩放 uiview 到纵横填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆