Skip to content

Commit

Permalink
Merge pull request #1 from novom/change-circle-animation-according-to…
Browse files Browse the repository at this point in the history
…-time-elapsed

change circle animation according to time elapsed
  • Loading branch information
Steve authored Jun 13, 2018
2 parents 3265f9d + 21c297d commit e82f00c
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
} from 'react-native'
import PropTypes from 'prop-types'

const SEC_TO_MS = 1000;

// compatability for react-native versions < 0.44
const ViewPropTypesStyle = ViewPropTypes
? ViewPropTypes.style
Expand Down Expand Up @@ -69,10 +71,13 @@ function calcInterpolationValuesForHalfCircle2(

function getInitialState(props) {
const circleProgress = new Animated.Value(0)
const textProgress = new Animated.Value(0)

return {
circleProgress,
secondsElapsed: 0,
text: props.updateText(0, props.seconds),
textProgress,
interpolationValuesHalfCircle1: calcInterpolationValuesForHalfCircle1(
circleProgress,
props,
Expand Down Expand Up @@ -117,14 +122,16 @@ export default class PercentageCircle extends React.PureComponent {

this.state = getInitialState(props)
this.restartAnimation()
this.restartAnimationCircle()
}

componentWillReceiveProps(nextProps) {
if (
this.props.seconds !== nextProps.seconds
) {
this.state.circleProgress.stopAnimation()
this.setState(getInitialState(nextProps), this.restartAnimation)
this.state.textProgress.stopAnimation()
this.setState(getInitialState(nextProps), this.restartAnimation, this.restartAnimationCircle)
}
}

Expand All @@ -140,25 +147,27 @@ export default class PercentageCircle extends React.PureComponent {
secondsElapsed,
this.props.seconds,
)
this.setState(
{
...getInitialState(this.props),
secondsElapsed,
text: updatedText,
},
callback,
)
this.setState({secondsElapsed, text: updatedText }, callback )
};

restartAnimation = () => {
this.state.circleProgress.stopAnimation()
Animated.timing(this.state.circleProgress, {
this.state.textProgress.stopAnimation()
Animated.timing(this.state.textProgress, {
toValue: 100,
duration: 1000,
easing: Easing.linear,
}).start(this.onCircleAnimated)
};

restartAnimationCircle = () => {
this.state.circleProgress.stopAnimation()
Animated.timing(this.state.circleProgress, {
toValue: 100,
duration: this.props.seconds * SEC_TO_MS,
easing: Easing.linear,
}).start()
};

renderHalfCircle({ rotate, backgroundColor }) {
const { radius } = this.props

Expand Down

0 comments on commit e82f00c

Please sign in to comment.