Skip to content

Instantly share code, notes, and snippets.

@spencercarli
Created December 21, 2017 16:32
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spencercarli/58b5f8896b40c1f8ba9ad4bc2ce77ca9 to your computer and use it in GitHub Desktop.
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
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