【问题标题】:React Native Modal doesn't show animationsReact Native Modal 不显示动画
【发布时间】:2019-04-12 09:30:07
【问题描述】:

所以在我的应用程序中,我有一个工作模式,显示可见性是否设置为 true。它也可以正确关闭并且一切正常(除了如果您重新加载模拟器并且 Modal 打开它会保持打开状态并且您无法关闭它的错误)。

我正在使用 react-native-modal 包。

我的问题是动画在模态中不起作用。 如果我将值设置为“slideInLeft”,“animationIn”道具不会显示任何变化,“animationOut”道具也不会发生任何变化。

有人知道为什么会这样吗?

      <View>
        <Modal
          isVisible={this.props.visible}
          onBackButtonPress={this.props.toggle}
          animationIn="slideInLeft"
          animationOut="slideOutRight"
        >
          <View style={modalStyle.container}>
            <View style={modalStyle.headerText}>
              <Text style={{ textAlign: "center", color: "black", 
                fontWeight:"bold" }}>
                Projectbrowser
              </Text>
            </View>

           { SOME MORE CODE IN BETWEEN HERE }

          </View>
        </Modal>
      </View>

为了简单起见,我删掉了一些代码。如果您遇到相同的问题,任何修复和支持表示赞赏。

【问题讨论】:

    标签: javascript reactjs react-native


    【解决方案1】:

    确保将 useNativeDriver 属性设置为 true,如下所示:

    <View>
      <Modal
              isVisible={this.props.visible}
              onBackButtonPress={this.props.toggle}
              animationIn="slideInLeft"
              animationOut="slideOutRight"
              useNativeDriver={true} // try adding This line
            >
           <View style={modalStyle.container}>
              <View style={modalStyle.headerText}>
                 <Text style={{ textAlign: "center", color: "black", 
                    fontWeight:"bold" }}>
                    Projectbrowser
                 </Text>
              </View>
    
            { SOME MORE CODE IN BETWEEN HERE }
    
          </View>
      </Modal>
    </View>
    

    【讨论】:

    • 谢谢哥们,我的模态出现了一些抖动和滞后现象,这解决了我的问题
    【解决方案2】:

    你可以在这个Modal中使用这行代码..

    animationType="slide"
    

    此动画道具将有助于将模态视图设置为幻灯片格式。

    这对我有帮助,你可以像 as 一样使用这段代码

    <Modal
              animationType="slide"
              transparent={false}
              visible={this.state.modalVisible}
              onRequestClose={() => {
                Alert.alert('Modal has been closed.');
              }}>
    

    【讨论】:

    • 由于某种原因,animationType 适用于幻灯片,但它不是来自包,因此它在黑色背景中滑动,然后我的实际模态加载正常但非常慢。 animationType 不适用于 react-native-modal。
    • 它实际上可能工作,因为它从底部滑入,但它的整体速度非常慢,模态在 5 秒后出现,但没有动画类型它也很慢,所以可能是由于模拟器速度慢。谢谢,我会更深入地研究它。
    • 模拟器,这也许是它这么慢的原因。
    【解决方案3】:

    我在这里分享世博小吃链接,请检查它是否有效。我已经为你的问题做了一个演示。

    通过它,让我知道。我刚刚更改了你的 props 状态,并根据 TouchableOpacity 更改了它的值。

    https://snack.expo.io/SJepmJRtV

    Example.js

      export default class App extends React.Component {
      constructor(props) {
      super(props);
      this.state = {visible:false,toggle:false}}
      render() {
        return (
          <View>
          <View>
            <Modal
              isVisible={this.state.visible}
              onBackButtonPress={this.state.toggle}
              animationIn="slideInLeft"
              animationOut="slideOutRight"
            >
              <View style={{width:100,height:200}}>
                <View style={{}}>
                  <Text style={{ textAlign: "center", color: "black", 
                    fontWeight:"bold" }}>
                    Projectbrowser
                  </Text>
                <TouchableOpacity style={{width:"10%",height:"10%",backgroundColor:"red",justifyContent:"center",alignItems:"center"}} onPress={()=>this.setState({visible:false})}>
          <Text>Press me</Text>
          </TouchableOpacity>
                </View>
              </View>
            </Modal>
          </View>
          <TouchableOpacity style={{width:"20%",height:"80%",bottom:150,alignItems:"center"}} onPress={()=>this.setState({visible:true})}>
          <Text>Press me</Text>
          </TouchableOpacity>
          </View>
         );
       }
     }
    

    【讨论】:

    • 我只得到了一个白屏,博览会小吃不适合我。
    • Expo 仍然不起作用,示例也不起作用,我在 animationIn" 或 "Out 中放入的任何东西都没有改变任何东西。
    猜你喜欢
    • 1970-01-01
    • 2021-06-12
    • 1970-01-01
    • 2016-02-29
    • 2021-09-19
    • 1970-01-01
    • 2016-06-06
    • 1970-01-01
    • 2018-02-03
    相关资源
    最近更新 更多