简体   繁体   中英

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". Those tap gestures are drag from object library and place over my 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. As you can see in the picture, I tried picker.restorationIdentifier but it always returns 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. like artistImage = 1001 and 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

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

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

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