Created
July 4, 2019 15:13
-
-
Save arafeek/6ae18862c7b97acd4223fbddab39596d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { Animated, Easing } from 'react-native'; | |
import { LinearGradient as SvgLinearGradient, Stop } from 'react-native-svg'; | |
const AnimatedStop = Animated.createAnimatedComponent(Stop); | |
export class AnimatedLinearGradient extends React.Component<LinearGradientProps> { | |
constructor(props) { | |
super(props); | |
this.state = { | |
offsetOne: new Animated.Value(0), | |
offsetTwo: new Animated.Value(0.1), | |
offsetThree: new Animated.Value(0.3), | |
}; | |
this._mounted = false; | |
} | |
componentDidMount() { | |
this._mounted = true; | |
this.startAnimation(); | |
} | |
componentWillUnmount() { | |
this._mounted = false; | |
} | |
startAnimation() { | |
if (!this._mounted) return; | |
Animated.loop( | |
Animated.parallel([ | |
Animated.timing(this.state.offsetOne, { | |
toValue: 1, | |
duration: 1000, | |
// easing: Easing.out(Easing.exp), | |
delay: 400, | |
// useNativeDriver: true, | |
}), | |
Animated.timing(this.state.offsetTwo, { | |
toValue: 1, | |
duration: 700, | |
//easing: Easing.out(Easing.exp), | |
delay: 400, | |
// useNativeDriver: true, | |
}), | |
Animated.timing(this.state.offsetThree, { | |
toValue: 1, | |
duration: 400, | |
//easing: Easing.out(Easing.exp), | |
delay: 400, | |
// useNativeDriver: true, | |
}), | |
]), | |
).start(); | |
} | |
render() { | |
const { primaryColor, secondaryColor, ...restProps } = this.props; | |
return ( | |
<SvgLinearGradient {...restProps}> | |
<AnimatedStop offset={this.state.offsetOne} stopColor={primaryColor} opacity={1} /> | |
<AnimatedStop offset={this.state.offsetTwo} stopColor={secondaryColor} opacity={1} /> | |
<AnimatedStop offset={this.state.offsetThree} stopColor={primaryColor} opacity={1} /> | |
</SvgLinearGradient> | |
); | |
} | |
} | |
// Usage | |
// <AnimatedLinearGradient | |
// x1="0%" | |
// y1="0%" | |
// x2="0%" | |
// y2="100%" | |
// id="areaFill" | |
// primaryColor={'#F2DEB1'} | |
// secondaryColor={'#D68D1C'} | |
// /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment