簡體   English   中英

如何識別調用哪個手勢識別器?

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

我有2個UIImageView,分別名為“ artistImage”和“ albumImage”,每個UIImageView包含1個輕擊手勢,所有手勢都連接到1個@IBAction,名為“ 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方法中將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