ToastView.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * Toast
  3. * Created by chenyxi on 2020/3/17.
  4. */
  5. import React,{Component}from 'react';
  6. import {
  7. StyleSheet,
  8. View,
  9. Easing,
  10. Dimensions,
  11. Text,
  12. Animated,
  13. ActivityIndicator,
  14. Keyboard,
  15. Platform
  16. } from 'react-native';
  17. import PropTypes from 'prop-types';
  18. const {width,height} = Dimensions.get('window');
  19. //iphoneX
  20. const X_WIDTH = 375;
  21. const X_HEIGHT = 812;
  22. //显示时长
  23. const DURATION = 1500;
  24. // Toast提示框透明度
  25. const OPACITY = 0.8;
  26. class ToastView extends Component {
  27. static PropTypes = {
  28. message : PropTypes.string,
  29. time: PropTypes.number,
  30. isShow:PropTypes.bool,
  31. toastStartPosition:PropTypes.string,
  32. marginBottom:PropTypes.number,
  33. containerTop:PropTypes.number
  34. }
  35. dismissHandler = null;
  36. isShow = false;
  37. constructor(props) {
  38. super(props);
  39. this.state = {
  40. message:'',
  41. time:DURATION,
  42. isShow:false,
  43. toastStartPosition:'center',//toast展示的位置,默认中间位置
  44. opacityAnimate: new Animated.Value(OPACITY), // 动画 值初始化
  45. marginBottom:0,//距离底部高度
  46. containerTop:0//距离顶部偏移
  47. }
  48. }
  49. componentWillUnmount() {
  50. //在页面生命周期结束时,解除定时器,避免内存泄露
  51. clearTimeout(this.dismissHandler);
  52. }
  53. show(message, position = 'center') {
  54. if(this.isShow){
  55. clearTimeout(this.dismissHandler);
  56. this.state.isShow = false;
  57. }
  58. this.isShow = true;
  59. this.state.toastStartPosition = 'center';
  60. this.state.marginBottom = 0;
  61. this.state.containerTop = 0;
  62. this.setState({
  63. isShow:true,
  64. message:message
  65. });
  66. this.state.opacityAnimate.setValue(OPACITY);
  67. this.state.isShow =true;
  68. if (this.dismissHandler) {
  69. clearTimeout(this.dismissHandler);
  70. }
  71. this.dismissHandler = setTimeout(() => {
  72. // 开启动画
  73. Animated.timing(this.state.opacityAnimate, {
  74. toValue: 0.0,
  75. duration: 600,
  76. }).start(() => {
  77. // 动画结束后,初始化状态
  78. this.setState({
  79. isShow: false,
  80. });
  81. this.isShow = false;
  82. this.onDismiss();
  83. });
  84. }, this.state.time);
  85. }
  86. onDismiss = () => {
  87. if (this.props.onDismiss) {
  88. this.props.onDismiss()
  89. }
  90. }
  91. //判断是否iphoneX
  92. isIphoneX = () => {
  93. return (
  94. Platform.OS === 'ios' && ((height === X_HEIGHT && width === X_WIDTH) || (height === X_WIDTH && width === X_HEIGHT))
  95. )
  96. }
  97. render() {
  98. return this.state.isShow ?(
  99. <View style={[styles.container,{marginTop:this.state.containerTop}]} pointerEvents='none' >{/*pointerEvents='none'*/}
  100. <Animated.View style={[styles.textContainer, { opacity: this.state.opacityAnimate,alignSelf:this.state.toastStartPosition,marginBottom:this.state.marginBottom}]}>
  101. <Text
  102. style={styles.defaultText}>{this.state.message}
  103. </Text>
  104. </Animated.View>
  105. </View>
  106. ):null
  107. }
  108. }
  109. const styles = StyleSheet.create({
  110. textContainer: {
  111. backgroundColor: 'rgba(0,0,0,.8)',
  112. borderRadius: 8,
  113. padding: 10,
  114. maxWidth: width/5*4,
  115. justifyContent: 'center',
  116. alignItems: 'center',
  117. },
  118. defaultText: {
  119. color: "#FFF",
  120. fontSize: 15,
  121. },
  122. container: {
  123. position: "absolute",
  124. width: width,
  125. height: height,
  126. zIndex: 99999,
  127. flexDirection: "row",
  128. justifyContent: "center",
  129. alignItems: 'center',
  130. }
  131. });
  132. export default ToastView