Toast.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. modalVisible: false
  11. };
  12. }
  13. componentDidMount() {
  14. Animated.sequence([
  15. // 使用宽松函数让数值随时间动起来。
  16. Animated.timing(
  17. // 随时间变化而执行动画
  18. this.state.fadeAnim, // 动画中的变量值
  19. {
  20. toValue: 1, // 透明度最终变为1,即完全不透明
  21. duration: 500 // 让动画持续一段时间
  22. }
  23. ),
  24. Animated.delay(4000),
  25. Animated.timing(this.state.fadeAnim, {
  26. toValue: 0,
  27. duration: 200
  28. })
  29. ]).start((res) => {
  30. this.props.delete && this.props.delete(res);
  31. });
  32. }
  33. render() {
  34. let { fadeAnim } = this.state;
  35. const opacity = fadeAnim.interpolate({
  36. inputRange: [ 0, 1 ],
  37. outputRange: [ 0, 1 ]
  38. });
  39. const translateY = fadeAnim.interpolate({
  40. inputRange: [ 0, 1 ],
  41. outputRange: [ 20, 0 ]
  42. });
  43. return (
  44. <Animated.Text // 使用专门的可动画化的Text组件
  45. style={{
  46. opacity: opacity, // 将透明度指定为动画变量值
  47. backgroundColor: 'rgba(0,0,0,0.7)',
  48. borderRadius: 10,
  49. color: '#FFF',
  50. marginTop: 5,
  51. paddingBottom: 5,
  52. paddingLeft: 15,
  53. paddingTop: 5,
  54. paddingRight: 15,
  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}>
  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({ modalVisible: false });
  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;