原生控件boilerplate

swift原生模块

https://reactnative.cn/docs/native-modules-intro

  1. // xyLive.swift
  2. import Foundation
  3. @objc(XyLive)
  4. class XyLive: NSObject {
  5. @objc(addEvent:location:date:)
  6. func addEvent(name: String, location: String, date: NSNumber) -> Void {
  7. // Date is ready to use!
  8. print("hello")
  9. }
  10. }
  11. // xyLive-bridge.m
  12. #import <React/RCTBridgeModule.h>
  13. @interface RCT_EXTERN_MODULE(XyLive, NSObject)
  14. RCT_EXTERN_METHOD(addEvent:(NSString *)name location:(NSString *)location date:(nonnull NSNumber *)date)
  15. @end
  16. // index.tsx
  17. <Button
  18. onPress={() => {
  19. const XyLive = NativeModules.XyLive;
  20. XyLive.addEvent(
  21. 'Birthday Party',
  22. '4 Privet Drive, Surrey',
  23. Date.now(),
  24. );
  25. }}>
  26. test ios module
  27. </Button>

oc ui控件

//
//  RNTMapManager.m
//  mapp
//
//  Created by 武昭 on 2021/12/29.
//


#import <MapKit/MapKit.h>
#import "RCTConvert+Mapkit.h"
#import <React/RCTViewManager.h>

@interface RCTXyRtcTextureViewManager : RCTViewManager
@end

@implementation RCTXyRtcTextureViewManager

RCT_EXPORT_MODULE(RCTXyRtcTextureView)

RCT_EXPORT_VIEW_PROPERTY(zoomEnabled, BOOL)

RCT_CUSTOM_VIEW_PROPERTY(region, MKCoordinateRegion, MKMapView)
{
  [view setRegion:json ? [RCTConvert MKCoordinateRegion:json] : defaultView.region animated:YES];
}

- (UIView *)view
{

  return [[MKMapView alloc] init];
}

@end


// ==========================================================

//
//  RCTConvert+Mapkit.h
//  mapp
//
//  Created by 武昭 on 2021/12/30.
//

#ifndef RCTConvert_Mapkit_h
#define RCTConvert_Mapkit_h

#import <MapKit/MapKit.h>
#import <React/RCTConvert.h>
#import <CoreLocation/CoreLocation.h>
#import <React/RCTConvert+CoreLocation.h>

@interface RCTConvert (Mapkit)

+ (MKCoordinateSpan)MKCoordinateSpan:(id)json;
+ (MKCoordinateRegion)MKCoordinateRegion:(id)json;

@end

@implementation RCTConvert(MapKit)

+ (MKCoordinateSpan)MKCoordinateSpan:(id)json
{
  json = [self NSDictionary:json];
  return (MKCoordinateSpan){
    [self CLLocationDegrees:json[@"latitudeDelta"]],
    [self CLLocationDegrees:json[@"longitudeDelta"]]
  };
}

+ (MKCoordinateRegion)MKCoordinateRegion:(id)json
{
  return (MKCoordinateRegion){
    [self CLLocationCoordinate2D:json],
    [self MKCoordinateSpan:json]
  };
}

@end

#endif /* RCTConvert_Mapkit_h */