RN 小白看了都會寫的原生組件

目錄
一、前言
二、iOS原生組件
三、Android原生組件
四、ReactNative整合

一、前言

好好學(xué)習(xí),天天向上,是程序員必備的基本品質(zhì),作為移動開發(fā)者不掌握幾門語言還真不行,比如做iOS開發(fā),需要掌握Object-C和Swift,高級一點(diǎn)的還得會C和C++,混合開發(fā)盛行之際還得掌握J(rèn)avaScript,現(xiàn)在Flutter又火了,是不是也得了解一下Dart。學(xué)習(xí)是無止境的,有時(shí)候知識面寬廣也是一種優(yōu)勢,但是也不能什么都是蜻蜓點(diǎn)水,起碼得精通一門語言,每種語言都會個(gè)Hello Word估計(jì)找工作都很艱難。接下來一起回顧一下怎么用ReactNative封裝Android和iOS原生組件吧!這里主要介紹高德地圖如何繪制點(diǎn)標(biāo)記,剛開始查了一下資料沒有找到如何寫回調(diào)方法,都是仿官方文檔寫ImageView組件的封裝。

二、iOS原生組件

YFRNMapView繼承RCTViewManager類就可以被RN調(diào)用,下面ReactNative整合會介紹。RCT_EXPORT_VIEW_PROPERTY主要用來傳遞參數(shù)用的,RCTBubblingEventBlock定義的時(shí)候一定要用on開頭。

@interface YFRNMapView()
@property(nonatomic,strong)YFMapView *mapView ;
@end
@implementation YFRNMapView
RCT_EXPORT_MODULE()
- (UIView *)view {
 _mapView=[[YFMapView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-YFStatusHeight-44)];
  return _mapView;
}
RCT_EXPORT_VIEW_PROPERTY(type, NSString)
RCT_EXPORT_VIEW_PROPERTY(deviceArray, NSArray)
RCT_EXPORT_VIEW_PROPERTY(onClick, RCTBubblingEventBlock)
@end

暴露 type, deviceArray,onClick,并重寫set方法。

@interface YFMapView ()<MAMapViewDelegate>
@property(nonatomic,strong) MAMapView *mapView;
@end
@implementation YFMapView
 - (instancetype)initWithFrame:(CGRect)frame
{
  self = [super initWithFrame:frame];
  if (self) {
       self.backgroundColor=YFColorRGB(246, 246, 250);
        self.mapView = [[MAMapView alloc] initWithFrame:self.bounds];
        self.mapView.delegate=self;
        self.mapView.rotateEnabled=NO;
        [self.mapView setZoomLevel:16];
        [self addSubview:self.mapView];
  }
  return self;
}


- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation
 {
     if ([annotation isKindOfClass:[MAUserLocation class]]) {
         return  nil;
     }
     if ([annotation isKindOfClass:[YFPointAnnotation class]])
     {
         __weak typeof(self) weakSelf=self;
         static NSString *pointReuseIndentifier = @"pointReuseIndentifier";
         MAPinAnnotationView*annotationView = (MAPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];
         if (annotationView == nil)
         {
             annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];
         }
  
         annotationView.imageView.contentMode=UIViewContentModeScaleAspectFit;
         YFPointAnnotation *yfAnnotation=(YFPointAnnotation*)annotation;
        if (yfAnnotation.tag==1) {
             [annotationView.imageView sd_setImageWithURL: [NSURL URLWithString:[NSString stringWithFormat:@"%@", yfAnnotation.deviceDict[@"device_icon"]]]];
              UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
              [[tap rac_gestureSignal] subscribeNext:^(id x) {
          
                 NSString *deviceID = [NSString stringWithFormat:@"%@", yfAnnotation.deviceDict[@"device_id"]];
                  weakSelf.onClick(@{@"deviceId":deviceID});
              }];
              [annotationView addGestureRecognizer:tap];
              return annotationView;
        }else{
          [annotationView.imageView sd_setImageWithURL: [NSURL URLWithString:[NSString stringWithFormat:@"%@", yfAnnotation.deviceDict[@"status_icon"]]]];
                       UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
                       [[tap rac_gestureSignal] subscribeNext:^(id x) {
                   
                          NSString *dalarmID = [NSString stringWithFormat:@"%@", yfAnnotation.deviceDict[@"alarm_id"]];
                           weakSelf.onClick(@{@"alarmId":dalarmID});
                          
                       }];
                       [annotationView addGestureRecognizer:tap];
                       return annotationView;
        }
       
         return annotationView;
     }
     return nil;
 }

-(void)setOnClick:(RCTBubblingEventBlock)onClick{
  _onClick=onClick;
}

