【问题标题】:UITapGestureRecognizer Action on UIImageView in swiftswift中UIImageView上的UITapGestureRecognizer动作
【发布时间】:2015-12-28 12:44:17
【问题描述】:

我很难弄清楚我的 UITapGestureRecognizer 出了什么问题,我在动作开始时放置了断点,但它永远不会被调用。我正在按照此处显示的示例进行操作。

https://developer.apple.com/library/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/Lesson4.html

我将 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 上有一个名为 User Interaction Enabled 的复选框。如果您选中此项,它将允许从您的 UITapGestureRecognizer 触发触摸事件。另一种解决方案是在下面的答案中说明的代码中执行此操作。

【问题讨论】:

  • 图像照片控制。 userInteractionEnabled = true ;在 ViewDidLoad 中设置
  • 哇...这完全解决了它。我不明白在未设置的演示代码中。为什么我的应用程序不同?您也可以将其发布为答案,我可以接受。
  • 我已经添加了答案并解释了原因

标签: ios xcode swift uiimageview uitapgesturerecognizer


【解决方案1】:

//在ViewDidLoad中设置

imagePhotoControl.userInteractionEnabled = true ;

【讨论】:

  • 默认情况下imageView的userInterAction是禁用的,因为imageView主要用于显示图像。你必须将它的userInterActionEnabled设置为true才能接收到它的动作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-18
  • 1970-01-01
  • 1970-01-01
  • 2011-12-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多