Toast.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import React, { Component } from 'react';
  2. import { StyleSheet, View, Easing, Dimensions, Text, Animated } from 'react-native';
  3. import ModalView from '../utils/ModalUtil';
  4. const { width, height } = Dimensions.get('window');
  5. class AddToast extends Component {
  6. constructor(props) {
  7. super(props);
  8. this.state = {
  9. fadeAnim: new Animated.Value(0)
  10. };
  11. }
  12. componentDidMount() {
  13. this.anim = Animated.sequence([
  14. // 使用宽松函数让数值随时间动起来。
  15. Animated.timing(
  16. // 随时间变化而执行动画
  17. this.state.fadeAnim, // 动画中的变量值
  18. {
  19. toValue: 1, // 透明度最终变为1,即完全不透明
  20. duration: 500 // 让动画持续一段时间
  21. }
  22. )
  23. // Animated.delay(4000),
  24. ]).start();
  25. this.anim_timeout = setTimeout(() => {
  26. this.props.delete();
  27. }, 3000);
  28. }
  29. componentWillUnmount() {
  30. clearTimeout(this.anim_timeout);
  31. }
  32. render() {
  33. let { fadeAnim } = this.state;
  34. const opacity = fadeAnim.interpolate({
  35. inputRange: [ 0, 1 ],
  36. outputRange: [ 0, 1 ]
  37. });
  38. const translateY = fadeAnim.interpolate({
  39. inputRange: [ 0, 1 ],
  40. outputRange: [ 20, 0 ]
  41. });
  42. return (
  43. <Animated.Text // 使用专门的可动画化的Text组件
  44. style={{
  45. opacity: opacity, // 将透明度指定为动画变量值
  46. backgroundColor: 'white',
  47. borderRadius: 10,
  48. color: 'black',
  49. marginTop: 5,
  50. width: '70%',
  51. textAlign: 'center',
  52. textAlignVertical: 'center',
  53. height: '15%',
  54. fontSize: 22,
  55. transform: [ { translateY: translateY } ]
  56. }}
  57. >
  58. {this.props.children}
  59. </Animated.Text>
  60. );
  61. }
  62. }
  63. let _this;
  64. let key = 0;
  65. class ToastView extends Component {
  66. constructor(props) {
  67. super(props);
  68. _this = this;
  69. this.state = {
  70. toastList: [],
  71. modalVisible: false
  72. };
  73. this.deleteToast = this.deleteToast.bind(this);
  74. }
  75. static add = (value) => {
  76. var toastList = _this.state.toastList;
  77. var toastAddState = true;
  78. toastList.forEach((item, index) => {
  79. if (item.text === value) {
  80. toastAddState = false;
  81. }
  82. });
  83. if (toastAddState) {
  84. toastList.push({
  85. text: value,
  86. value: (
  87. <AddToast key={key} delete={_this.deleteToast} visible={_this.state.modalVisible}>
  88. {value}
  89. </AddToast>
  90. )
  91. });
  92. key++;
  93. _this.setState({ toastList: toastList, modalVisible: true });
  94. }
  95. };
  96. deleteToast() {
  97. var toastList = this.state.toastList;
  98. toastList.splice(0, 1);
  99. this.setState({ toastList: toastList, modalVisible: false });
  100. }
  101. getView = () => {
  102. return (
  103. <View
  104. style={{
  105. position: 'absolute',
  106. height: '100%',
  107. width: '100%',
  108. justifyContent: 'center',
  109. alignItems: 'center'
  110. }}
  111. >
  112. {this.state.toastList.map((item, index) => {
  113. return item.value;
  114. })}
  115. </View>
  116. );
  117. };
  118. render() {
  119. return (
  120. <ModalView
  121. close={() => {
  122. this.setState(this.deleteToast());
  123. }}
  124. visible={this.state.modalVisible}
  125. customerlayout={{ flex: 1, justifyContent: 'flex-end' }}
  126. contentView={this.getView}
  127. />
  128. );
  129. }
  130. }
  131. export default ToastView;