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 中缩放和捏合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Hardware Volume buttons change in app volume(硬件音量按钮更改应用程序音量)
Resume game cocos2d(恢复游戏 cocos2d)
Cocos2D + Disabling only Retina iPad Graphics(Cocos2D + 仅禁用 Retina iPad 图形)
[ios.cocos2d+box2d]how to disable auto-rotation?([ios.cocos2d+box2d]如何禁用自动旋转?)
Storing and reading files from Documents directory iOS 5(从 Documents 目录存储和读取文件 iOS 5)
How can I implement a virtual joystick for a cocos2d game outside the cocos2d environment?(如何在 cocos2d 环境之外实现 cocos2d 游戏的虚拟摇杆?)