在Gesture Tapped上显示UIPickerView

时间:2017-01-13 14:09:53

标签: ios swift cocoa-touch uipickerview

当我点击文字字段时,我的当前应用程序有效,它会调出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 个答案:

答案 0 :(得分:2)

这是在图像上放置明文字段的另一种方法:

  1. 创建一个按钮,将图像分配给其背景图像属性
  2. 使用屏幕框架
  3. 初始化pickerView
  4. 为您的按钮创建一个IBAction,将pickerView调用到屏幕上并创建一个点按手势并添加您的视图
  5. 创建点按手势在触发时会调用的方法,这会将pickerView从屏幕上移回来
  6. 以下是相关代码:

    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)
            })
        }
    }
    

    我认为一般来说,不使用隐形视图会更好,因为它们可能会给您的后来造成麻烦。希望这会有所帮助。