RN:自定义Toask

新建js文件:

import React, { Component } from 'react';
import { StyleSheet, Animated, Text, View, Modal, Easing } from 'react-native';
import UiUtils from '../UiUtils';
let utils = new UiUtils();
utils.setHeightCalculateEnable(2224);
const getDeviceValue = utils.getDeviceValue;
const DEFAULT_SHOW_TIME = 2000;
const DEFAULT_FADE_IN_OPACITY_VALUE = new Animated.Value(1);
class Toast extends Component {
  constructor(props) {
    super(props);
    this.state = {
      isShowToast: false,
      msg: '',
      fadeInOpacity: DEFAULT_FADE_IN_OPACITY_VALUE,
    };
  }
  getInitialState() {
    return { fadeInOpacity: DEFAULT_FADE_IN_OPACITY_VALUE };
  }
  show = (msg, showTime) => {
    if (this.state.isShowToast === true) {
      return;
    }
    let toastShowTime = showTime ? showTime : DEFAULT_SHOW_TIME;
    setTimeout(() => {
      this.hidden();
    }, toastShowTime);
    this.setState({
      isShowToast: true,
      msg: msg,
    });
  };
  showAnimation = (msg, showTime) => {
    if (this.state.isShowToast === true) {
      return;
    }
    let animatedDuration = showTime ? showTime : DEFAULT_SHOW_TIME;
    let timing = Animated.timing(this.state.fadeInOpacity, {
      toValue: 0,
      duration: animatedDuration,
      easing: Easing.linear,
      useNativeDriver: false,
    });
    timing.start((isFinished) => {
      if (isFinished) {
        this.hidden();
        timing.reset();
      }
    });
    this.setState({
      isShowToast: true,
      msg: msg,
      fadeInOpacity: DEFAULT_FADE_IN_OPACITY_VALUE,
    });
  };
  hidden = () => {
    this.setState({
      isShowToast: false,
      msg: '',
    });
  };
  render() {
    return (
      <Modal
        //无效
        pointerEvents="none"
        animationType="fade"
        transparent={true}
        visible={this.state.isShowToast}
      >
        <Animated.View
          ref={(ref) => (this.animatedView = ref)}
          style={[styles.mainContainer, { opacity: this.state.fadeInOpacity }]}
        >
          <View style={styles.msgContainer}>
            <Text style={styles.msg}>{this.state.msg}</Text>
          </View>
        </Animated.View>
      </Modal>
    );
  }
}

export default Toast;

const styles = StyleSheet.create({
  mainContainer: {
    flex: 1,
    alignSelf: 'stretch',
    justifyContent: 'flex-end',
  },
  msgContainer: {
    alignContent: 'center',
    justifyContent: 'center',
    alignSelf: 'center',
    marginBottom: getDeviceValue(350),
  },
  msg: {
    padding: getDeviceValue(30),
    borderRadius: getDeviceValue(30),
    backgroundColor: 'black',
    textAlign: 'center',
    fontSize: getDeviceValue(60),
    color: 'white',
  },
});

使用:

this.toast.show('这是哈哈哈哈哈');
....
<Toast ref={(ref) => (this.toast = ref)} />
.....
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值