如何限制UIPickerView组件 [英] How to restrict UIPickerView Component
问题描述
如何限制UIPickerView
组件滚动.
我在UIPickeView
中有两个组件,第二个组件结束滚动,允许用户触摸第一个组件以更改其中的值.
I have two components in UIPickeView
one second component ends scroll allows user to touch the first component to change the value in it.
如何限制用户触摸第一个组件,直到第二个组件滚动补充为止.
推荐答案
使用此扩展名,您可以检查UIPickerView是否为是否滚动.取决于滚动,您可以启用/禁用与UIPickerView的交互.
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是否检测滚动.
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.
这篇关于如何限制UIPickerView组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!