美文网首页
RN自定义PickerView

RN自定义PickerView

作者: Miss_QL | 来源:发表于2018-07-19 13:37 被阅读29次

根据需求可以自定义为自己的格式,这份demo的需求是:picker上面添加包含取消和确定的按钮视图。

具体如图:


image.png

上代码:

import React, { Component } from 'react';
import {
    StyleSheet,
    View,
    Text,
    Animated,
    Easing,
    PickerIOS,
} from 'react-native';
import SystemHandler from './SystemHandler';

export default class PickerView extends Component {

    constructor(props) {
        super(props);
        this.state = {
          offset: new Animated.Value(0),
          opacity: new Animated.Value(0),
          dataSource: [],
          choice: this.props.defaultVal,
          hide: true,
        };
    }

    componentWillUnMount() {
        this.timer && clearTimeout(this.timer);
    }

    render() {
        if(this.state.hide) {
            return (<View />);
        } 
        else {
            return (
                <View style = {pickerStyles.container}>
                    <View style = {pickerStyles.mask}>
                    </View>
                    <Animated.View 
                        style = {[
                            pickerStyles.tip, 
                            {transform: [{translateY: this.state.offset.interpolate({
                                    inputRange: [0, 1],
                                    outputRange: [SystemHandler.windowSize.height, (SystemHandler.windowSize.height - 320)]
                                }),
                            }]}
                        ]}
                    >
                        <View style = {pickerStyles.tipTitleView}>
                            <Text style = {pickerStyles.cancelText} onPress = {this.cancel.bind(this)}>取消</Text>
                            <Text style = {pickerStyles.okText} onPress = {this.ok.bind(this)}>确定</Text>
                        </View>
                        <PickerIOS
                            style = {pickerStyles.picker}
                            selectedValue = {this.state.choice}
                            onValueChange = {choice => this.setState({choice: choice})}
                        >
                            {this.state.dataSource.map((aOption) => (
                                <PickerIOS.Item
                                    key = {aOption}
                                    value = {aOption}
                                    label = {aOption}
                                />
                            ))}
                        </PickerIOS>
                    </Animated.View>
                </View>
            );
        }
    }

    /****************************** event ******************************/ 
    /******************** public ********************/
    // 设置Pickerview的数据源数组
    setDataSource(array) {
        this.setState({
            choice: array[0],
            dataSource: array,
        })
    }

    // 显示Pickerview
    show() {
        if(this.state.hide) {
            this.setState({hide: false});
            this.in();
        }
    }


    /******************** private ********************/
    // 显示动画
    in() {
        Animated.parallel([
            Animated.timing(
                this.state.opacity,
                {
                    easing: Easing.linear,
                    duration: this.props.duration,
                    toValue: 0.8,
                }
            ),
            Animated.timing(
                this.state.offset,
                {
                    easing: Easing.linear,
                    duration: this.props.duration,
                    toValue: 1,
                }
            )
        ]).start();
    }

    //隐藏动画
    out() {
        Animated.parallel([
            Animated.timing(
                this.state.opacity,
                {
                    easing: Easing.linear,
                    duration: this.props.duration,
                    toValue: 0,
                }
            ),
            Animated.timing(
                this.state.offset,
                {
                    easing: Easing.linear,
                    duration: this.props.duration,
                    toValue: 0,
                }
            )
        ]).start();

        this.timer = setTimeout(() => {
            this.setState({hide: true})
        }, this.props.duration);
    }

    //取消
    cancel(event) {
        if(!this.state.hide) {
            this.out();
        }
    }

    //选择
    ok() {
        if(!this.state.hide) {
            this.out();
            if(this.props.okCallback) {
                this.props.okCallback(this.state.choice);
            }
        }
    }
}
  
const pickerStyles = StyleSheet.create({
    container: {
        position: 'absolute',
        width: SystemHandler.windowSize.width,
        height: SystemHandler.windowSize.height - 64,
    },
    mask: {
        justifyContent: "center",
        backgroundColor: "#383838",
        opacity: 0.3,
        position: "absolute",
        width: SystemHandler.windowSize.width,
        height: SystemHandler.windowSize.height - 64,
    },
    tip: {
        width: SystemHandler.windowSize.width,
        height: 260,
        position: "absolute",
        backgroundColor: '#f7f7f7',
        alignItems: "center",
        justifyContent: "space-between",
    },
    tipTitleView: {
        height: 50,
        width: SystemHandler.windowSize.width,
        flexDirection: 'row',
        alignItems: 'center', 
        justifyContent: 'space-between',
        borderBottomWidth: 0.5,
        borderColor: '#eeeeee',
        backgroundColor: '#fff',
    },
    cancelText:{
        color: '#ff9f45',
        fontSize: 16,
        paddingLeft: 30,
    },
    okText:{
        color: '#ff9f45',
        fontSize: 16,
        paddingRight: 30,
        fontWeight: 'bold',
    },
    picker:{
        justifyContent: 'center',
        height: 210,
        width: SystemHandler.windowSize.width,
    },
});

其中SystemHandler组件中定义了当前屏幕宽高的代码:

static windowSize = {
    width: Dimensions.get('window').width,
    height: Dimensions.get('window').height
}

若要适配安卓,则可将PickerIOS更换为Picker,对应的item标签对别忘了哦!

相关文章

  • RN自定义PickerView

    根据需求可以自定义为自己的格式,这份demo的需求是:picker上面添加包含取消和确定的按钮视图。 具体如图: ...

  • UIPickerView

    目录:1、单个PickerView的使用2、两个PickerView联动3、PickerView标题显示自定义 1...

  • iOS UIPickerView

    普通展示 二级联动 自定义pickerView

  • 城市选择器,百度地图poi完整实现地址填写

    实现了一个类似于京东的地址填写的东西,首先是省市区地址选择,自定义pickerview,pickerview最麻烦...

  • iOS-自定义PickerView

    分享一个自定义的日期选择控件,或者其他自定义选择项,通过UIPickerView实现,实现Pickerview的几...

  • 自定义pickerview

    今天写了一个自定义的pickerview,是关于时间选择的,先上图,有兴趣的朋友可以看看. 先创建一个model....

  • RN系列:Android中远程调试RN及混淆打包

    【简述RN集成到Android原生项目】【RN系列:RN使用Android原生控件或自定义组件】【React Na...

  • RN 组定义组件

    RN 组定义组件 背景 自定义RN native Component,js绑定事件与native通信 CheckB...

  • r n 坑

    rn 引用原生控件的时候,自定义组件的首字母必须大写。。。不然会出现错误。 2.rn 进行block。 rn调用...

  • pickerViewTitle的无上下限选择

    -(void)pickerView:(UIPickerView*)pickerView didSelectRow:...

网友评论

      本文标题:RN自定义PickerView

      本文链接:https://www.haomeiwen.com/subject/qkhtmftx.html