UITapGestureRecognizer没有正确触发

时间:2016-10-25 17:13:52

标签: ios swift uitapgesturerecognizer

我是iOS开发的新手,我正在关注Apple开发应用程序的教程,所以请耐心等待。我目前正在使用XCode 8上的Swift 3进行开发

我正在教程中的一个点,我将UITapGestureRecognizer连接到故事板上的图像视图。我编写了代码来实现从库中挑选照片并将其放入图像视图的操作。当我运行模拟并单击图像视图时,没有弹出窗口要求应用程序访问照片或任何内容。以下是我写的代码

import UIKit

class ViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {


// MARK: Properties
@IBOutlet weak var mealNameLabel: UILabel!
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var photoImageView: UIImageView!


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    //  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.
    dismiss(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.
    photoImageView.image = selectedImage

    // Dismiss the picker.
    dismiss(animated: 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

    present(imagePickerController, animated: true, completion: nil)
}

@IBAction func setDefaultLabelText(_ sender: UIButton) {
    mealNameLabel.text = "Default Text"
}

}

这是我单击图像视图之前和之后的控制台输出

2016-10-25 13:11:05.115607 TestApp1[17930:1007384] subsystem: com.apple.UIKit, category: GestureEnvironment, enable_level: 0, persist_level: 0, default_ttl: 1, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 1, privacy_setting: 2, enable_private_data: 0  
2016-10-25 13:11:05.116342 TestApp1[17930:1007384] subsystem: com.apple.UIKit, category: GestureExclusion, enable_level: 0, persist_level: 0, default_ttl: 1, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 1, privacy_setting: 2, enable_private_data: 0

根据以下帖子,我还选择并添加了"隐私 - 照片库使用说明"键入info.plist但也没有骰子。

UIImagePickerController crashes app | Swift3, Xcode8

如何弹出照片库?

1 个答案:

答案 0 :(得分:3)

您是否已将isUserInteractionEnabled启用为 true

self.photoImageView.isUserInteractionEnabled = true

如果你已经设置好的和公平的,那么你可能会错过这一步。同时使用调试器检查selectImageFromPhotoLibrary函数是否在调用photoImageView时调试器是否进入。

要按代码设置Tap Gesture,请在ViewController课程中将其写入。

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(selectImageFromPhotoLibrary))
self.photoImageView.addGestureRecognizer(tapGesture)

func selectImageFromPhotoLibrary(sender:UITapGestureRecognizer) {
    //open photo library code here
}