UIScrollView 缩放 &内容插入 [英] UIScrollView Zooming & contentInset

查看:33
本文介绍了UIScrollView 缩放 &内容插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似于用户通过捏合来放大和缩小图像的 iOS 照片应用:

Simliar to iOS Photos App where the user is zooming in and out of an image by pinching:

UIView > UIScrollView > UIImageView > UIImage

UIView > UIScrollView > UIImageView > UIImage

最初,我遇到了缩放比例低于 1 的问题:图像偏离中心.我通过这样做修复了它:

Initially, I had the issue of zooming below scale 1: image being off centered. I got it fixed by doing this:

func scrollViewDidZoom(scrollView: UIScrollView) {
        let offsetX = max((scrollView.bounds.width - scrollView.contentSize.width) * 0.5, 0)
        let offsetY = max((scrollView.bounds.height - scrollView.contentSize.height) * 0.5, 0)
        scrollView.contentInset = UIEdgeInsetsMake(offsetY, offsetX, 0, 0)
}

这在缩小时效果很好.

UIImage 内容模式是 aspectFit

UIImage content mode is aspectFit

当我ZOOM IN时,当zoomScale大于1时,滚动视图插入需要拥抱滚动视图包含的UIImage的周围.这消除了 UIImage 周围的死空间.通过捏合或双击放大时的 IE、照片应用程序.

When I ZOOM IN, when zoomScale is above 1, scroll view insets need to hug the surroundings of the UIImage that the scroll view contains. This takes away the dead-space that was surrounding the UIImage. IE, Photos app when zooming-in by pinching or double tapping.

    func scrollViewDidZoom(scrollView: UIScrollView) {
    if scrollView.zoomScale > 1 {
        let imageScale = (self.imageView.bounds.width/self.imageView.image!.size.width)
        let imageWidth = self.imageView.image!.size.width * imageScale
        let imageHeight = self.imageView.image!.size.height * imageScale
        scrollView.contentInset = UIEdgeInsetsMake(((scrollView.frame.height - imageHeight) * 0.5), (scrollView.frame.width - imageWidth) * 0.5 , 0, 0)
        print (scrollView.contentInset.top)
    }
    else {

        let offsetX = max((scrollView.bounds.width - scrollView.contentSize.width) * 0.5, 0)
        let offsetY = max((scrollView.bounds.height - scrollView.contentSize.height) * 0.5, 0)
        scrollView.contentInset = UIEdgeInsetsMake(offsetY, offsetX, 0, 0)
    }
}

以上添加似乎仍然改变了插入量.

Above addition seems to vary the inset amount still.

第一张图片显示了默认布局.放大时休息显示.....

First image shows the default layout. Rest shows when zoomed in.....

推荐答案

您的方法看起来是正确的.您需要按如下方式更新您的代码.

Your approach looks correct. You need to update your code as below.

    func scrollViewDidZoom(scrollView: UIScrollView) {
    
    if scrollView.zoomScale > 1 {
        
        if let image = imageView.image {
            
            let ratioW = imageView.frame.width / image.size.width
            let ratioH = imageView.frame.height / image.size.height
            
            let ratio = ratioW < ratioH ? ratioW:ratioH
            
            let newWidth = image.size.width*ratio
            let newHeight = image.size.height*ratio
            
            let left = 0.5 * (newWidth * scrollView.zoomScale > imageView.frame.width ? (newWidth - imageView.frame.width) : (scrollView.frame.width - scrollView.contentSize.width))
            let top = 0.5 * (newHeight * scrollView.zoomScale > imageView.frame.height ? (newHeight - imageView.frame.height) : (scrollView.frame.height - scrollView.contentSize.height))
            
            scrollView.contentInset = UIEdgeInsetsMake(top, left, top, left)
        }
    } else {
        scrollView.contentInset = UIEdgeInsetsZero
    }
}

斯威夫特 5

    func scrollViewDidZoom(scrollView: UIScrollView) {
    
    if scrollView.zoomScale > 1 {

        if let image = imageView.image {

            let ratioW = imageView.frame.width / image.size.width
            let ratioH = imageView.frame.height / image.size.height

            let ratio = ratioW < ratioH ? ratioW:ratioH

            let newWidth = image.size.width*ratio
            let newHeight = image.size.height*ratio

            let left = 0.5 * (newWidth * scrollView.zoomScale > imageView.frame.width ? (newWidth - imageView.frame.width) : (scrollView.frame.width - scrollView.contentSize.width))
            let top = 0.5 * (newHeight * scrollView.zoomScale > imageView.frame.height ? (newHeight - imageView.frame.height) : (scrollView.frame.height - scrollView.contentSize.height))

            scrollView.contentInset = UIEdgeInsets(top: top, left: left, bottom: top, right: left)
        }
    } else {
        scrollView.contentInset = .zero
    }
}

这篇关于UIScrollView 缩放 &amp;内容插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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