当我点击文本字段时,我当前的应用程序可以工作,它会调出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
}
我是否使用正确的单词进行搜索?
我想要的只是在点击图像时显示选取器。我不担心didSelectRow
因为会有一个隐藏的标签来做 x、y 和 z。
如果这个问题已经被问过并回答了,请指导我。谢谢。
以下是在
图像上放置清晰文本字段的替代方法:
- 创建一个按钮,将图像分配给其背景图像属性
- 初始化选取器视图,使其框架离开屏幕
- 为您的按钮创建一个 IBAction,该按钮在屏幕上调用选取器视图并创建点击手势并添加您的视图
- 创建点击手势在触发时将调用的方法,这会将您的选取器视图从屏幕上移回
以下是相关代码:
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)
})
}
}
我认为一般来说,最好不要使用不可见的视图,因为它们可能会给您以后带来麻烦。希望这有帮助。