我有一个 iOS native View ,它是一个带有 map View 的 UIView 和一个 UIView。该 map 有一个名为“regionDidChangeAnimated”的事件,我想将事件发送到 React Native。但是 reactTag 不对。
- (UIView *)view
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
UIView *frameView = [[UIView alloc] initWithFrame:screenRect];
CGRect frameRect = frameView.bounds;
MAMapView *mapView;
mapView = [[MAMapView alloc] initWithFrame:frameRect];
self.mapview = mapView;
mapView.delegate = self;
[frameView addSubview:mapView];
RCTFixedPin* pin = [[RCTFixedPin alloc] initWithFrame:CGRectMake(0, 0, screenRect.size.width, 260)];
pin.userInteractionEnabled = NO;
[frameView addSubview:pin];
return frameView;
}
- (void)mapView:(MAMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
if (self.dragging) {
self.dragging = NO;
}
MACoordinateRegion region = mapView.region;
NSDictionary *event = @{
***@"target": ,***
@"region": @{
@"latitude": @(region.center.latitude),
@"longitude": @(region.center.longitude),
@"latitudeDelta": @(region.span.latitudeDelta),
@"longitudeDelta": @(region.span.longitudeDelta),
}
};
[self.bridge.eventDispatcher sendInputEventWithName:@"topChange" body:event];
}
最佳答案
如果您查看他们已实现的原生 View 的 react-native 代码:
https://github.com/facebook/react-native/tree/master/React/Views
看起来文档已经过时了,而不是使用:
[self.bridge.eventDispatcher sendInputEventWithName...
您应该执行以下操作:
@property (nonatomic, copy) RCTBubblingEventBlock onTopChange;
self.onTopChange(@{
@"region": @{
@"latitude": @(region.center.latitude),
@"longitude": @(region.center.longitude),
@"latitudeDelta": @(region.span.latitudeDelta),
@"longitudeDelta": @(region.span.longitudeDelta),
}
};
还有一个 RCTDirectEventBlock
我不确定它和 RCTBublingEventBlock
有什么区别
查看 RCTComponent.m
第 160-169 行,它应该会自动为您处理目标设置:
// Special case for event handlers
__weak RCTViewManager *weakManager = _manager;
setterBlock = ^(id target, __unused id source, id json) {
__weak id<RCTComponent> weakTarget = target;
((void (*)(id, SEL, id))objc_msgSend)(target, setter, [RCTConvert BOOL:json] ? ^(NSDictionary *body) {
body = [NSMutableDictionary dictionaryWithDictionary:body];
((NSMutableDictionary *)body)[@"target"] = weakTarget.reactTag;
[weakManager.bridge.eventDispatcher sendInputEventWithName:RCTNormalizeInputEventName(name) body:body];
} : nil);
};
还要确保在 Manager 类中添加:
RCT_EXPORT_VIEW_PROPERTY(onTopChange, RCTBubblingEventBlock)
并且不要忘记在您的 JSX 中实际连接事件:
<MyComponent onTopChange={this.handleOnTopChange}/>
关于ios - React Native iOS Native View 使用适当的 reactTag 发送事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36029820/