-
Notifications
You must be signed in to change notification settings - Fork 42
/
index.js
144 lines (121 loc) · 3.7 KB
/
index.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
'use strict';
import { ViewPropTypes } from 'react-native';
var React = require('react');
var {
Component,
} = React;
var PropTypes = require('prop-types');
var ReactNative = require('react-native');
var {
View,
Easing,
StyleSheet,
Animated,
} = ReactNative;
class FlipView extends Component {
static propTypes = {
style: ViewPropTypes.style,
flipDuration: PropTypes.number,
flipEasing: PropTypes.func,
flipAxis: PropTypes.oneOf(['x', 'y']),
front: PropTypes.object,
back: PropTypes.object,
perspective: PropTypes.number,
onFlip: PropTypes.func,
onFlipped: PropTypes.func,
isFlipped: PropTypes.bool,
};
static defaultProps = {
style: {},
flipDuration: 500,
flipEasing: Easing.out(Easing.ease),
flipAxis: 'y',
perspective: 1000,
onFlip: () => {},
onFlipped: () => {},
isFlipped: false,
};
constructor(props) {
super(props);
var targetRenderState = this._getTargetRenderStateFromFlippedValue(props.isFlipped);
var frontRotationAnimatedValue = new Animated.Value(targetRenderState.frontRotation);
var backRotationAnimatedValue = new Animated.Value(targetRenderState.backRotation);
var interpolationConfig = {inputRange: [0, 1], outputRange: ["0deg", "360deg"]};
var frontRotation = frontRotationAnimatedValue.interpolate(interpolationConfig);
var backRotation = backRotationAnimatedValue.interpolate(interpolationConfig);
this.state = {
frontRotationAnimatedValue,
backRotationAnimatedValue,
frontRotation,
backRotation,
isFlipped: props.isFlipped,
};
}
componentWillReceiveProps = (nextProps) => {
if (nextProps.isFlipped !== this.props.isFlipped) {
this.flip();
}
};
_getTargetRenderStateFromFlippedValue = (isFlipped) => {
return {
frontRotation: isFlipped ? 0.5 : 0,
backRotation: isFlipped ? 1 : 0.5
};
};
render = () => {
var rotateProperty = this.props.flipAxis === 'y' ? 'rotateY' : 'rotateX';
return (
<View {...this.props}>
<Animated.View
pointerEvents={this.state.isFlipped ? 'none' : 'auto'}
style={[styles.flippableView, {transform: [{perspective: this.props.perspective}, {[rotateProperty]: this.state.frontRotation}]}]}>
{this.props.front}
</Animated.View>
<Animated.View
pointerEvents={this.state.isFlipped ? 'auto' : 'none'}
style={[styles.flippableView, {transform: [{perspective: this.props.perspective}, {[rotateProperty]: this.state.backRotation}]}]}>
{this.props.back}
</Animated.View>
</View>
);
};
flip = () => {
this.props.onFlip();
var nextIsFlipped = !this.state.isFlipped;
var {frontRotation, backRotation} = this._getTargetRenderStateFromFlippedValue(nextIsFlipped);
setImmediate(() => {
requestAnimationFrame(() => {
Animated.parallel([this._animateValue(this.state.frontRotationAnimatedValue, frontRotation, this.props.flipEasing),
this._animateValue(this.state.backRotationAnimatedValue, backRotation, this.props.flipEasing)]
).start(k => {
if (!k.finished) {
return;
}
this.setState({isFlipped: nextIsFlipped});
this.props.onFlipped(nextIsFlipped);
});
});
});
};
_animateValue = (animatedValue, toValue, easing) => {
return Animated.timing(
animatedValue,
{
toValue: toValue,
duration: this.props.flipDuration,
easing: easing
}
);
};
}
var styles = StyleSheet.create({
flippableView: {
position: 'absolute',
left: 0,
top: 0,
right: 0,
bottom: 0,
backfaceVisibility: 'hidden',
}
});
module.exports = FlipView;