Created
December 21, 2017 16:32
-
-
Save spencercarli/58b5f8896b40c1f8ba9ad4bc2ce77ca9 to your computer and use it in GitHub Desktop.
React Native Animated.Sequence that calls a function each time a step in the sequence starts
This file contains hidden or 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
const customAnimatedSequence = (animations, onStepStart) => { | |
let current = 0; | |
return { | |
start: (callback) => { | |
const onComplete = (result) => { | |
if (!result.finished) { | |
callback && callback(result); | |
return; | |
} | |
current += 1; | |
if (current === animations.length) { | |
callback && callback(result); | |
return; | |
} | |
onStepStart && onStepStart({ current }); | |
animations[current].start(onComplete); | |
}; | |
if (animations.length === 0) { | |
callback && callback({ finished: true }); | |
} else { | |
onStepStart && onStepStart({ current }); | |
animations[current].start(onComplete); | |
} | |
}, | |
stop: () => { | |
if (current < animations.length) { | |
animations[current].stop(); | |
} | |
}, | |
reset: () => { | |
animations.forEach((animation, idx) => { | |
if (idx <= current) { | |
animation.reset(); | |
} | |
}); | |
current = 0; | |
}, | |
_startNativeLoop: () => { | |
throw new Error('Loops run using the native driver cannot contain Animated.sequence animations'); | |
}, | |
_isUsingNativeDriver: () => false, | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment