ReactNative系列

RN调用iOS组件

2017-06-02  本文已影响46人  酷酷的开发匠
#import "TestScrollViewManager.h"
#import "TestScrollView.h"//第三方组件
#import "RCTBridge.h" //进行通信文件
#import "RCTEventDispatcher.h" //事件派发
@interface TestScrollViewManager()<SDCycleScrollViewDelegate>
@property (nonatomic , strong) TestScrollView *testScrollView;
@end
  - (UIView *)view{
    //实际组件的大小位置由js控制
    _testScrollView = [TestScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width/2+20, [UIScreen mainScreen].bounds.size.width/2+20)delegate:self placeholderImage:nil];
    //  初始化时将delegate指向了self
    _testScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleClassic;
    _testScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
    return _testScrollView;   
}
RCT_EXPORT_MODULE()
RCT_EXPORT_VIEW_PROPERTY(autoScroll, BOOL)
RCT_EXPORT_VIEW_PROPERTY(onClickBanner, RCTBubblingEventBlock)
- (NSDictionary *)constantsToExport
{
    return @{
             @"SDCycleScrollViewPageContolAliment": @{
                     @"right": @(SDCycleScrollViewPageContolAlimentRight),
                     @"center": @(SDCycleScrollViewPageContolAlimentCenter)
                     }
             };
}

- (void)cycleScrollView:(TestScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
   
    //  导出事件
    cycleScrollView.onClickBanner(@{@"target": cycleScrollView.reactTag,
                                    @"value": [NSNumber numberWithInteger:index+1]});
}

//引用组件
var TextScrollView = require('./TextScrollView');
var TestScrollViewConsts = require('react-native').UIManager.TestScrollView.Constants;
 render() {

       return (
           <ScrollView style = {{marginTop:64}}>
               <View>
                   <TextScrollView style={styles.scrollViewStyle}
                                   autoScrollTimeInterval = {2}
                                   imageURLStringsGroup = {bannerImgs}
                                   pageControlAliment = {TestScrollViewConsts.SDCycleScrollViewPageContolAliment.right}
                                   onClickBanner={(e) => {
                                       console.log('test' + e.nativeEvent.value);
                                       this.setState({bannerNum:e.nativeEvent.value});
                                   }}
                   />
                   <Text style={{fontSize: 15, margin: 10, textAlign:'center'}}>
                       点击banner -> {this.state.bannerNum}
                   </Text>
               </View>
           </ScrollView>
       );
   }
import React,{Component,PropTypes} from 'react';
import {
    requireNativeComponent
}from 'react-native'
a. RCTScrollView 对应下方创建的名字
b. 'TestScrollView' 对应ios原生组件文件名字
c. 对应本文件 class 创建及导出的名字
//requireNativeComponent
var RCTScrollView = requireNativeComponent('TestScrollView',TextScrollView);
export default class TextScrollView extends Component{
   render(){
       return <RCTScrollView {...this.props}/>
   }
}
TextScrollView.propTypes = {
   //属性类型
   autoScrollTimeInterval:PropTypes.number,
   imageURLStringGroup:PropTypes.array,
   autoScroll:PropTypes.bool,
}
module.exports = TextScrollView;

demo下载

上一篇下一篇

猜你喜欢

热点阅读