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
祝你好运:]