我正在尝试在react native中为ios扩展当前的Picker组件:
https://github.com/facebook/react-native/blob/master/React/Views/RCTPicker.m
https://github.com/facebook/react-native/blob/master/React/Views/RCTPickerManager.m
我想增加监听startDragging和endDragging,startDecelerating和endDecelerating事件的可能性。
我发现there实际上可以通过检查子视图dragging
和decelerating
标志来了解用户当前是否在拖动选择器。很好,但是类似事件/委托的模式将更适合React本机桥接模型。
能否请您建议我如何收听Picker子视图dragging
和decelerating
的值更改?
---编辑---
这是我到目前为止尝试过的:
我编辑了Picker Manager文件并添加了它:
// ...
@implementation RCTPickerManager
RCT_EXPORT_MODULE()
- (UIView *)view
{
//return [RCTPicker new];
RCTPicker* picker = [RCTPicker new];
[self setDelegateForScrollViews:picker];
return picker;
}
// ...
RCT_EXPORT_VIEW_PROPERTY(onScrollChange, RCTBubblingEventBlock)
// ...
-(void)setDelegateForScrollViews:(UIView*)view
{
if([view isKindOfClass:[UIScrollView class]]){
UIScrollView* scroll_view = (UIScrollView*) view;
RCTLogInfo(@"DEBUG setting a delegate on SV");
scroll_view.delegate = self;
}
else {
for(UIView *sub_view in [view subviews]){
[self setDelegateForScrollViews:sub_view];
}
}
}
// UIScrollViewDelegate methods
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
RCTLogInfo(@"DEBUG scrollViewWillBeginDragging");
((RCTPicker*)self.view).onScrollChange(@{ @"state": [NSNumber numberWithBool:YES] });
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView
willDecelerate:(BOOL)decelerate {
RCTLogInfo(@"DEBUG scrollViewDidEndDragging");
((RCTPicker*)self.view).onScrollChange(@{ @"state": [NSNumber numberWithBool:NO] });
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
RCTLogInfo(@"DEBUG scrollViewDidEndDecelerating");
// TODO
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
RCTLogInfo(@"DEBUG scrollViewDidEndScrollingAnimation");
}
@end
但是当我滚动选择器时,不会引发UIScrollView Delegate方法:(...
有人知道吗?
答案 0 :(得分:0)
尝试将选择器视图子类化,并实现scrollview委托方法。不要忘记致电super
。我不明白您为什么要这么做。这些功能在设计上是隐藏的。您要通过什么来实现?即使您有正当的理由,也应该重新考虑您要尝试做的事情。如果您真的需要知道用户何时滚动选择某项,请使用UITableView并获取其scrollview委托事件。