在点击手势时显示 UIPickerView

Show UIPickerView on Gesture Tapped

当我点击文本字段时,我当前的应用程序可以运行,它会弹出 UIPickerView,但是如果我点击图像本身(手势放在图像上)会怎样?

class SomeVC: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
  @IBOutlet weak var inputLabel: UITextField!
  @IBOutlet weak var laImageGestureTapped: UITapGestureRecognizer!

  let demoPicker = UIPickerView()

  // viewDidLoad()
  inputLabel.inputView = demoPicker // All is well with this.

  // How to open the UIPickerView with laImageGestureTapped?

  // I have omitted the required functions for: numberOfRowsInComponent, viewForRow, didSelectRow, numberOfComponents etc
}

searching with the correct words吗?

我只想在点击图像时显示选择器。我不担心 didSelectRow 因为会有一个隐藏的标签来做 x、y 和 z。

如果这个问题已经被问过并回答过,请指导我。谢谢

这是在图像上放置清晰文本字段的替代方法:

  1. 创建一个按钮,将您的图像指定为其背景图像属性
  2. 初始化您的 pickerView,使其框架离开屏幕
  3. 为您的按钮创建一个 IBAction,将 pickerView 调用到屏幕上并创建点击手势并添加您的视图
  4. 创建点按手势在触发时将调用的方法,这会将您的 pickerView 送回屏幕

相关代码如下:

class VC: UIViewController {
    var pickerView = UIPickerView()

    override func viewDidLoad() {
        super.viewDidLoad()
    ...
        pickerView = UIPickerView(frame: CGRect(x: 0, y: self.view.bounds.height, width: self.view.bounds.width, height: 100)) //Step 2

    }

    @IBAction func buttonPressed(sender: UIButton){ //Step 3

        UIView.animate(withDuration: 0.3, animations: {
            self.pickerView.frame = CGRect(x: 0, y: self.view.bounds.size.height - self.pickerView.bounds.size.height, width: self.pickerView.bounds.size.width, height: self.pickerView.bounds.size.height)
    })
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(doneWithPickerView))
        view.addGestureRecognizer(tapGesture)

    }

    func doneWithPickerView() { //Step 4

        UIView.animate(withDuration: 0.3, animations: {
            self.pickerView.frame = CGRect(x: 0, y: self.view.bounds.size.height, width: self.pickerView.bounds.size.width, height: self.pickerView.bounds.size.height)
        })
    }
}

我认为通常最好不要使用不可见视图,因为它们会给您以后带来麻烦。希望这有帮助。