-(void)setDeviceArray:(NSArray *)deviceArray{
  if (deviceArray) {
    _deviceArray=deviceArray;
    
    NSMutableArray *pointArray=[NSMutableArray array];
     for (NSDictionary *dict in  deviceArray) {
       NSLog(@"%@",dict);
       YFPointAnnotation *pointAnnotation = [[YFPointAnnotation alloc] init];
       float lat=[dict[@"lat"] floatValue];
        float lng=[dict[@"lng"] floatValue];
       if ([dict[@"gis_type"] intValue]==1) {
         if (lat>0 && lng>0) {
            pointAnnotation.coordinate =[YFLocationConverter wgs84ToGcj02:CLLocationCoordinate2DMake(lat,lng)];
         }else{
           continue;
         }
          
       }else{
           if (lat>0 && lng>0) {
           pointAnnotation.coordinate = CLLocationCoordinate2DMake(lat,lng);
           }else{
              continue;
           }
       }
       pointAnnotation.deviceDict=dict;
       if ([self.type isEqualToString:@"alarm"]) {
           pointAnnotation.tag=2;
       }else{
          pointAnnotation.tag=1;
       }
      
       [self.mapView addAnnotation:pointAnnotation];
       [pointArray addObject:pointAnnotation];
     }
     [self.mapView showAnnotations:pointArray animated:YES];
  }
}

三、Android原生組件

在封裝NativeModule (原生模塊) 的時(shí)候,定義了ReactContextBaseJavaModule的子類,并實(shí)現(xiàn)了getName、@ReactMethod注解的供RN調(diào)用的通信方法等。創(chuàng)建原生UI組件需要我們定義SimpleViewManager的子類,并實(shí)現(xiàn)getName、createViewInstance、@ReactProp注解的方法。

public class YFMapViewManager extends SimpleViewManager<YFRNMapView> {
    public static final String REACT_CLASS = "YFRNMapView";

    @Override
    public String getName() {
        return REACT_CLASS;
    }

    @Override
    protected YFRNMapView createViewInstance(ThemedReactContext reactContext) {
        return new YFRNMapView(reactContext);
    }

    @ReactProp(name = "deviceArray")
    public  void setDeviceArray(YFRNMapView mapView, ReadableArray deviceArray){

        mapView.setDeviceArray(deviceArray);

    }
   @ReactProp(name = "type")
    public  void setType(YFRNMapView mapView, String type){
        mapView.setType(type);
   }

    @Override
    protected void addEventEmitters(
            final ThemedReactContext reactContext,
            final YFRNMapView view) {
    }

    @Override
    public Map<String, Object> getExportedCustomDirectEventTypeConstants() {
        return MapBuilder.<String, Object>builder()
                .put("onClick", MapBuilder.of("registrationName", "onClick"))//registrationName 后的名字,RN中方法也要是這個(gè)名字否則不執(zhí)行
                .build();
    }
}

注冊UI模塊,定義ReactPackage的子類,即包管理類,并將其添加。還需要將YFMapPackage類添加到Application的getPackages。

public class YFMapPackage implements ReactPackage {
    @Override
    public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
        return Collections.emptyList();
    }

    @Override
    public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
        List<ViewManager> modules = new ArrayList<>();
        modules.add(new YFMapViewManager());
        return modules;
    }
}

通過receiveEvent實(shí)現(xiàn)函數(shù)回調(diào),執(zhí)行onClick方法。

public class YFRNMapView extends FrameLayout {

