RN自定义PickerView

根据需求可以自定义为自己的格式,这份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标签对别忘了哦!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,240评论 3 119
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 179,392评论 25 708
  • 昨晚断网了,静下心来,看了4个小时的书,感觉挺好的! 看来,一切都只是自己的借口,没有网络真的可以,强迫自己改变即...
    西风潇潇阅读 529评论 0 1
  • 文,冷月秋风 那年那月 画押一纸契约 许十里红妆,长亭柳依依 从此,左边是你,右边是我 一方庭院,爱的37度不温不...
    冷月秋风qin阅读 407评论 0 3
  • 至今至远东西,至深至浅清溪。至高至明日月,至亲至疏夫妻。琴瑟在御,莫不静好。
    呀___土豆阅读 212评论 0 0

友情链接更多精彩内容