检测 UIPickerView 何时开始更改/移动

Detect when UIPickerView starts changing / moving

我正在尝试对 UIPickerView 开始移动的事件做出反应(不是在该行已被选中时)。

我搜索了整个委托方法,none 提供了帮助。我也尝试注册一个通知,但无法弄清楚当用户将手指放在组件上并开始滚动时会发出通知的任何通知。

有什么替代方案吗?

您可以创建 UIPickerView 的自定义 class 并覆盖 hitTest(point:with:)。创建一个协议,您可以通过委托方法将当前选择器发送到您的控制器并绘制任何您喜欢的东西:

protocol CustomPickerViewDelegate: class {
    func didTapped(_ picker: CustomPickerView)
}

class CustomPickerView: UIPickerView {

    weak var myDelegate: CustomPickerViewDelegate?

    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        // Only test for points in your needed view
        if !self.point(inside: point, with: event) {
            return nil
        }

        // Return using CustomPickerViewDelegate the current picker
        // that can be used to determine which one was selected
        myDelegate?.didTapped(self)

        // Call super.hitTest(_: with:)
        return super.hitTest(point, with: event)
    }
}

不要忘记(在您的控制器中:例如 YourViewController):

self.pickerView.myDelegate = self.

创建订阅 CustomPickerViewDelegate 协议的控制器扩展:

extension YourViewController: CustomPickerViewDelegate {
    func didTapped(_ picker: CustomPickerView) {
        // do what you want here
        self.addBorderTo(picker: picker)
    }
}

如果您愿意,可以扩展 UIPickerViewDelegate(请参阅下文如何扩展基础 class 委托)

祝你好运:]