    private MapView MAPVIEW;
    private ThemedReactContext CONTEXT;
    private ViewGroup.LayoutParams PARAM;
    private AMap AMAP;
    private  String TYPE;

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

    }

    public YFRNMapView(ThemedReactContext context) {
        super(context);
        this.CONTEXT = context;
 PARAM = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    }

    /**
     * Activity onResume后調(diào)用view的onAttachedToWindow
     */
    @Override
    protected void onAttachedToWindow() {
        init();
        super.onAttachedToWindow();
    }

    /**
     * 初始化控件,定位位置
     */
    private void init() {
        MAPVIEW = new MapView(CONTEXT);
        MAPVIEW.setLayoutParams(PARAM);
        this.addView(MAPVIEW);
        MAPVIEW.onCreate(CONTEXT.getCurrentActivity().getIntent().getExtras());

        setMapOptions();

    }

    /**
     * 設(shè)置一些amap的屬性
     */
    private void setMapOptions() {

        AMAP = MAPVIEW.getMap();
        AMAP.setMapType(AMap.MAP_TYPE_NORMAL);// 矢量地圖模式

        AMap.OnMarkerClickListener markerClickListener = new AMap.OnMarkerClickListener() {
            // marker 對象被點(diǎn)擊時(shí)回調(diào)的接口
            // 返回 true 則表示接口已響應(yīng)事件,否則返回false
            @Override
            public boolean onMarkerClick(Marker marker) {
             
                WritableMap eventMap = Arguments.createMap();
                if(TYPE == "alarm") {
                    eventMap.putString("alarmId", marker.getTitle());
                }else {
                    eventMap.putString("deviceId", marker.getTitle());
                }

                ReactContext reactContext = (ReactContext) getContext();
                reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(
                        getId(),
                        "onClick",
                        eventMap);

                return true;
            }
        };
        // 綁定 Marker 被點(diǎn)擊事件
        AMAP.setOnMarkerClickListener(markerClickListener);
   }

    public  void setType(String type){
        TYPE=type;
    }
    public void setDeviceArray(ReadableArray deviceArray) {
        LatLngBounds.Builder builder = new LatLngBounds.Builder();
        LatLngBounds bounds = null;
        for (int i = 0; i < deviceArray.size(); i++) {
            ReadableMap map = deviceArray.getMap(i);
            double lat = convertToDouble(map.getString("lat"),0);
            double lon =convertToDouble(map.getString("lng"),0) ;

            LatLng sourceLatLng =new LatLng(lat, lon);
            LatLng desLatLng=null;
            int gisType=map.getInt("gis_type");
            if(lat>0 && lon>0) {
                if (gisType == 1) {
                    CoordinateConverter converter = new CoordinateConverter(getContext());
                    converter.from(CoordinateConverter.CoordType.GPS);
                    converter.coord(sourceLatLng);
                    desLatLng = converter.convert();
                } else {
                    desLatLng = sourceLatLng;
                }
                Log.i("ReadableMap",desLatLng.toString());
                builder.include(desLatLng);
            }else {
                continue;
            }

            Log.i("ReadableMap",TYPE);
            int ID=0;
            String imgURL="";
            if(TYPE.equals("alarm")){
                ID = map.getInt("alarm_id");
                imgURL=map.getString("status_icon");
            }else {
                ID = map.getInt("device_id");
                imgURL=map.getString("device_icon");
            }
            MarkerOptions markerOptions = new MarkerOptions();
            markerOptions.position(desLatLng);
            markerOptions.title(String.valueOf(ID));
            markerOptions.visible(true);
            markerOptions.draggable(false);
            Glide.with(getContext()).asBitmap().load(imgURL).diskCacheStrategy(DiskCacheStrategy.ALL).into(new CustomTarget() {

                @Override
                public void onResourceReady(@NonNull Object resource, @Nullable Transition transition) {
                    BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap((Bitmap)resource);
                   //添加覆蓋物
                    markerOptions.icon(bitmapDescriptor);
                   AMAP.addMarker(markerOptions);
                }
                @Override
                public void onLoadCleared(@Nullable Drawable placeholder) { }
            });
      }

        bounds = builder.build();
        if (bounds!=null) {
            AMAP.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 40));
        }

    }
     @Override
    protected Parcelable onSaveInstanceState() {
        if (CONTEXT.getCurrentActivity().getIntent() != null && CONTEXT.getCurrentActivity().getIntent().getExtras() != null) {
            MAPVIEW.onSaveInstanceState(CONTEXT.getCurrentActivity().getIntent().getExtras());
        }
        return super.onSaveInstanceState();
    }

    @Override
    protected void onDetachedFromWindow() {
        this.removeView(MAPVIEW);
        MAPVIEW.onDestroy();
        super.onDetachedFromWindow();
    }

    /**
     * 對應(yīng)onResume、對應(yīng)onPause
     *
     * @param hasWindowFocus
     */
    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {

        super.onWindowFocusChanged(hasWindowFocus);

        if (hasWindowFocus) {
//            對應(yīng)onResume
            MAPVIEW.onResume();
        } else {
            //對應(yīng)onPause
            MAPVIEW.onPause();
      }
    }


    public static double convertToDouble(String number, double defaultValue) {
        if (TextUtils.isEmpty(number)) {
          return defaultValue;
       }

        try {
          return Double.parseDouble(number);
       } catch (Exception e) {
          return defaultValue;
      }
    }
 }

四、ReactNative整合

import React, { Component } from 'react';
import { requireNativeComponent} from 'react-native';
import PropTypes from 'prop-types';
var YFRNMapView = requireNativeComponent('YFRNMapView', MapView);
 
export default class MapView extends Component {
   
  render() {
    return (
      <YFRNMapView {...this.props} />
    );
  }
}
  
MapView.propTypes = {
      type:PropTypes.string,
    deviceArray:PropTypes.array,
    onClick:PropTypes.func
};

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,156評論 6 531
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,401評論 3 415
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事?!?“怎么了?”我有些...
    開封第一講書人閱讀 176,069評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,873評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 71,635評論 6 408
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,128評論 1 323
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,203評論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,365評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,881評論 1 334
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,733評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 42,935評論 1 369
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,475評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,172評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,582評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,821評論 1 282
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 51,595評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 47,908評論 2 372