React Native 自定义实现【Toast】提示框

前端之家收集整理的这篇文章主要介绍了React Native 自定义实现【Toast】提示框前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

今天和大家分享一篇自定义View的内容


一、简介


之前和大家分享自定义View内容,我们知道自定义View一般分为三个步骤:


(1)定义属性、状态

(2)定义组件行为

(3)绘制布局

(4)定义样式


二、分析


App中的提示(Toast)想必大家都不陌生,RN中也提供了ToastAndroid,但是由于仅能在Android平台使用,所以出现了很多开源的三方依赖库,例如react-native-root-toast等等。那么这种该如何实现呢?

1、Toast提示框大致由三部分组成:

(1)显示文字

(2)显示时长

(3)显示位置

(4)背景

我们可以根据这些来定义基本属性


2、Toast的的状态分为两种:

(1)显示

(2)隐藏

显示/隐藏的行为我们可以通过state状态来控制。


3、在Toast显示隐藏时,可以用透明度动画来增加用户体验

分析了以上思路,那么就是撸码的节奏了。


三、实现


(1)定义属性、状态:

  1. // 定义props
  2. static propTypes = {
  3. textStyle: ViewPropTypes.style,contentStyle: ViewPropTypes.style,containerStyle: ViewPropTypes.style,position: PropTypes.oneOf([
  4. 'top','center','bottom'
  5. ])
  6. }
  7.  
  8. //初始化 默认 props
  9. static defaultProps = {
  10. position: 'center'
  11. }
  12.  
  13. constructor(props) {
  14. super(props);
  15. this.state = {
  16. text: '',isShow: false,opacityAnimate: new Animated.Value(OPACITY) // 动画 值初始化
  17. };
  18.  
  19. // 当前显示状态
  20. this.isShow = false;
  21. // 初始化默认显示时长为SHORT
  22. this.duration = DURATION.SHORT;
  23. }
(2)定义组件行为
  1. /**
  2. * 显示
  3. */
  4. show(text,duration) {
  5.  
  6. if(duration >= DURATION.LONG) {
  7. this.duration = DURATION.LONG;
  8. } else {
  9. this.duration = DURATION.SHORT;
  10. }
  11.  
  12. // 显示
  13. this.setState({
  14. text: text,isShow: true
  15. });
  16. this.isShow = true;
  17. this.state.opacityAnimate.setValue(OPACITY);
  18.  
  19. // 执行隐藏操作
  20. this.hide();
  21. }
  22.  
  23. /**
  24. * 隐藏
  25. */
  26. hide() {
  27. // 隐藏状态下不执行操作
  28. if(!this.isShow) {
  29. return;
  30. }
  31.  
  32. this.animateTimer && clearTimeout(this.animateTimer);
  33. this.animateTimer = setTimeout(()=>{
  34. // 开启动画
  35. Animated.timing(
  36. this.state.opacityAnimate,{
  37. toValue: 0.0,duration: 600
  38. }
  39. ).start(()=>{
  40. // 动画结束后,初始化状态
  41. this.setState({
  42. isShow: false
  43. })
  44. this.isShow = false;
  45. })
  46. },this.duration);
  47.  
  48. }
(3)绘制布局
  1. render() {
  2. let top;
  3. switch(this.props.position){
  4. case 'top':
  5. top = 30;
  6. break;
  7. case 'center':
  8. top = height / 2;
  9. break;
  10. case 'bottom':
  11. top = height - 100;
  12. break;
  13. default:
  14. break;
  15. }
  16. return this.state.isShow ?
  17. <View
  18. pointerEvents={ 'none' }
  19. style={[ styles.container,{ top: top } ]}>
  20. <Animated.View
  21. style={[ styles.content,this.props.contentStyle,{ opacity: this.state.opacityAnimate } ]}
  22. >
  23. <Text style={[ styles.text,this.props.textStyle ]}>
  24. { this.state.text }
  25. </Text>
  26. </Animated.View>
  27. </View> : null;
  28. }
  29. }
(4)定义样式(建议单独封装样式,在组件中引用,遵循模块化开发)
  1. const styles = StyleSheet.create({
  2.  
  3. container: {
  4. position: 'absolute',left: 0,right: 0,alignItems: 'center'
  5. },content: {
  6. backgroundColor: '#000000',opacity: OPACITY,borderRadius: 20,padding: 10
  7. },text: {
  8. color: '#FFFFFF',fontSize: 14,fontFamily: 'PingFang-SC-Regular'
  9. }
  10. })
以上代码很简单,基本使用了Animated + View 的方式来实现。最后贴上完整代码
  1. /**
  2. * Toast 提示
  3. */
  4. import React,{ Component,PropTypes } from 'react';
  5. import {
  6. View,Text,Animated,Dimensions,StyleSheet,ViewPropTypes
  7. } from 'react-native';
  8.  
  9. // 设备屏幕宽高
  10. const { width,height } = Dimensions.get('window');
  11. // Toast提示框透明度
  12. const OPACITY = 0.8;
  13. // 显示时长
  14. export const DURATION = { LONG: 1500,SHORT: 500 };
  15.  
  16. export default class Toast extends Component {
  17.  
  18. // 定义props
  19. static propTypes = {
  20. textStyle: ViewPropTypes.style,opacityAnimate: new Animated.Value(OPACITY) // 动画 值初始化
  21. };
  22.  
  23. // 当前显示状态
  24. this.isShow = false;
  25. // 初始化默认显示时长为SHORT
  26. this.duration = DURATION.SHORT;
  27. }
  28.  
  29. componentWillUnmount() {
  30. // 在页面生命周期结束时,解除定时器,避免内存泄漏
  31. this.animateTimer && clearTimeout(this.animateTimer);
  32. }
  33.  
  34. /**
  35. * 显示
  36. */
  37. show(text,this.duration);
  38.  
  39. }
  40.  
  41. render() {
  42. let top;
  43. switch(this.props.position){
  44. case 'top':
  45. top = 30;
  46. break;
  47. case 'center':
  48. top = height / 2;
  49. break;
  50. case 'bottom':
  51. top = height - 100;
  52. break;
  53. default:
  54. break;
  55. }
  56. return this.state.isShow ?
  57. <View
  58. pointerEvents={ 'none' }
  59. style={[ styles.container,this.props.textStyle ]}>
  60. { this.state.text }
  61. </Text>
  62. </Animated.View>
  63. </View> : null;
  64. }
  65. }
  66.  
  67. const styles = StyleSheet.create({
  68.  
  69. container: {
  70. position: 'absolute',fontFamily: 'PingFang-SC-Regular'
  71. }
  72. })


四、使用


1、导入

  1. import Toast from './components/Toast';
  2.  
  3. <Toast ref="toast" position={'bottom'} />

2、调用
  1. /**
  2. * 显示
  3. */
  4. showToast() {
  5. this.refs.toast.show('中奖了...',1200);
  6. }

猜你在找的React相关文章