簡體   English   中英

使用適當的reactTag響應本機iOS本機視圖發送事件

[英]React Native iOS Native View send event with proper reactTag

我有一個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];
}

如果您在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行,它應該為您自動處理目標設置:

// 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}/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM