1

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.

Bhavin Ramani
  • 3,221
  • 5
  • 30
  • 41
kiran
  • 4,285
  • 7
  • 53
  • 98

1 Answers1

0

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.

Jay Patel
  • 2,642
  • 2
  • 18
  • 40