我用的是react-native-animatable库。基本上,当我加载我的应用程序时,动画就会运行,但是,当我转到另一个选项卡并返回初始页面时,动画就不再运行了。我想这是因为它不再被重新加载,我想知道如何重新加载一个组件。正如您所看到的,View有一个动画属性,它是必须加载的动画。
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>
);
}
}发布于 2018-05-17 18:55:18
如果您使用的是react-navigation,下面的解决方案可能对您有效。
创建一个函数,它将在几毫秒后启动动画,并将其作为参数传递给下一个屏幕。例如,
屏幕A
animateFunction() {
setTimeout(() => {
// start your animation
}, 100);
}navigation.navigate(SCREEN_NAME, { startPrevScreenAnimation: animateFunction });
在下一个屏幕中,当组件卸载时调用该函数(componentWillUnmount())。例如,
屏幕B
componentWillUnmount() {
this.props.navigation.state.params.startPrevScreenAnimation();
}我之所以说几毫秒,是因为您希望动画在屏幕过渡完成后开始播放。
或
在屏幕上添加一个监听器,它在屏幕处于焦点时触发一个事件。
if (this.props.navigation) {
this.willFocusSubscription = this.props.navigation.addListener(
'willFocus',
() => { // Run your animation },
);
}发布于 2018-05-23 20:56:59
谢谢你的帮助。我最终用一种不同的方法让它工作。我使用react导航中的withNavigationFocus来获取当前屏幕的isFocused属性。然后,我使用了一条if语句,如果屏幕被聚焦,则运行动画,否则就不运行动画。
从“react-navigation”导入{withNavigationFocus};
类配置文件扩展了React.Component {
constructor(props) {
super(props);
}
render() //这将检查当前屏幕是否被聚焦,然后运行动画,否则不运行。
{
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);// <-别忘了!!
https://stackoverflow.com/questions/50389237
复制相似问题