我有一个iOS Native View,它是一个带有地图视图和UIView的UIView。地图有一个名为'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];
}
答案 0 :(得分:9)
如果您查看它们已实现的本机视图的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
我不确定它与RCTBubblingEventBlock
查看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}/>