简体   繁体   中英

Unable to trigger Tap Gesture Identifier for 2 Images on the same View Controller

I have assigned 2 different tap gesture identifiers to my 2 images in my View Controller. I want access and assign them different images when tapped. I have written the following code and it works only for one image. But, I am not sure how can assign it to two different images. I have even assigned the two images different tags, but, not sure how to use them. Any help will highly appreciated! Thanks!

import UIKit

class EleventhViewController: UIViewController { 

@IBOutlet weak var person1ImageView: UIImageView!

@IBOutlet weak var person2ImageView: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()
    person1ImageView.tag = 1
    person2ImageView.tag = 2

}

extension EleventhViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {

//This is the tap gesture added on my UIImageView.

@IBAction func didTapOnImageView(sender: UITapGestureRecognizer) {
    //call Alert function
    self.showAlert()
}

//Show alert to selected the media source type.
private func showAlert() {

    let alert = UIAlertController(title: "Image Selection", message: "From where you want to pick this image?", preferredStyle: .actionSheet)
    alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: {(action: UIAlertAction) in
        self.getImage(fromSourceType: .camera)
    }))
    alert.addAction(UIAlertAction(title: "Photo Album", style: .default, handler: {(action: UIAlertAction) in
        self.getImage(fromSourceType: .photoLibrary)
    }))
    alert.addAction(UIAlertAction(title: "Cancel", style: .destructive, handler: nil))
    self.present(alert, animated: true, completion: nil)
}

//get image from source type
private func getImage(fromSourceType sourceType: UIImagePickerController.SourceType) {

    //Check is source type available
    if UIImagePickerController.isSourceTypeAvailable(sourceType) {

        let imagePickerController = UIImagePickerController()
        imagePickerController.delegate = self
        imagePickerController.sourceType = sourceType
        self.present(imagePickerController, animated: true, completion: nil)
    }
}

//MARK:- UIImagePickerViewDelegate.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

    self.dismiss(animated: true) { [weak self] in

        guard let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else { return }
        //Setting image to your image view
        
        self?.person2ImageView.image = image
    }
}

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    picker.dismiss(animated: true, completion: nil)
}

}

You have to create a UITapGestureRecognizer object for every imageView and add it, if you added the same 1 to both images it will trigger only for the last 1

 var last = 0
 @objc func didTapOnImageView(sender: UITapGestureRecognizer) { 
    last = sender.view!.tag
 }
 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM