React Native技术篇—自定义Toast弹窗「建议收藏」

React Native技术篇—自定义Toast弹窗「建议收藏」注意:未经允许不可私自转载,违者必究ReactNative官方文档:https://reactnative.cn/docs/getting-started/项目GitHub地址:https://github.com/zhouwei1994/nativeCase.git在写自定义Toast弹窗之前我们要先创建一个ReactNative第二视图层。创建教程:https://b…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

注意:未经允许不可私自转载,违者必究

React Native官方文档:https://reactnative.cn/docs/getting-started/

项目GitHub地址:https://github.com/zhouwei1994/nativeCase.git

在写自定义Toast弹窗之前我们要先创建一个React Native第二视图层。

创建教程:https://blog.csdn.net/weixin_40614372/article/details/86506678

自定义Toast弹窗代码

在项目src/components/common目录下创建 Toast.js

import React, {
    Component,
} from 'react';

import {
    StyleSheet,
    View,
    Easing,
    Dimensions,
    Text,
    Animated
} from 'react-native';
const { width, height } = Dimensions.get('window')
class AddToast extends Component {
    constructor(props) {
        super(props);
        this.state = {
            fadeAnim: new Animated.Value(0)
        }
    }
    componentDidMount() {
        Animated.sequence([
            // 使用宽松函数让数值随时间动起来。
            Animated.timing(                  // 随时间变化而执行动画
                this.state.fadeAnim,            // 动画中的变量值
                {
                    toValue: 1,                   // 透明度最终变为1,即完全不透明
                    duration: 500,              // 让动画持续一段时间
                }
            ),
            Animated.delay(4000),
            Animated.timing(
                this.state.fadeAnim,
                {
                    toValue: 0,
                    duration: 500,
                }
            )
        ]).start((res) => {
            this.props.delete && this.props.delete(res);
        });
    }
    render() {
        let { fadeAnim } = this.state;
        const opacity = fadeAnim.interpolate({
            inputRange: [0, 1],
            outputRange: [0, 1]
        });
        const translateY = fadeAnim.interpolate({
            inputRange: [0, 1],
            outputRange: [20, 0]
        });
        return (
            <Animated.Text                 // 使用专门的可动画化的Text组件
                style={
  
  {
                    opacity: opacity,         // 将透明度指定为动画变量值
                    backgroundColor: "rgba(0,0,0,0.7)",
                    borderRadius: 10,
                    color: "#FFF",
                    marginTop: 5,
                    paddingBottom: 5,
                    paddingLeft: 15,
                    paddingTop: 5,
                    paddingRight: 15,
                    transform: [
                        { translateY: translateY },
                    ]
                }}
            >
                {this.props.children}
            </Animated.Text>
        )
    }
}
let _this;
let key = 0;
class ToastView extends Component {
    constructor(props) {
        super(props);
        _this = this;
        this.state = {
            toastList: []
        }
        this.deleteToast = this.deleteToast.bind(this);
    }
    static add = (value) => {
        var toastList = _this.state.toastList;
        var toastAddState = true;
        toastList.forEach((item, index) => {
            if (item.text === value) {
                toastAddState = false;
            }
        });
        if (toastAddState) {
            toastList.push({
                text: value,
                value: <AddToast key={key} delete={_this.deleteToast}>{value}</AddToast>
            });
            key++;
            _this.setState({ toastList: toastList })
        }
    };
    deleteToast() {
        var toastList = this.state.toastList;
        toastList.splice(0, 1);
        this.setState({ toastList: toastList })
    }
    render() {
        return (
            <View style={
  
  {
                position: "absolute",
                left: 0,
                bottom: 50,
                width: width,
                justifyContent: "center",
                alignItems: "center",
            }}>
                {this.state.toastList.map((item, index) => {
                    return item.value;
                })}
            </View>
        )
    }
}
export default ToastView;

使用方法

