代码之家  ›  专栏  ›  技术社区  ›  Jacobo Koenig

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

  •  0
  • Jacobo Koenig  · 技术社区  · 6 年前

    我正试图对一个 UIPickerView

    我已经搜索了所有委托方法,但没有一种方法有用。我还试图注册一个通知,但当用户将手指放在组件上并开始滚动时,无法找出任何通知。

    你知道还有什么替代方案吗?

    1 回复  |  直到 6 年前
        1
  •  2
  •   Deryck Lucian Red Heart    5 年前

    UIPickerView 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 (请参见下面如何扩展基类委托)

    Extending a delegate from a base class

    祝你好运:]

    推荐文章