UIImageView 在 UIScrollView 中缩放和捏合

UIImageView zoom and pinch in UIScrollView(UIImageView 在 UIScrollView 中缩放和捏合)
本文介绍了UIImageView 在 UIScrollView 中缩放和捏合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我尝试了许多不同的方法来实现扩展图像和捏拉缩放.我找到了有关如何使用项目中保存的图像执行此操作的教程,但是我正在从 Firebase 检索我的图像.我希望一张图片占据 ProfileViewController 屏幕的一半左右,而其他图片能够扩展整页并且能够缩放.我现在拥有的代码可以做到这一点,但是缩放和位置不干净或易于使用.

I have tried many different ways of implementing expand image and pinch to zoom. I have found tutorials on how to do this with images saved in my project however I am retrieving my images from Firebase. I would like one image to take about half the ProfileViewController screen and the other pictures to be able to be expanded full page and also have the ability to zoom. My code I have for it now does this however the zooming and the position is not clean or easy to use.

var newImageView: UIImageView!



@IBAction func imageTapped(_ sender: UITapGestureRecognizer)  {
    let imageView = sender.view as! UIImageView

    let scrollView = UIScrollView(frame: self.view.frame)


    newImageView = UIImageView(image: imageView.image)
    newImageView.frame = self.view.frame
    newImageView.backgroundColor = .black
    newImageView.contentMode = .top
    newImageView.isUserInteractionEnabled = true


    let tap = UITapGestureRecognizer(target: self, action: #selector(dismissFullscreenImage))
    scrollView.addGestureRecognizer(tap)


    scrollView.delegate = self
    scrollView.minimumZoomScale = 1.0
    scrollView.maximumZoomScale = 3.0

    scrollView.addSubview(newImageView)

    self.view.addSubview(scrollView)
}

func viewForZooming(in scrollView: UIScrollView) -> UIView?
{
    return newImageView;
}

func dismissFullscreenImage(_ sender: UITapGestureRecognizer) {
    sender.view?.removeFromSuperview()
}

推荐答案

这是我正在以编程方式创建滚动视图和图像视图,这里 img 是我正在选择的 PHAsset图片来自画廊.

Here is I am creating scrollview and imageview programatically and here img is a PHAsset that I am selecting image from gallery.

我在做什么,选择一个图像并移动到其他视图控制器,然后我根据我的 imageview 调整该图像的大小因此图像在缩放后不会模糊.

最小缩放受限于 imageview,最大缩放为 4.0.

Here what I am doing, select an image and move to other view controller and I resize that image according to my imageview So image is not blurry after zooming it.

Minimum zooming is imageview bound and maxmimum zooming is 4.0.

func singleVC1()  {
        let v = UIView(frame: CGRect(x: 40, y: 50, width: self.view.frame.size.width - 80, height: view.frame.size.height - 200))
        let view1 = UIView(frame: CGRect(x: 0, y: 0, width: v.frame.size.width, height: v.frame.size.height/2 - 1))
        let scroll1 = UIScrollView.init(frame: view1.bounds)
        let imgview = UIImageView.init(frame: (CGRect)(origin: CGPoint.zero, size: self.returnAspectSize(targetWidth: view.frame.size.width, img: img)))
        self.setZoomScale(scrollview: scroll1, imageview: imgview)
        imgview.image = self.getUIImage(asset: img)
        scroll1.maximumZoomScale = 4.0;
        scroll1.delegate = self;
        scroll1.bounces = false
        scroll1.contentSize =  imgview.frame.size
        scroll1.addSubview(imgview)
        view1.layer.borderColor = UIColor.red.cgColor
        view1.layer.borderWidth = 5.0
        view1.addSubview(scroll1)
        v.addSubview(view1)

        let view2 = UIView(frame: CGRect(x: 0, y: v.frame.size.height/2+1, width: v.frame.size.width, height: v.frame.size.height/2 - 1))
        let scroll2 = UIScrollView.init(frame: view2.bounds)
        let imgview1 = UIImageView.init(frame: (CGRect)(origin: CGPoint.zero, size: self.returnAspectSize(targetWidth: view.frame.size.width, img: img)))
        imgview1.image = self.getUIImage(asset: img)
        self.setZoomScale(scrollview: scroll2, imageview: imgview1)
        scroll2.bounces = false

        scroll2.maximumZoomScale = 4.0;
        scroll2.delegate = self;
        view2.layer.borderColor = UIColor.red.cgColor
        view2.layer.borderWidth = 5.0
        scroll2.contentSize = imgview1.frame.size
        scroll2.addSubview(imgview1)
        view2.addSubview(scroll2)
        v.addSubview(view2)
        self.view.addSubview(v)
    }
func setZoomScale(scrollview : UIScrollView, imageview : UIImageView) {
        var minZoom = min(self.view.bounds.size.width / imageview.bounds.size.width, self.view.bounds.size.height / imageview.bounds.size.height);

        if (minZoom > 1.0) {
            minZoom = 1.0;
        }
        scrollview.minimumZoomScale = minZoom;
        scrollview.zoomScale = minZoom;
    }
    func returnAspectSize(targetWidth : CGFloat, img : PHAsset) -> CGSize {
        let img1 = self.getUIImage(asset: self.img)
        let targetwidth = targetWidth
        let scalefactor = (CGFloat)(targetwidth) / (CGFloat)((img1?.size.width)!)
        let targetheight = (img1?.size.height)! * scalefactor
        let targesize = CGSize(width: (CGFloat)(targetwidth), height: targetheight)
        return targesize
    }

试试这个.我相信这会对你有所帮助.

Try this. I am sure this will help you.

这篇关于UIImageView 在 UIScrollView 中缩放和捏合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

how to set scrollview content size in swift 3.0(如何在 swift 3.0 中设置滚动视图内容大小)
Stop a UITableView from automatically scrolling(阻止 UITableView 自动滚动)
iOS UIScrollView Lazy Loading(iOS UIScrollView 延迟加载)
using iOS 6.0 SDK and building for iOS 5 Target causes UIScrollView setMinimumZoomScale to fail when running on iOS 5 simulator(在 iOS 5 模拟器上运行时,使用 iOS 6.0 SDK 并为 iOS 5 Target 构建会导致 UIScrollView setMinimumZoomScale 失败) -
Create partial-screen UIPageViewController programmatically(以编程方式创建部分屏幕 UIPageViewController)
how to make an ImageView zoomable with or without ScrollView.?(如何使用或不使用 ScrollView 使 ImageView 可缩放?)