博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
React-Native用动画写一个弹窗
阅读量:4087 次
发布时间:2019-05-25

本文共 2371 字,大约阅读时间需要 7 分钟。

效果图

image.png

提供的方法 和 属性

ref.show() // ref 主动调用显示打开  ref.hide() // ref 主动调用隐藏关闭  modalBoxHeight: 300, // 盒子高度  modalBoxBg: '#fff', // 盒子背景色  hide: function () { }, // 关闭时的回调函数  transparentIsClick: true  // 透明区域是否可以点击

简单使用方式

import React, { Component } from 'react'import PopUp from './PopUp'import { View, Text, TouchableOpacity } from 'react-native'export default class App extends Component {  render() {    return (      
{ this.popUp.show() }}>打开弹框
this.popUp = ref}>
{ this.popUp.hide() }} style={
{ alignItems: 'center', backgroundColor: '#316DE6', height: 45, width: 80, borderRadius: 8, alignSelf: 'center', justifyContent: 'center', marginTop: 50 }}>
关闭弹框
) }}

组件代码

import React, { Component } from 'react'import { StyleSheet, View, TouchableOpacity, Animated, Easing, Dimensions } from 'react-native'/** * 弹出层 */const { width, height } = Dimensions.get('window')export default class PopUp extends Component {  constructor(props) {    super(props)    this.state = {      offset: new Animated.Value(0),      show: false    }  }  in() {    Animated.timing(      this.state.offset,      {        easing: Easing.linear,        duration: 300,        toValue: 1      }    ).start()  }  out() {    Animated.timing(      this.state.offset,      {        easing: Easing.linear,        duration: 300,        toValue: 0      }    ).start()    setTimeout(      () => this.setState({ show: false }),      300    )  }  show() {    this.setState({      show: true    }, this.in())  }  hide() {    this.out()  }  defaultHide() {    this.props.hide()    this.out()  }  render() {    let { transparentIsClick, modalBoxBg, modalBoxHeight } = this.props    if (this.state.show) {      return (        
{/*
*/}
{this.props.children}
) } return
}}const styles = StyleSheet.create({ container: { width: width, backgroundColor: 'rgba(0, 0, 0, 0.6)', position: 'absolute', top: 0, zIndex: 9 }, modalBox: { position: 'absolute', width: width }})PopUp.defaultProps = { modalBoxHeight: 300, // 盒子高度 modalBoxBg: '#fff', // 背景色 hide: function () { }, // 关闭时的回调函数 transparentIsClick: true // 透明区域是否可以点击}

 

转载地址:http://vpgni.baihongyu.com/

你可能感兴趣的文章
乘法逆元
查看>>
STL源码分析----神奇的 list 的 sort 算法实现
查看>>
Linux下用math.h头文件
查看>>
Linux中用st_mode判断文件类型
查看>>
Ubuntu修改host遇到unable to resolve host
查看>>
路由选择算法
查看>>
Objective-C 基础入门(一)
查看>>
Objective-C 基础入门(三) 读写文件与回调
查看>>
C++ STL标准库与泛型编程(一)概述
查看>>
C++ STL标准库与泛型编程(四)Deque、Queue、Stack 深度探索
查看>>
C++ STL标准库 算法
查看>>
JVM内存模型_Minor GC笔记
查看>>
SpringCloud学习之PassCloud——(一)PassCloud源代码下载
查看>>
Linux下安装Python环境并部署NLP项目
查看>>
Nginx篇-springCloud配置Gateway+Nginx进行反向代理和负载均衡
查看>>
Nginx篇-Nginx配置动静分离
查看>>
缓存篇-Redis缓存失效以及解决方案
查看>>
缓存篇-使用Redis进行分布式锁应用
查看>>
缓存篇-Redisson的使用
查看>>
phpquery抓取网站内容简单介绍
查看>>