图像查看到全屏 - 需要缩放

时间:2018-05-18 23:54:58

标签: swift image zoom fullscreen pinch

美好的一天,我正在写这个问题,因为我正在尝试在全屏图像上添加缩放(捏合手势)。我所做的是使用我在这里找到的一些功能,但我从来没有找到添加缩放手势(捏)的方法。基本上我在ViewController中有一个图像(@IBOutlet weak var fotografiaContactoGenerales:UIImageView!),我应用了下面的代码:

在我的View Controller里面viewDidLoad():

    // add TAP Recognizer
    let singleTap = UITapGestureRecognizer(target: self, action: #selector(ViewControllerGenerales.tapDetected))
    singleTap.numberOfTapsRequired = 1 // you can change this value
    fotografiaContactoGenerales.isUserInteractionEnabled = true
    fotografiaContactoGenerales.addGestureRecognizer(singleTap)

外部viewDidLoad()

@objc func tapDetected() {
    let imageView = fotografiaContactoGenerales!
    let newImageView = UIImageView(image: imageView.image)
    newImageView.frame = UIScreen.main.bounds
    newImageView.backgroundColor = .white
    newImageView.contentMode = .scaleAspectFit
    newImageView.isUserInteractionEnabled = true

    // Set Tap Recognizer
    let tap = UITapGestureRecognizer(target: self, action: #selector(dismissFullscreenImage))
    newImageView.addGestureRecognizer(tap)

    self.view.addSubview(newImageView)
    self.navigationController?.isNavigationBarHidden = true
    self.tabBarController?.tabBar.isHidden = true
}

@objc func dismissFullscreenImage(_ sender: UITapGestureRecognizer) {
    self.navigationController?.isNavigationBarHidden = false
    self.tabBarController?.tabBar.isHidden = false
    sender.view?.removeFromSuperview()
}

此时我的应用程序可以全屏显示图片但我不知道如何应用例程或某些代码以允许用户对显示的图像应用缩放缩放。

请您通过添加以上代码来帮助我进行缩放捏合手势吗?

提前谢谢。

2 个答案:

答案 0 :(得分:1)

谢谢,我想分享有关此问题的解决方案,这非常容易,只是我添加了一个带有捏和下面功能的新手势:)

@objc func scaleImage(_ sender: UIPinchGestureRecognizer) {
    if sender.state == .ended || sender.state == .changed {

        let currentScale = sender.view!.frame.size.width / sender.view!.bounds.size.width
        var newScale = currentScale*sender.scale

        if newScale < 1 {
            newScale = 1
        }
        if newScale > 7 {
            newScale = 7
        }

        let transform = CGAffineTransform(scaleX: newScale, y: newScale)

        self.newImageView.transform = transform
        sender.scale = 1

    }
}

我要做的另一步是将UIIMAGE变量移动或创建到原始的全屏功能之外,这对我有所帮助。

现在,我可以缩放图像了,但是现在我的问题是允许用手指ojjojoj移动图像

答案 1 :(得分:0)

我将此添加为全球分店

var headerImageWithZoom = UIImageView()

lazy var headerImageView: UIImageView = {
    let iv = UIImageView()
    iv.contentMode = .scaleAspectFill
    iv.layer.masksToBounds = true
    iv.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tapHeaderImageAction)))
    iv.isUserInteractionEnabled = true
    iv.translatesAutoresizingMaskIntoConstraints = false
    return iv
}()

这些是动作:

@objc func tapHeaderImageAction(_ sender: UITapGestureRecognizer) {
    let imageView = sender.view as! UIImageView
    headerImageWithZoom = UIImageView(image: imageView.image)
    headerImageWithZoom.frame = UIScreen.main.bounds
    headerImageWithZoom.contentMode = .scaleAspectFit
    headerImageWithZoom.isUserInteractionEnabled = true
    self.navigationController?.isNavigationBarHidden = true
    self.tabBarController?.tabBar.isHidden = true
    
    
    let scrollView = UIScrollView()
    scrollView.frame = UIScreen.main.bounds
    scrollView.backgroundColor = .black
    scrollView.frame = UIScreen.main.bounds
    scrollView.maximumZoomScale = 4
    scrollView.minimumZoomScale = 1.0
    scrollView.bounces = true
    scrollView.bouncesZoom = true
    scrollView.showsHorizontalScrollIndicator = true
    scrollView.showsVerticalScrollIndicator = true
    scrollView.delegate = self
    let tap = UITapGestureRecognizer(target: self, action: #selector(dismissFullscreenImage))
    scrollView.addGestureRecognizer(tap)
    scrollView.addSubview(headerImageWithZoom)
    headerImageView.center = scrollView.center
    view.addSubview(scrollView)
}

@objc func dismissFullscreenImage(_ sender: UITapGestureRecognizer) {
    self.navigationController?.isNavigationBarHidden = false
    self.tabBarController?.tabBar.isHidden = false
    sender.view?.removeFromSuperview()
}

最后是委托函子:

extension PinnedViewViewController: UIScrollViewDelegate {
    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return headerImageWithZoom
    }
}