有没有像scrollviewDidScroll这样的uipickerview委托方法?
我有一个自定义的 UIPickerview,我不想使用日期选择器。我想实现以下功能:当用户向下/向上滚动小时时,AM/PM 组件会在小时滚动时切换。这意味着我需要在调用 pickerView didSelectRow 之前切换它。有办法做到这一点吗?
谢谢
I have a customized UIPickerview and I do not want to use a datepicker. I want to implement the feature where when a user scrolls down/up the hours, the AM/PM component switches while the hour is scrolling. This means that I need to switch it before pickerView didSelectRow is called. Is there a way to do this?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用以下方法,
以上方法来自 UIPickerViewDelegate,如果用户使用 pickerview 选择任何元素,则会自动触发此方法。
希望对您有帮助。
编辑:
我认为你应该使用以下方法来检测 - 用户滚动的方向?
每当用户向上/向下滚动时,都会自动触发上述方法,这与
UITableView
类似。请记住UIPickerView
使用的是私有的UIPickerTableView
,因此我们无法检测到您想要的滚动方式。让我解释一下在pickerview中检测方向。
例子。可见行为
index_4、index_5、index_6、index_7
。现在,如果用户向下滚动index_8
将被调用。同样,如果用户向上滚动index_3
将被调用。我希望这个技巧能解决你的问题。尽管如此,请让我知道您对此的反馈。
Use following method,
Above method is from UIPickerViewDelegate, If user selects any element using pickerview, this method is automatically triggered.
Hope it helps to you.
Edit :
I think you should use following method for detecting - in which direction user is scrolling ?
whenever user is scrolling up/down above method is triggered automatically which is similar to
UITableView
. RememberUIPickerView
is usingUIPickerTableView
which is private, so we can not detect the scrolling the way you want to have.Let me explain the detecting the direction in pickerview.
Example. Visible rows are
index_4,index_5,index_6,index_7
. Now if user is scrolling downindex_8
will be called. Similarly if user is scrolling to upindex_3
will be called.I hope this trick will solve your problem. Even-though let me know your feedback on this.
有一个技巧可以检测到这一点,但没有委托方法/属性来检测其滚动是否
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
希望这有帮助,顺便说一句,这就是中的解释所有上述答案
There is a trick to detect this but there is no delegate method/ property to detect if its scrolling or not
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
or equivalent methodfunc pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
hope this helps, BTW this is what is explained in all the above mentioned answers
必须使用这两个 UIPickerView 委托方法来处理事件:
DID END SCROLL:
WILL START SCROLLING:
显然除了 UIPickerview 的构建(firstTIME!);所以当你完成构建你的 piker 时,你必须实现一个假的 didendscroll 事件,或者忽略第一个 willstart 滚动可见的 piker 行
have to use this two UIPickerView delegate method for event:
DID END SCROLL:
WILL START SCROLLING:
obviosly except for the building of UIPickerview (firstTIME!); so u have to implement a fake didendscroll event when you finish to build you piker or ignore first willstart scroll for the visible piker row
不幸的是,上述解决方案(在调用 titleForRow 等数据源方法时设置标志并在调用 didSelectRow 时重置标志)很粗略,在许多情况下不起作用。数据源方法可能在许多情况下被调用,其中用户没有滚动 - 例如 UI 更改导致视图布局更改,也可能在 didSelectRow 之后立即调用 - 您无法控制何时调用这些方法。
关于这个特定问题,这些解决方案可能有效,因为这些方法总是在滚动时调用 - 而不仅仅是在滚动时调用。但是,需要注意的是,不要假设用户在这些情况下必须滚动。另外,如果您使用此标志管理状态机(例如在滚动时禁用某些按钮并在之后启用它),您会遇到麻烦。
最后,这仅允许检测滚动(具有上述警告),但它不会告诉您速度或当前值 - 因此很难判断何时切换 AM/PM 标签。
我能够可靠地检测滚动的唯一方法是通过在进行 UI 更改和其他丑陋的黑客时设置的一堆标志(例如在 didSelectRow 之后等待 100 毫秒,然后再次尝试检测滚动,因为我注意到对紧接在 didSelectRow 之后的数据源)。向委托添加 willScroll/didScroll 方法似乎是苹果忽略添加的一个明显的事情。
Unfortunately, the above solutions (setting a flag when data source methods such as titleForRow are called and resetting it when didSelectRow is called) are sketchy and won't work in many cases. The data source methods may be called in many cases where there is no scrolling by the user - such as UI changes resulting in changing view layouts, and also may be called immediately after didSelectRow - you have no control over when these methods are called.
In relation to this particular question, these solutions may work, since these methods are always called when scrolling - just not ONLY when scrolling. However, care needs to be taken not to assume that the user MUST be scrolling in these cases. Also, if you manage a state machine with this flag (like disabling some button when scrolling and enabling it after), you'll get in trouble.
Finally, this only allows detecting a scroll (with the above caveats), but it won't tell you the speed or current values - so it would be difficult to tell when to switch the AM/PM label.
The only way I was able to reliably detect a scroll was through a mess of flags that I set when UI changes are made and other ugly hacks (like waiting 100 ms after a didSelectRow before trying to detect scrolls again, because I noticed calls to the data source immediately after didSelectRow). Adding willScroll/didScroll methods to the delegate seems like an obvious thing Apple neglected to add.
我刚刚找到了一种实现“pickerDidScroll”的方法,它工作得很好。关键点是将KVO添加到行视图中。这是我的代码:
“RatchetScrollUnit”类:
尝试一下
I just found a way to achieve "pickerDidScroll",it work fine. The key point is add KVO to the row view. Here is my code:
The "RatchetScrollUnit" class :
Have a try