Search code examples
iosswiftuiscrollviewuiimageviewuitapgesturerecognizer

Move image to other view with double tap


I am new to Swift and I am looking for a way to move my image to other view controller with double tap.

I made my image into scroll view, so I can slide to view it.

Here is my code.

class Quotes: UIViewController, UIScrollViewDelegate {

   @IBOutlet weak var scroll: UIScrollView!

let imageview = ["quotes1","quotes2","quotes3","quotes4"]
var imagine = UIImageView()
override func viewDidLoad() {
    let tap = UITapGestureRecognizer(target: self, action: #selector(doubletap))
    tap.numberOfTapsRequired = 2
    view.addGestureRecognizer(tap)
    self.navigationController?.setNavigationBarHidden(true, animated: true)
    quotesimageload()
    }
func quotesimageload() {
    for index in 0 ... imageview.count - 1
    {
        imagine = UIImageView (frame:CGRect(x: self.scroll.frame.width * CGFloat(index), y: 0 , width: self.scroll.frame.width, height: self.scroll.frame.height))
        imagine.image = UIImage(named : imageview[index])
        imagine.tag = index
        imagine.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        self.scroll.addSubview(imagine)
    }
    self.scroll.contentSize = CGSize(width: self.scroll.frame.width * CGFloat(imageview.count), height: self.scroll.frame.height)
  }
func doubletap(){
    let view = self.storyboard?.instantiateViewController(withIdentifier: "pinch")
    self.navigationController?.pushViewController(view!, animated: true)

        }

}


Solution

  • You don't exactly move the image to another view controller - you pass a copy of it to the target VC.

    Set up a "var" UIImage in the secondVC (let's say you called it image) and before you push that VC into view, populate it from the first VC.

    One more word of caution - naming a UIViewController as "view" can be very confusing, as many would think it's a UIView. So assuming you rename that to be pinchViewController, the full syntax would be:

    Second VC:

    var image:UIView!
    

    First VC:

    func doubletap(){
        let  pinchViewController = self.storyboard?.instantiateViewController(withIdentifier: "pinch")
        pinchViewController.image = imagine.image
        self.navigationController?.pushViewController(view!, animated: true)
    }
    

    And if you have things properly coded and/or wired up in IB, you should have you UIImageView in the second VC displaying the image.