![](/img/trans.png)
[英]Swift: UITapGestureRecognizer for uiimageview terminate app in custome view
[英]UITapGestureRecognizer Action on UIImageView in swift
我很難確定我的UITapGestureRecognizer出了什么問題,我在操作開始時就已經設置了斷點,但從未調用過它。 我正在遵循此處顯示的示例。
我將UIImageView連接到這樣的輕擊手勢。
然后,我像這樣設置了UITapGestureRecognizer。
最后,我的代碼幾乎是演示代碼的精確副本。
import UIKit
class ViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate{
//MARK: Properties
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var mealNameLabel: UILabel!
@IBOutlet weak var imagePhotoControl: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
nameTextField.delegate = self;
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//MARK: UITextFieldDelegate
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder();
return true;
}
func textFieldDidEndEditing(textField: UITextField) {
mealNameLabel.text = textField.text;
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
// Dismiss the picker if the user canceled.
dismissViewControllerAnimated(true, completion: nil)
}
// MARK: Actions
@IBAction func selectImageFromLibraryAlso(sender: UITapGestureRecognizer) {
// Hide the keyboard.
nameTextField.resignFirstResponder()
// UIImagePickerController is a view controller that lets a user pick media from their photo library.
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
presentViewController(imagePickerController, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
// The info dictionary contains multiple representations of the image, and this uses the original.
let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
// Set photoImageView to display the selected image.
imagePhotoControl.image = selectedImage
// Dismiss the picker.
dismissViewControllerAnimated(true, completion: nil)
}
@IBAction func setDefaultLabelText(sender: UIButton) {
mealNameLabel.text = "Default Text"
}
}
我只是不明白為什么當我點擊圖像時動作永遠不會被觸發。 我什至下載了演示代碼,並且按預期工作。 我確實給我的項目Food Tracker命名了空格,這可能是問題嗎? 我基本上是在發瘋,試圖弄清楚為什么事件沒有被解雇。 我准備在項目和演示項目之間進行文件夾比較。
答案:事實證明,UIImageView的“屬性”檢查器中有一個復選框,稱為“啟用用戶交互”。 如果選中此選項,它將允許從UITapGestureRecognizer觸發觸摸事件。 另一個解決方案是按照以下答案中所述的代碼進行操作。
//在ViewDidLoad中設置
imagePhotoControl.userInteractionEnabled = true ;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.