【问题标题】:React Native iOS Native View send event with proper reactTag使用正确的 reactTag React Native iOS Native View 发送事件
【发布时间】:2016-07-01 23:41:30
【问题描述】:

我有一个 iOS 原生视图,它是一个带有地图视图和 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];
}

【问题讨论】:

    标签: ios objective-c uiview react-native


    【解决方案1】:

    如果你在 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}/>
    

    【讨论】:

      猜你喜欢
      • 2022-12-22
      • 1970-01-01
      • 2016-05-13
      • 1970-01-01
      • 2021-10-13
      • 1970-01-01
      • 2021-08-14
      • 2016-05-19
      • 1970-01-01
      相关资源
      最近更新 更多