Skip to content

Instantly share code, notes, and snippets.

@cssoul
Created July 22, 2015 02:48
Show Gist options
  • Save cssoul/eda63b173311a323653b to your computer and use it in GitHub Desktop.
Save cssoul/eda63b173311a323653b to your computer and use it in GitHub Desktop.
React Native Loading Animation
/**
* LoadingView
*/
'use strict';
var React = require('react-native');
var {
TouchableWithoutFeedback,
Animated,
StyleSheet,
Image,
View,
Easing
} = React;
var TIMES = 400;
var LoadingView = React.createClass({
getInitialState() {
return {
angle: new Animated.Value(0),
};
},
componentDidMount() {
this._animate();
},
_animate() {
this.state.angle.setValue(0);
this._anim = Animated.timing(this.state.angle, {
toValue: 360*TIMES,
duration: 800*TIMES,
easing: Easing.linear
}).start(this._animate);
},
componentDidUnmount:function(){
},
render() {
return (
<View style={styles.container}>
<Animated.Image
source={{uri: 'MGJControls.bundle/loading_circle.png'}}
style={[
styles.rotateCard,
{transform: [
{rotate: this.state.angle.interpolate({
inputRange: [0, 360],
outputRange: ['0deg', '360deg']
})},
]}]}>
</Animated.Image>
</View>
);
}
});
var styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center'
},
rotateCard: {
width:35,
height:35,
justifyContent:'center',
backgroundColor:'transparent'
}
});
module.exports = LoadingView;
@cmhsu
Copy link

cmhsu commented Sep 11, 2015

Hi great work! When I tried this, the animation was kind of choppy. It would stop for a split second every second or so. Is there a way to have a smooth effect? Thank you!

@zachgibson
Copy link

This is amazing. Thanks for this!

@virgial
Copy link

virgial commented May 5, 2016

Very nice work! A little comment. componentDidUnmount is not a valid method. please correct this with componentWillUnmount.
You can add the option to stop the animation in there.
componentWillUnmount()
{
this._anim.stopAnimation();
}

@rclai
Copy link

rclai commented May 19, 2016

You have to change the duration value to make it smoother.

@codeofdiego
Copy link

How would this work to animate, for example, from 0 to 270deg and stop?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment