Xcode Swift图像缩放手势

时间:2016-03-07 11:28:13

标签: xcode swift uiimage

希望有人可以帮助我。我试图做出缩放手势,因此当呈现图像时,用户可以用手指缩放图像。

我展示图片的代码是:

// MARK: Show image full screen

func imageTapped(img: AnyObject) {

    self.navigationController?.navigationBarHidden = true

    let imageView = productImage as UIImageView

    let newImageView = UIImageView(image: imageView.image)
    newImageView.frame = self.view.frame
    newImageView.backgroundColor = .blackColor()
    newImageView.contentMode = .ScaleToFill
    newImageView.userInteractionEnabled = true

    let tap = UITapGestureRecognizer(target: self, action: "dismissFullscreenImage:")
    newImageView.addGestureRecognizer(tap)
    self.view.addSubview(newImageView)
}

func dismissFullscreenImage(sender: UITapGestureRecognizer) {
    sender.view?.removeFromSuperview()
    self.navigationController?.navigationBarHidden = false
}

2 个答案:

答案 0 :(得分:2)

使用UIScrollView并在滚动视图中添加UIImgeView

import UIKit

class ViewController: UIViewController,UIScrollViewDelegate
{
    var scrollV : UIScrollView!
    var imageView : UIImageView!

    override func viewDidLoad()
    {
        super.viewDidLoad()

        self.navigationController?.navigationBarHidden = true

        scrollV=UIScrollView()
        scrollV.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height)
        scrollV.minimumZoomScale=1
        scrollV.maximumZoomScale=3
        scrollV.bounces=false
        scrollV.delegate=self;
        self.view.addSubview(scrollV)

        imageView=UIImageView()
        imageView.image = UIImage(imageLiteral: "neymar.jpg")
        imageView.frame = CGRectMake(0, 0, scrollV.frame.width, scrollV.frame.height)
        imageView.backgroundColor = .blackColor()
        imageView.contentMode = .ScaleToFill
        scrollV.addSubview(imageView)
   }

   func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView?
   {
        return imageView
   }
}

答案 1 :(得分:1)

以编程方式进行手势并制作其方法并在方法中记下此代码。

pinchRecognizerOnView()是我的方法名称。在视图控制器中获取一个视图或图像视图,并在其中添加此新视图。现在在此新视图上应用手势方法。

func pinchRecognizerOnView(sender: UIPinchGestureRecognizer!) {

    sender.view?.transform = CGAffineTransformScale((sender.view?.transform)!, sender.scale, sender.scale)

    sender.scale = 1

    // its for zoom in out screen
}