检查按下了哪个手势识别器

时间:2019-01-07 18:29:12

标签: ios swift image uigesturerecognizer

我将gestureRecognizerUIImageView一起使用。我有两个UIImageView。我需要同时在UIImageView中上传图片。为了打开UIImagePickerController(),我在两个gestureRecognizer上都使用了UIImageView

所以我需要在UIImageView中的不同func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])中上传图片

如何从图书馆上传图片?

这是我的代码:

@IBOutlet weak var mainPhotoImageView: UIImageView!
@IBOutlet weak var addImageView: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()

    configureGestureRecognizer()

}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    let chosenImage = info[UIImagePickerControllerEditedImage] as? UIImage
    if chosenImage != nil {
        logoImageView.image = chosenImage
        dismiss(animated: true, completion: nil)
    }
}

@objc func addLogoHall(_ sender: AnyObject) {
    let picker = UIImagePickerController()
    picker.delegate = self
    picker.allowsEditing = true
    picker.sourceType = .photoLibrary
    present(picker, animated: true, completion: nil)
}

@objc func addMainPhoto(_ sender: AnyObject) {
    let picker = UIImagePickerController()
    picker.delegate = self
    picker.allowsEditing = true
    picker.sourceType = .photoLibrary
    present(picker, animated: true, completion: nil)
}

func configureGestureRecognizer() {
    let addHallLogo = UITapGestureRecognizer(target: self, action: #selector(AddPhotoAndLogoViewController.addLogoHall(_:)))
    logoImageView.isUserInteractionEnabled = true
    logoImageView.addGestureRecognizer(addHallLogo)
    let addMainPhotoImage = UITapGestureRecognizer(target: self, action: #selector(AddPhotoAndLogoViewController.addMainPhoto(_:)))
    mainPhotoImageView.isUserInteractionEnabled = true
    mainPhotoImageView.addGestureRecognizer(addMainPhotoImage)
}

因此在func imagePickerController中,我需要检测使用了哪个gestureRecognizer。也许还有其他机会这样做?

2 个答案:

答案 0 :(得分:2)

您可以通过以下方式为选择器添加差异标签:

@objc func addLogoHall(_ sender: AnyObject) {
    let picker = UIImagePickerController()
    picker.delegate = self
    picker.allowsEditing = true
    picker.sourceType = .photoLibrary
    picker.view.tag = 1
    present(picker, animated: true, completion: nil)
}

@objc func addMainPhoto(_ sender: AnyObject) {
    let picker = UIImagePickerController()
    picker.delegate = self
    picker.allowsEditing = true
    picker.view.tag = 2
    picker.sourceType = .photoLibrary
    present(picker, animated: true, completion: nil)
}

然后您可以通过这种方式标识此标签:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    let chosenImage = info[UIImagePickerControllerEditedImage] as? UIImage
    if chosenImage != nil {
       if(picker.view.tag == 1) {
          logoImageView.image = chosenImage
        } else {
          mainImageView.image = chosenImage
        }

        dismiss(animated: true, completion: nil)
    }
}

答案 1 :(得分:0)

尽管Jogendar的回答很好,但我看到了一个陷阱。随着视图控制器文件中图像视图实例数量的增加,if-else也将增加。这可能会变得凌乱并且不必要地很快变大。我可能会建议其他解决方案:

class ImageSelectionController: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    weak var delegate: ImageSelectionControllerDelegate?
    private weak var imageView: UIImageView?
    private weak var presentingViewController: UIViewController?

    // MARK: - Initialization
    init(withImageView imageView: UIImageView?, andPresentingViewController viewController: UIViewController?) {
        self.imageView = imageView
        self.presentingViewController = viewController
    }

    // MARK: - Public
    func presentImagePickerController(withConfigurationHandler handler: ((UIImagePickerController) -> Void)? = nil) -> Void {
        let picker = UIImagePickerController()
        picker.allowsEditing = true
        picker.sourceType = .photoLibrary
        handler?(picker)
        picker.delegate = self
    }

    // MARK: - UIImagePickerController
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        let chosenImage = info[UIImagePickerControllerEditedImage] as? UIImage
        self.imageView?.image = chosenImage
        self.delegate?.controller(self, didFinishPickingImage: chosenImage, forImageView: self.imageView)
        picker.dismiss(animated: true, completion: nil)
    }

}

protocol ImageSelectionControllerDelegate: AnyObject {
    func controller(_ controller: ImageSelectionController, didFinishPickingImage image: UIImage?, forImageView: UIImageView?) -> Void
}

// ----您的View Controller ---- //

@IBOutlet weak var logoImageView: UIImageView?
@IBOutlet weak var mainPhotoImageView: UIImageView?

override func viewDidLoad() {
    super.viewDidLoad()

    configureGestureRecognizer()

}

@objc func addLogoHall(_ sender: AnyObject) {
    let controller = ImageSelectionController.init(withImageView: self.logoImageView, andPresentingViewController: self)
    controller.delegate = self
    controller.presentImagePickerController()
}

@objc func addMainPhoto(_ sender: AnyObject) {
    let controller = ImageSelectionController.init(withImageView: self.logoImageView, andPresentingViewController: self)
    controller.presentImagePickerController { (picker) in
        picker.sourceType = .camera
    }
}

// MARK: - ImageSelectionControllerDelegate
func controller(_ controller: ImageSelectionController, didFinishPickingImage image: UIImage?, forImageView: UIImageView?) {
    print("image picked!")
}

通过这种方式,您可以将所有与UIImagePickerController相关的代码放在一个地方,并且不需要任何if-else情况。再次,没有正确或错误的答案,有人可能还会在我的解决方案中发现其他缺陷。只是您要如何构造代码的问题。

编码愉快!