2

我正在慢慢地通过 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"
    }

}
4

4 回答 4

5

我发现解决该问题的最简单方法是在图像视图的属性编辑器中单击“启用用户交互”复选框。截屏

于 2016-08-01T18:54:35.890 回答
3

您的代码非常好。问题出在故事板中的 UIImageView 上。该user interaction选项未选中。

所以你只需要在故事板中启用这个选项。

  1. 去你的故事板。

  2. 选择您的 UIImageView(您将其命名为 photoimageview)

  3. 现在在“Attribute Inspector”中寻找user interaction enabled,应该是“checked”</p>

  4. 现在运行你的项目,它将工作。

在此处输入图像描述

于 2017-09-19T00:37:15.140 回答
3

喜欢

原因,默认情况下UIImageview userInteraction为false,所以需要手动开启

步骤1

let tapGesture = UITapGestureRecognizer(target:self, action:Selector("imagePressed"))
photoImageView.userInteractionEnabled = true // this line is important
photoImageView.addGestureRecognizer(tapGesture)

第2步

func imagePressed()
{
//do  Your action here  whatever you want 
}
于 2016-01-25T04:49:29.533 回答
0

您在哪里添加了UITapGestureRecognizer图像视图?如果您在 Storyboard 中添加它,是否将其连接到选择器selectImageFromPhotoLibrary:

于 2016-01-25T05:23:18.037 回答