Skip to content

Commit

Permalink
Improvement on SlideAnimation
Browse files Browse the repository at this point in the history
  • Loading branch information
jacklam718 committed Apr 3, 2019
1 parent ef0e4b0 commit d81d0e7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
2 changes: 1 addition & 1 deletion dist/animations/SlideAnimation.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 20 additions & 11 deletions src/animations/SlideAnimation.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,28 @@
import { Animated, Dimensions } from 'react-native';
import Animation, { type AnimationConfig } from './Animation';

const {
width: SCREEN_WIDTH,
height: SCREEN_HEIGHT,
} = Dimensions.get('window');

type SlideFrom = 'top' | 'bottom' | 'left' | 'right';
type SlideAnimationConfig = AnimationConfig & {
slideFrom?: SlideFrom,
}

const { width: SCREEN_WIDTH, height: SCREEN_HEIGHT } = Dimensions.get('window');

export default class SlideAnimation extends Animation {
slideFrom: SlideFrom
slideFrom: SlideFrom;

static SLIDE_FROM_TOP = 'top';
static SLIDE_FROM_BOTTOM = 'bottom';
static SLIDE_FROM_LEFT = 'left';
static SLIDE_FROM_RIGHT = 'right';

constructor({
initialValue = 0,
useNativeDriver = true,
slideFrom = 'bottom',
slideFrom = SlideAnimation.SLIDE_FROM_BOTTOM,
}: SlideAnimationConfig = {}) {
super({ initialValue, useNativeDriver });
this.slideFrom = slideFrom;
Expand Down Expand Up @@ -44,37 +52,38 @@ export default class SlideAnimation extends Animation {

getAnimations(): Object {
const transform = [];
if (this.slideFrom === 'top') {
if (this.slideFrom === SlideAnimation.SLIDE_FROM_TOP) {
transform.push({
translateY: this.animate.interpolate({
inputRange: [0, 1],
outputRange: [-SCREEN_HEIGHT, 0],
}),
});
}
if (this.slideFrom === 'bottom') {
} else if (this.slideFrom === SlideAnimation.SLIDE_FROM_BOTTOM) {
transform.push({
translateY: this.animate.interpolate({
inputRange: [0, 1],
outputRange: [SCREEN_HEIGHT, 0],
}),
});
}
if (this.slideFrom === 'left') {
} else if (this.slideFrom === SlideAnimation.SLIDE_FROM_LEFT) {
transform.push({
translateX: this.animate.interpolate({
inputRange: [0, 1],
outputRange: [-SCREEN_WIDTH, 0],
}),
});
}
if (this.slideFrom === 'right') {
} else if (this.slideFrom === SlideAnimation.SLIDE_FROM_RIGHT) {
transform.push({
translateX: this.animate.interpolate({
inputRange: [0, 1],
outputRange: [SCREEN_WIDTH, 0],
}),
});
} else {
throw new Error(`
slideFrom: ${this.slideFrom} not supported. 'slideFrom' must be 'top' | 'bottom' | 'left' | 'right'
`);
}
return {
transform,
Expand Down

0 comments on commit d81d0e7

Please sign in to comment.