【问题标题】:UITapGestureRecogniser not workingUITapGestureRecognizer 不工作
【发布时间】:2016-10-11 10:15:52
【问题描述】:

我编写了与 Apple 文档中相同的代码,但是当我单击照片时,照片库没有出现。点击手势识别器可能无法正常工作。

import UIKit

class ViewController: UIViewController, UITextFieldDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        nameTextField.delegate = self
    }

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

    //MARK: Actions
    @IBAction func setDefaultLabelText(sender: UIButton) {
        nameLabel.text = "default"
    }
    @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 = UIImagePickerControllerSourceType.PhotoLibrary

            // Make sure ViewController is notified when the user picks an image.
            imagePickerController.delegate = self

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

    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: UITextFieldDelegate
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }

    func textFieldDidEndEditing(textField: UITextField) {
        nameLabel.text = textField.text
    }

}

【问题讨论】:

  • 你在哪里添加了手势,你能显示那个代码
  • 请尝试想出一个真正描述问题的问题标题。避免使用“紧急”之类的词,它们在这里不太受欢迎。
  • 好吧抱歉这是我第一次

标签: ios swift uitapgesturerecognizer


【解决方案1】:

检查一次你是否为你的Imageview启用了userInteraction属性,默认是false,我们需要手动启用,喜欢

photoImageView.userInteractionEnabled = true

例如

override func viewDidLoad(){
{
    super.viewDidLoad()

    let tapGestureRecognizer = UITapGestureRecognizer(target:self, action:Selector("imageTapped:"))
    photoImageView.userInteractionEnabled = true
    photoImageView.addGestureRecognizer(tapGestureRecognizer)
}

调用你的方法

func imageTapped(img: AnyObject)
{
    // Your action 
}

【讨论】:

  • 错误信息“预期声明
  • 谢谢它的工作我这样做了:-
  • 覆盖 func viewDidLoad() { super.viewDidLoad() photoImageView.userInteractionEnabled = true }
  • @ShalinGadhavi - 很高兴听到
  • 这也解决了我的教程问题,但我仍然很困惑为什么在调用 viewDidLoad() 时 userInteractionEnabled 的默认值为 false。我已经检查以启用故事板编辑器中图像视图的“启用用户交互”字段,并且运行时的值仍然是错误的......结果如果父视图(在这种情况下为堆叠视图)具有“用户已启用交互”设置为 false,它也适用于其子级:stackoverflow.com/questions/5887305/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-15
  • 2016-09-25
相关资源
最近更新 更多