简体   繁体   English

在React Native中完成动画时触发功能吗?

[英]Trigger a function when an animation is completed in React Native?

I'm implementing a Splash Screen using React Native and i'm simply using a StackNavigator to change the route to the Main activity when the animations in Splash Screen are completed. 我正在使用React Native实现一个启动画面,而我只是在完成启动画面中的动画时,使用StackNavigator路由更改为Main活动。 To do this, I'm trying to find a way for triggering the this.props.navigation.navigate('Main') function. 为此,我试图找到一种触发this.props.navigation.navigate('Main')函数的方法。

The question is that how i can implement the onComplete function in my animation class? 问题是我如何在动画类中实现onComplete函数?

Here is my animation code: 这是我的动画代码:

import React from 'react';
import { Animated, Text, View } from 'react-native';

class FadeInView extends React.Component {
state = {
    fadeAnim: new Animated.Value(0),  // Initial value for opacity: 0
}

componentDidMount() {
    Animated.timing(                  // Animate over time
    this.state.fadeAnim,            // The animated value to drive
    {
        toValue: 1,                   // Animate to opacity: 1 (opaque)
        duration: 1000,              // Make it take a while
    }
    ).start();                        // Starts the animation
}

render() {
    let { fadeAnim } = this.state;

    return (
    <Animated.View                 // Special animatable View
        style={{
        ...this.props.style,
        opacity: fadeAnim,         // Bind opacity to animated value
        }}
    >
        {this.props.children}
    </Animated.View>
    );
}
}

export default FadeInView

and my SplashScreen : 和我的SplashScreen

import React, { Component } from 'react';
import {
StyleSheet,
Text,
View,
Image
} from 'react-native';
import FadeInView from '../Animations/FadeInAnimation';
import { StackNavigator } from 'react-navigation';
import RanjoorMain from './RanjoorMain';

class SplashScreen extends Component {
render() {
    return (
    <View style={styles.container}>
        <View style={{flex:1}}></View>
        <FadeInView style={{width: 230, height: 230, backgroundColor: 'transparent'}}>
            <Image style={styles.introLogo}
            source={require('../Images/Logo/Ranjoor.png')}
            />
        </FadeInView>
        <View style={{flex:1}}></View>
    </View>
    );
}
}

const styles = StyleSheet.create({
container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#3863cc',
},
welcome: {
    fontSize: 45,
    textAlign: 'center',
    margin: 10,
},
instructions: {
    textAlign: 'center',
    color: '#242329',
    marginBottom: 5,
},
introLogo: {
    width: 230,
    height: 230
}
});

/* This StackNavigator will change the route to the RanjoorMain.js after a couple of ms */
const GoToRanjoorMain = StackNavigator({
    Splash: { screen: Splas },
    Main: { screen: RanjoorMain }
})

GoToRanjoorMain.navigationOptions = {
title: 'Ranjoor', 
};

export default SplashScreen

You can provide a completion handler to that start method of an animation. 您可以为动画的start方法提供完成处理程序。

Animated.timing(              
    this.state.fadeAnim,          
    {
        toValue: 1,        
        duration: 1000,            
    }
).start(() => nav.goToSomeScreen()); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM