[英]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.我在我的视图 Controller 中为我的 2 个图像分配了 2 个不同的点击手势标识符。我想要访问并在点击时为它们分配不同的图像。 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您必须为每个 imageView 创建一个
UITapGestureRecognizer
object 并添加它,如果您将相同的 1 添加到两个图像,它将仅触发最后一个 1
var last = 0
@objc func didTapOnImageView(sender: UITapGestureRecognizer) {
last = sender.view!.tag
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.