我的代码中有多个UIPickerView,让我们说用户无意中打开了PickerView,但是想要保持选择相同的行,就像用户触摸已经选择的行时一样,选择器隐藏?
我试过这个:UIPicker detect tap / touch on currently selected row,但我无法让它发挥作用。
---编辑---
我会尝试更具体。我是编程和Swift语言的初学者。
这是我的ViewController.swift
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var colorLabel: UILabel!
@IBOutlet weak var sizeLabel: UILabel!
@IBOutlet weak var colorButton: UIButton!
@IBOutlet weak var sizeButton: UIButton!
@IBOutlet weak var resultButton: UIButton!
@IBOutlet weak var colorPickerView: UIPickerView! = UIPickerView()
@IBOutlet weak var sizePickerView: UIPickerView! = UIPickerView()
var colorPickerData = ["Blue", "Red"]
var sizePickerData = ["Small", "Big"]
var descri = String()
var resultadoImagem = UIImage()
override func viewDidLoad() {
super.viewDidLoad()
colorPickerView.isHidden = true
sizePickerView.isHidden = true
self.colorPickerView.delegate = self
self.colorPickerView.dataSource = self
self.sizePickerView.delegate = self
self.sizePickerView.dataSource = self
colorLabel.text = colorPickerData[0]
sizeLabel.text = sizePickerData[0]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView.tag == 1 {
return colorPickerData.count
} else if pickerView.tag == 2 {
return sizePickerData.count
}
return 0
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView.tag == 1 {
return "\(colorPickerData[row])"
} else if pickerView.tag == 2 {
return "\(sizePickerData[row])"
}
return ""
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView.tag == 1 {
colorLabel.text = colorPickerData[row]
colorPickerView.isHidden = true
} else if pickerView.tag == 2 {
sizeLabel.text = sizePickerData[row]
sizePickerView.isHidden = true
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
colorPickerView.isHidden = true
sizePickerView.isHidden = true
}
@IBAction func colorButton(_ sender: Any) {
colorPickerView.isHidden = !colorPickerView.isHidden
}
@IBAction func sizeButton(_ sender: Any) {
sizePickerView.isHidden = !sizePickerView.isHidden
}
@IBAction func resultButton(_ sender: Any) {
if (colorLabel.text == "Blue")
&& (sizeLabel.text == "Big") { resultadoImagem = UIImage(named: "blue-big.png")!; descri = "BLUE"}
else if (colorLabel.text == "Blue")
&& (sizeLabel.text == "Small") { resultadoImagem = UIImage(named: "blue-small.png)!; descri = "BLUE"}
else if (colorLabel.text == "Red")
&& (sizeLabel.text == "Big") { resultadoImagem = UIImage(named: "red-big.png")!; descri = "RED"}
else if (colorLabel.text == "Red")
&& (sizeLabel.text == "Small") { resultadoImagem = UIImage(named: "red-small.png")!; descri = "RED"}
self.performSegue(withIdentifier: "resultSegue", sender: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "resultSegue" {
let DestViewController : SecondViewController = segue.destination as! SecondViewController
DestViewController.descText = descri
DestViewController.resultPhoto = resultadoImagem
}
}
}
这段代码效果很好,当我点击“结果”按钮时,它会转到SecondViewController并显示照片和描述。
我的难点在于,colorPickerView会打开并选择数据“Blue”,就像sizerPickerView默认打开数据“Small”一样。我希望当用户触摸已经选择的数据时,pickerView会关闭/隐藏。
答案 0 :(得分:1)
使类符合UIGestureRecognizerDelegate
协议
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UIGestureRecognizerDelegate {
// ...
}
在UITapGestureRecognizer
colorPickerView
let tap = UITapGestureRecognizer(target: self, action: #selector(self.tapAction(_:)))
tap.cancelsTouchesInView = false
tap.delegate = self
colorPickerView.addGestureRecognizer(tap)
实施UIGestureRecognizerDelegate
方法
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
最后实施tapAction
func tapAction(_ tapRecognizer:UITapGestureRecognizer) {
if tapRecognizer.state == .ended {
let rowHeight : CGFloat = colorPickerView.rowSize(forComponent: 0).height
let selectedRowFrame: CGRect = colorPickerView.bounds.insetBy(dx: 0.0, dy: (colorPickerView.frame.height - rowHeight) / 2.0)
let userTappedOnSelectedRow = selectedRowFrame.contains(tapRecognizer.location(in: colorPickerView))
if (userTappedOnSelectedRow){
let selectedRow = colorPickerView.selectedRow(inComponent: 0)
//do whatever you want here
}
}
}
使用sizerPickerView
复制步骤2和4以扩展功能。