Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何识别调用了哪个轻拍手势识别器?_Ios_Swift_Xcode_Uiimageview_Uitapgesturerecognizer - Fatal编程技术网

Ios 如何识别调用了哪个轻拍手势识别器?

Ios 如何识别调用了哪个轻拍手势识别器?,ios,swift,xcode,uiimageview,uitapgesturerecognizer,Ios,Swift,Xcode,Uiimageview,Uitapgesturerecognizer,我有2个UIImageView,名为artistImage和albumImage,每个都包含1个点击手势,所有手势都连接到1个@iAction,名为artistImageTap。这些点击手势从对象库中拖动并放置在我的ImageView上 故事板-代码 我的应用程序有艺术家、专辑和歌曲的列表,当点击一首歌曲时,应用程序将移动到此视图并显示其详细信息。如果我单击添加按钮,应用程序将移动到此视图,但这次所有文本字段都是可编辑的,图像是默认的,用户可以点击它们从库中选择图像以创建新歌曲 我的问题是我不

我有2个UIImageView,名为artistImage和albumImage,每个都包含1个点击手势,所有手势都连接到1个@iAction,名为artistImageTap。这些点击手势从对象库中拖动并放置在我的ImageView上

故事板-代码

我的应用程序有艺术家、专辑和歌曲的列表,当点击一首歌曲时,应用程序将移动到此视图并显示其详细信息。如果我单击添加按钮,应用程序将移动到此视图,但这次所有文本字段都是可编辑的,图像是默认的,用户可以点击它们从库中选择图像以创建新歌曲

我的问题是我不知道如何识别哪个UIImageView被点击。如图所示,我尝试了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)
}
感谢您的帮助

从情节提要向两个UIImageView添加标记。 比如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)
} 
将所选标记存储在一个变量中

现在,您可以检查用户使用selectedTag变量点击的图像


首先,在图像上应用轻触手势时,设置轻触手势的标记

tapGestureArtistImage.tag = 0;
tapGestureAlbumImage.tag = 1;
然后在artistImageTap方法中发送敲击手势作为参数


甚至没有人知道您是如何创建手势识别器的。请使用sender.view并在UIImageView中设置标记,然后您可以将sender.view.tag用作identifier@ElTomato我通过从对象库拖动来添加它们。对不起,没提那件事@ReinierMelian我试图为我的视图、滚动视图、内容视图甚至图像视图设置标记,但在我使用sender.view.taguse debugprintssender.view时,它们都没有出现,请告诉我@hell2809找到了什么打印,与您的类似!谢谢@毫无疑问,这个问题是有帮助的。但是玩tag是不好的做法。所以不要像创建iAction那样创建点击手势对象的IBOutlet。并将该IBOutlet与sender对象进行比较,以更加清楚:
- (void) artistImageTap:(UITapGestureRecognizer*)sender
{
  if(sender.tag == 0)
  {
    // artistImage tapped
  }
  else if (sender.tag == 1)
  {
    // albumImage tapped
  }
}