How to restrict UIPickerView Component

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 05:56:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!