首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >重新加载动画React-native-animatable库

重新加载动画React-native-animatable库
EN

Stack Overflow用户
提问于 2018-05-17 18:30:25
回答 2查看 1.2K关注 0票数 1

我用的是react-native-animatable库。基本上,当我加载我的应用程序时,动画就会运行,但是,当我转到另一个选项卡并返回初始页面时,动画就不再运行了。我想这是因为它不再被重新加载,我想知道如何重新加载一个组件。正如您所看到的,View有一个动画属性,它是必须加载的动画。

代码语言:javascript
复制
import React, { Component } from 'react';
import { Text, Button, StyleSheet, Image, ImageBackground, TouchableOpacity } from 'react-native';
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
import LinearGradient from 'react-native-linear-gradient';
import {Fonts} from '../components/Fonts';
import { createAnimatableComponent, View, } from 'react-native-animatable';


class Home extends React.Component {

    render() {

        return (
            <View style={styles.container}>

               <View animation='bounceInLeft'    
               style={styles.container1}>

                    <View style={styles.card1}>


                                <ImageBackground 
                                source={require('../images/pictures/runop.jpg')} 
                                style={{width:'100%', height:200, }}>


                                    <Text
                                        style={{fontSize:30, alignSelf:'center', color:'white',
                                        fontFamily:Fonts.Nunito}}
                                        > Sport Schema's</Text>

                                </ImageBackground>

                   </View>

               </View>


               <View animation='bounceInRight'  style={styles.container2}>

                    <View style={styles.card2}>
                        <Image 
                            source={require('../images/pictures/foodop.jpg')} 
                            style={{width:'100%', height:200}}/>   

                    </View>

               </View>

               <View animation='bounceInLeft'  style={styles.container3}>

                    <View style={styles.card3}>
                        <Image 
                            source={require('../images/pictures/blogop.jpg')} 
                            style={{width:'100%', height:200}}/>   

                    </View>

               </View>   

            </View>

        );

    }
}
EN

回答 2

Stack Overflow用户

发布于 2018-05-17 18:55:18

如果您使用的是react-navigation,下面的解决方案可能对您有效。

创建一个函数,它将在几毫秒后启动动画,并将其作为参数传递给下一个屏幕。例如,

屏幕A

代码语言:javascript
复制
animateFunction() {
  setTimeout(() => {
    // start your animation
  }, 100);
}

navigation.navigate(SCREEN_NAME, { startPrevScreenAnimation: animateFunction });

在下一个屏幕中,当组件卸载时调用该函数(componentWillUnmount())。例如,

屏幕B

代码语言:javascript
复制
componentWillUnmount() {
  this.props.navigation.state.params.startPrevScreenAnimation();
}

我之所以说几毫秒,是因为您希望动画在屏幕过渡完成后开始播放。

在屏幕上添加一个监听器,它在屏幕处于焦点时触发一个事件。

代码语言:javascript
复制
if (this.props.navigation) {
  this.willFocusSubscription = this.props.navigation.addListener(
    'willFocus',
    () => { // Run your animation },
  );
}
票数 0
EN

Stack Overflow用户

发布于 2018-05-23 20:56:59

谢谢你的帮助。我最终用一种不同的方法让它工作。我使用react导航中的withNavigationFocus来获取当前屏幕的isFocused属性。然后,我使用了一条if语句,如果屏幕被聚焦,则运行动画,否则就不运行动画。

从“react-navigation”导入{withNavigationFocus};

类配置文件扩展了React.Component {

代码语言:javascript
复制
constructor(props) {
    super(props);

}        
render() 

//这将检查当前屏幕是否被聚焦,然后运行动画,否则不运行。

代码语言:javascript
复制
{

    if (this.props.isFocused && this.animation) {
        this.animation.bounce(800)
     }

    return (
        <View ref={(ref) =>  {this.animation = ref;}}

        style={styles.container3}>

            <View style={styles.card3}>
            <Text> hii</Text>

            </View>
        </View>

    );

}

}

});导出默认配置文件(WithNavigationFocus);// <-别忘了!!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50389237

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档