-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScene.js
94 lines (82 loc) · 2.39 KB
/
Scene.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import React, { Component } from 'react';
import {
Animated,
Dimensions
} from 'react-native';
const {width, height} = Dimensions.get('window');
const DEFAULT_CONFIG = {
duration: 250,
useNativeDriver: true
};
export default class Scene extends Component {
_progress = new Animated.Value(0)
static defaultProps = {
action: 'reset',
onAnimationDone: () => {}
}
_launchIncrement(config, callback) {
const animConfig = {...DEFAULT_CONFIG, ...config};
let fn = Animated.timing;
if (animConfig.fn) {
fn = animConfig.fn;
}
fn(this._progress, {
toValue: 1,
...animConfig
}).start(result => {
if (result.finished) {
callback();
}
});
}
_launchDecrement(config, callback) {
let animConfig = {...DEFAULT_CONFIG, ...config};
let fn = Animated.timing;
if (animConfig.fn) {
fn = animConfig.fn;
}
fn(this._progress, {
toValue: 0,
...animConfig
}).start(result => {
if (result.finished) {
callback();
}
});
}
_getAnimation() {
if (this.props.animation && this.props.animation.fn) {
// Use given transition animation
return this.props.animation.fn(this._progress);
}
}
componentWillReceiveProps(nextProps) {
if (nextProps.action) {
if (nextProps.action === 'blur') {
this._launchDecrement(nextProps.animation.config, () => this.props.onAnimationDone('blur'));
} else if (nextProps.action === 'focus') {
this._launchIncrement(nextProps.animation.config, () => this.props.onAnimationDone('focus'));
} else if (nextProps.action === 'pop' || nextProps.action === 'popModal') {
this._launchDecrement(nextProps.animation.config, () => this.props.onAnimationDone(nextProps.action));
} else if (nextProps.action === 'reset') {
this._progress.setValue(1);
this.props.onAnimationDone('reset');
}
}
}
componentDidMount() {
if (this.props.action === 'reset') {
this._progress.setValue(1);
this.props.onAnimationDone(this.props.action);
} else {
this._launchIncrement(this.props.animation.config, () => this.props.onAnimationDone(this.props.action));
}
}
render() {
return (
<Animated.View style={[{position: 'absolute', left: 0, top: 0, right: 0, bottom: 0}, this._getAnimation()]}>
{this.props.children}
</Animated.View>
);
}
}