问题
How to restrict the UIPickerView
component scrolling.
I have two components in UIPickeView
one second component ends scroll allows user to touch the first component to change the value in it.
How can I restrict the user to touch component one until and unless the second component scrolling complemented.
回答1:
Using this extension, you can check UIPickerView is scrolling or not. Depend on scrolling, you can enable/disable interaction with UIPickerView.
extension UIView {
func isScrolling () -> Bool {
if let scrollView = self as? UIScrollView {
if (scrollView.isDragging || scrollView.isDecelerating) {
return true
}
}
for subview in self.subviews {
if subview.isScrolling() {
return true
}
}
return false
}
}
UIPickerViewDelegate to detect scrolling or not.
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView.isScrolling() {
pickerView.isUserInteractionEnabled = false
} else {
pickerView.isUserInteractionEnabled = true
}
//Use this for reduce lines
//pickerView.isUserInteractionEnabled = !pickerView.isScrolling()
return "Row \(row)"
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
pickerView.isUserInteractionEnabled = true
}
The only problem is you can not touch current scrolling component, until component completed scrolling. HOPE, someone solve this also.
来源:https://stackoverflow.com/questions/38067904/how-to-restrict-uipickerview-component