import React, { Component } from 'react';
import { ScrollView, StyleSheet, Text, View, Button } from 'react-native';
import Toast from '../../components/common/Toast';
var s = 0;
class ToastPage extends Component {
    static navigationOptions = {
        title: '提示',
    };
    constructor(props) {
        super(props);
    }
    open() {
        s++;
        Toast.add("测试,我是Toast 顺序:"+s);
    }
    render() {
        return (
            <ScrollView style={styles.pageStyle}>
                <Text style={
  
  { fontSize: 30 }}>Date</Text>
                <Button title="打开提示" onPress={this.open.bind(this)} />
            </ScrollView>
        );
    }
}
export default ToastPage;
const styles = StyleSheet.create({
    pageStyle: {
        backgroundColor: '#f5f5f5',
    },
});

示例图

React Native技术篇—自定义Toast弹窗「建议收藏」

项目GitHub地址:https://github.com/zhouwei1994/nativeCase.git

注意:未经允许不可私自转载,违者必究

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/189718.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • pycharm永久激活码 2021.5【在线注册码/序列号/破解码】「建议收藏」

    pycharm永久激活码 2021.5【在线注册码/序列号/破解码】,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月20日
    57
  • 五个优秀的硬盘检测工具[通俗易懂]

    五个优秀的硬盘检测工具[通俗易懂]虽然硬盘价格不断大幅跳水,但它显然一直是我们最宝贵的资产,硬盘一旦损坏,所有下载的音乐、铃声、软件,工作的文档、报表、模型、图片都不复存在。本文介绍五个最好的硬盘检测工具,它们各有特色,有的可以监视局

    2022年7月4日
    41
  • SpringBoot调用第三方接口[通俗易懂]

    SpringBoot调用第三方接口[通俗易懂]Spring框架提供的RestTemplate类可用于在应用中调用rest服务,它简化了与http服务的通信方式,统一了RESTful的标准,封装了http链接,我们只需要传入url及返回值类型即可。相较于之前常用的HttpClient,RestTemplate是一种更优雅的调用RESTful服务的方式。RestTemplate默认依赖JDK提供http连接的能力(HttpURLConnect…

    2022年6月10日
    139
  • 卸载vs2012的步骤_plsql卸载详细步骤

    卸载vs2012的步骤_plsql卸载详细步骤要卸载VS系列时才发现卸载VS还是很麻烦的事情,经过研究网上的卸载工具都没什么用,还是要自己手动卸载才彻底些。手动卸载步骤:VisualStudioExpressEditions进入控制面板,运行添加或删除程序 卸载”MSDNLibraryforVisualStudio2005Beta” 卸载”MicrosoftSQLServer2005Expr

    2022年9月23日
    3
  • 整合Druid—SpringBoot[通俗易懂]

    整合Druid—SpringBoot[通俗易懂]整合Druid(数据源)Druid简介Java程序很大一部分要操作数据库,为了提高性能操作数据库的时候,又不得不使用数据库连接池。Druid是阿里巴巴开源平台上一个数据库连接池实现,结合了C3P0、DBCP等DB池的优点,同时加入了日志监控。Druid可以很好的监控DB池连接和SQL的执行情况,天生就是针对监控而生的DB连接池。Druid已经在阿里巴巴部署了超过600个应用,经过一年多生产环境大规模部署的严苛考验。SpringBoot2.0以上默认使用Hikari

    2022年7月23日
    12
  • mysql查询语句select-(null,not null,is null和is not null)

    mysql查询语句select-(null,not null,is null和is not null)1一些概念  1.1null与notnull  null和notnull是mysql的字段属性,或称为数据类型的属性,不属于任何类型。null值也不等同于空值,空值是不占用存储空间的,null占用存储空间,如对myisam表占用1bit额外存储空间。  1.2isnull与isnotnull  isnull和isnotnull是mysq

    2022年6月1日
    126

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号