加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

react-native – 使React Native Modal从上到下显示

发布时间:2020-12-15 20:16:57 所属栏目:百科 来源:网络整理
导读:我注意到 Modal组件的animationType属性只允许它从下到上滑动.我怎么能改变动画并让模态从上到下出现? 谢谢你的时间. 解决方法 看起来该组件不允许这种类型的配置. 您可以做的一件事是使用动画库来创建自己的模态.您可以将translateY属性设置为设备高度的负
我注意到 Modal组件的animationType属性只允许它从下到上滑动.我怎么能改变动画并让模态从上到下出现?

谢谢你的时间.

解决方法

看起来该组件不允许这种类型的配置.

您可以做的一件事是使用动画库来创建自己的模态.您可以将translateY属性设置为设备高度的负值,然后将translateY值设置为0:

openModal() {
    Animated.timing(this.state.modalY,{
        duration: 300,toValue: 0
     }).start();
  },closeModal() {
    Animated.timing(this.state.modalY,toValue: -deviceHeight
     }).start();
  },

完整实现如下:

'use strict';

var React = require('react-native');
var {
  AppRegistry,StyleSheet,Text,View,TouchableHighlight,Animated,Dimensions
} = React;

let deviceHeight = Dimensions.get('window').height
var deviceWidth = Dimensions.get('window').width

var SampleApp = React.createClass({

  openModal() {
    Animated.timing(this.state.modalY,getInitialState(){
    return {
        modalY: new Animated.Value(-deviceHeight)
    }
  },render() {
     var Modal = <Animated.View style={[ styles.modal,{ transform: [                        {translateY: this.state.modalY}] }]}>
                                <TouchableHighlight onPress={ this.closeModal } underlayColor="green" style={ styles.button }>
                    <Text style={ styles.buttonText }>Close Modal</Text>
                  </TouchableHighlight>
                             </Animated.View>

    return (
      <View style={styles.container}>
       <TouchableHighlight onPress={ this.openModal } underlayColor="green" style={ styles.button }>
        <Text style={ styles.buttonText }>Show Modal</Text>
       </TouchableHighlight>
      { Modal }
      </View>
    );
  }
});

var styles = StyleSheet.create({
  container: {
    flex: 1,justifyContent: 'center'
  },button: {
    backgroundColor: 'green',alignItems: 'center',height: 60,justifyContent: 'center',},buttonText: {
    color: 'white'
  },modal: {
    height: deviceHeight,width: deviceWidth,position: 'absolute',top:0,left:0,backgroundColor: '#ededed',}
});

AppRegistry.registerComponent('SampleApp',() => SampleApp);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读