gpt4 book ai didi

ios - 如何识别调用了哪个点击手势识别器?

转载 作者:行者123 更新时间:2023-11-28 15:07:17 24 4
gpt4 key购买 nike

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

Storyboard - 代码 enter image description here

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

我的问题是我不知道如何识别哪个 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)
}

不胜感激!!

最佳答案

  • 将标签添加到 Storyboard 中的两个 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 变量检查用户点击了图片上的哪些内容

关于ios - 如何识别调用了哪个点击手势识别器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48260451/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com