加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

React Native iOS Native View使用适当的reactTag发送事件

发布时间:2020-12-15 05:04:56 所属栏目:百科 来源:网络整理
导读:我有一个iOS Native View,它是一个带有地图视图和UIView的UIView.地图有一个名为’regionDidChangeAnimated’的事件,我想将事件发送到React Native.但reactTag不对. - (UIView *)view{ CGRect screenRect = [[UIScreen mainScreen] bounds]; UIView *frameVi
我有一个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,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),}
};

还有一个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}/>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读