iOS - 在图像视图上点击手势问题
Posted
技术标签:
【中文标题】iOS - 在图像视图上点击手势问题【英文标题】:iOS - Tap Gesture issue on image view 【发布时间】:2016-01-25 04:09:01 【问题描述】:我正在慢慢完成 Apple 的 Swift 学习开发,但我的点击手势一直遇到问题。我重新创建了项目编号,所有结果都相同。
我在图像视图中添加了一个点击手势,这应该会从我的计算机打开照片库。什么都没有发生。
当我从 Apple 下载并运行示例文件时,一切正常。当我将代码从 Apple 复制并粘贴到我的时,再次没有任何反应。我已经经历了我能做的所有事情,但我觉得我错过了一些东西。
这是我的代码。苹果的代码如下:
import UIKit
class ViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate
// MARK: Properties
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var mealNameLabel: UILabel!
@IBOutlet weak var photoImageView: UIImageView!
override func viewDidLoad()
super.viewDidLoad()
// handle the text fields user input through delegate callbacks
nameTextField.delegate = self
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
// MARK: UITextFieldDelegate
func textFieldShouldReturn(textField: UITextField) -> Bool
// Hide the Keyboard
textField.resignFirstResponder()
return true
func textFieldDidEndEditing(textField: UITextField)
mealNameLabel.text = textField.text
// MARK: UIImagePickerControllerDelegate
func imagePickerControllerDidCancel(picker: UIImagePickerController)
// dismiss the picker if user cancels
dismissViewControllerAnimated(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 the photoviewimage to be the selected image
photoImageView.image = selectedImage
// Dismiss the picker
dismissViewControllerAnimated(true, completion: nil)
// MARK: Actions
@IBAction func selectImageFromPhotoLibrary(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 pictures to be selected and not taken
imagePickerController.sourceType = .PhotoLibrary
// make sure the viewcontroller is notified when the user selects an image
imagePickerController.delegate = self
presentViewController(imagePickerController, animated: true, completion: nil)
@IBAction func setDefaultLabelText(sender: UIButton)
mealNameLabel.text = "Default Text"
这是苹果的代码:
import UIKit
class ViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate
// MARK: Properties
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var mealNameLabel: UILabel!
@IBOutlet weak var photoImageView: UIImageView!
override func viewDidLoad()
super.viewDidLoad()
// Handle the text field’s user input through delegate callbacks.
nameTextField.delegate = self
// MARK: UITextFieldDelegate
func textFieldShouldReturn(textField: UITextField) -> Bool
// Hide the keyboard.
textField.resignFirstResponder()
return true
func textFieldDidEndEditing(textField: UITextField)
mealNameLabel.text = textField.text
// MARK: UIImagePickerControllerDelegate
func imagePickerControllerDidCancel(picker: UIImagePickerController)
// Dismiss the picker if the user canceled.
dismissViewControllerAnimated(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.
photoImageView.image = selectedImage
// Dismiss the picker.
dismissViewControllerAnimated(true, completion: nil)
// MARK: Actions
@IBAction func selectImageFromPhotoLibrary(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)
@IBAction func setDefaultLabelText(sender: UIButton)
mealNameLabel.text = "Default Text"
来自 Anbu 的工作代码
import UIKit
class ViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate
// MARK: Properties
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var mealNameLabel: UILabel!
@IBOutlet weak var photoImageView: UIImageView!
override func viewDidLoad()
let tapgesture = UITapGestureRecognizer(target: self, action: Selector("imagepressed"))
photoImageView.userInteractionEnabled = true
photoImageView.addGestureRecognizer(tapgesture)
super.viewDidLoad()
// handle the text fields user input through delegate callbacks
nameTextField.delegate = self
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
// MARK: UITextFieldDelegate
func textFieldShouldReturn(textField: UITextField) -> Bool
// Hide the Keyboard
textField.resignFirstResponder()
return true
func textFieldDidEndEditing(textField: UITextField)
mealNameLabel.text = textField.text
// MARK: UIImagePickerControllerDelegate
func imagePickerControllerDidCancel(picker: UIImagePickerController)
// dismiss the picker if user cancels
dismissViewControllerAnimated(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 the photoviewimage to be the selected image
photoImageView.image = selectedImage
// Dismiss the picker
dismissViewControllerAnimated(true, completion: nil)
// MARK: Actions
func imagepressed ()
nameTextField.resignFirstResponder()
// UIImagePickerController is a view controller that lets a user pick media from their photo library.
let imagePickerController = UIImagePickerController()
// Only Allow pictures to be selected and not taken
imagePickerController.sourceType = .PhotoLibrary
// make sure the viewcontroller is notified when the user selects an image
imagePickerController.delegate = self
presentViewController(imagePickerController, animated: true, completion: nil)
/* @IBAction func selectImageFromPhotoLibrary(sender: UITapGestureRecognizer)
let tapgesture = UITapGestureRecognizer(target: self, action: Selector("imagepressed"))
photoImageView.userInteractionEnabled = true
photoImageView.addGestureRecognizer(tapgesture)
// 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 pictures to be selected and not taken
imagePickerController.sourceType = .PhotoLibrary
// make sure the viewcontroller is notified when the user selects an image
imagePickerController.delegate = self
presentViewController(imagePickerController, animated: true, completion: nil)
*/
@IBAction func setDefaultLabelText(sender: UIButton)
mealNameLabel.text = "Default Text"
【问题讨论】:
点击手势识别器在哪里? 您是否在情节提要/XIB 中为您的视图控制器添加了轻触识别器?如果您还没有将点击手势识别器添加到 storyboard/xib,然后将 action 方法连接到 selectImageFromPhotoLibrary。 点击手势被添加到情节提要到 ImageView 上,然后通过拖放连接到 //Mark: Actions with the UITapGesture 下 【参考方案1】:我发现解决此问题的最简单方法是在图像视图的属性编辑器中单击“启用用户交互”复选框。 Screenshot
【讨论】:
本质上,此错误来自“添加膳食照片”部分中缺少指令 #12:“在属性检查器中,找到交互字段并选中启用用户交互复选框。您需要此功能稍后让用户与图像视图进行交互。” 在将“Tap Gesture Recognizer”控制器与“Image View”控制器连接之前,您必须检查真正的“User Interaction Enabled”。【参考方案2】:喜欢
原因,UIImageview userInteraction默认为false,需要手动开启
第一步
let tapGesture = UITapGestureRecognizer(target:self, action:Selector("imagePressed"))
photoImageView.userInteractionEnabled = true // this line is important
photoImageView.addGestureRecognizer(tapGesture)
第二步
func imagePressed()
//do Your action here whatever you want
【讨论】:
我会在哪里添加这个?这就是我一直在想的(至少沿着这些思路,因为我不想给自己太多的功劳)但苹果开发者并没有在任何地方展示这一点。 在 viewdidload 上调用这个 Andu,我尝试了几个不同的位置来放置代码,但均未成功。请随时像对待我两岁的儿子一样对待我,并让我知道将代码放在哪里。另外,我是否只需复制并粘贴基金 selectuserimagefromlibrary 下的代码并将其粘贴到 func imagepressed() 中? 谢谢安布!我能够让它工作并感谢帮助!我已经添加了上面的工作代码。【参考方案3】:您的代码非常好。问题出在故事板中的 UIImageView 上。 user interaction
选项未选中。
因此,您只需在情节提要中启用此选项即可。
转到您的故事板。
选择您的 UIImageView(您将其命名为 photoimageview)
现在在“属性检查器”中查找user interaction enabled
,它应该被“选中”
现在运行你的项目,它就可以工作了。
【讨论】:
【参考方案4】:您在哪里为图像视图添加了UITapGestureRecognizer
?如果您在 Storyboard 中添加它,您是否将其连接到选择器 selectImageFromPhotoLibrary:
?
【讨论】:
点击手势被添加到情节提要到 ImageView 上,然后通过拖放连接到 //Mark: Actions with the UITapGesture 下以上是关于iOS - 在图像视图上点击手势问题的主要内容,如果未能解决你的问题,请参考以下文章
Xcode iOS UIView 带有点击手势,允许在子视图上输入