简体   繁体   English

如何识别调用哪个手势识别器?

[英]how to identify which tap gesture recognizer is called?

I have 2 UIImageView, named "artistImage" and "albumImage", each one contains 1 tap gesture and all the gestures are connected to 1 @IBAction, named "artistImageTap". 我有2个UIImageView,分别名为“ artistImage”和“ albumImage”,每个UIImageView包含1个轻击手势,所有手势都连接到1个@IBAction,名为“ artistImageTap”。 Those tap gestures are drag from object library and place over my ImageView. 这些点击手势是从对象库中拖动并放置在我的ImageView上的。

Storyboard - Code 故事板-代码 在此处输入图片说明

My app have a list of artists, albums and songs, when a song is tapped, the app moves to this view and display its details. 我的应用程序具有艺术家,专辑和歌曲的列表,当点击歌曲时,该应用程序将移至该视图并显示其详细信息。 If I click the add button, the app moves to this view but this time all the text fields are editable, the images are default and user can tap them to select image from library to create new song. 如果单击添加按钮,应用程序将移至该视图,但是这次所有文本字段都是可编辑的,图像为默认图像,用户可以点击它们从库中选择图像以创建新歌曲。

My problem is I don't know how to identify which UIImageView is tapped. 我的问题是我不知道如何识别哪个UIImageView被点击。 As you can see in the picture, I tried picker.restorationIdentifier but it always returns nil. 如您在图片中看到的,我尝试了picker.restorationIdentifier,但它始终返回nil。

@IBAction func artistImageTap(_ sender: UITapGestureRecognizer) {
    let imagePickerController = UIImagePickerController()

    // Only allow photos to be picked, not taken.
    imagePickerController.sourceType = .photoLibrary
    // Make sure ViewController is notified when the user picks an image.
    imagePickerController.delegate = self
    present(imagePickerController, animated: true, completion: nil)
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

    // The info dictionary may contain multiple representations of the image. You want to use the original.
    guard let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage else {
        fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
    }

    // Set photoImageView to display the selected image.
    if picker.restorationIdentifier == "artistImage" {
        artistImage.image = selectedImage
    } else {
        albumImage.image = selectedImage
    }
    // Dismiss the picker.
    dismiss(animated: true, completion: nil)
}

Every help is appreciated!! 感谢您的帮助!!

  • Add tag to both UIImageView from storyboard. 从故事板向两个UIImageView添加标签。 like artistImage = 1001 and albumImage = 1002 像artistImage = 1001和albumImage = 1002

     @IBAction func artistImageTap(_ sender: UITapGestureRecognizer) { if sender.view?.tag == 1001 { selectedTag = 1001 } else if sender.view?.tag == 1002 { selectedTag = 1002 } let imagePickerController = UIImagePickerController() // Only allow photos to be picked, not taken. imagePickerController.sourceType = .photoLibrary // Make sure ViewController is notified when the user picks an image. imagePickerController.delegate = self present(imagePickerController, animated: true, completion: nil) } 
  • store selected tag in one variable. 将所选标签存储在一个变量中。

  • now you can check which on image user have tapped with selectedTag variable 现在您可以检查哪个图像用户点击了selectedTag变量

First of all set tags for tap gestures when you are applying it on images. 首先,将其应用到图像时,为点击手势设置标签。

tapGestureArtistImage.tag = 0;
tapGestureAlbumImage.tag = 1;

then send tap gesture as parameters in artistImageTap method 然后在artistImageTap方法中将artistImageTap手势作为参数发送

- (void) artistImageTap:(UITapGestureRecognizer*)sender
{
  if(sender.tag == 0)
  {
    // artistImage tapped
  }
  else if (sender.tag == 1)
  {
    // albumImage tapped
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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