-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
129 lines (119 loc) · 3.46 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
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import {
Animated,
Easing,
Platform,
StyleSheet,
View,
ViewPropTypes,
} from 'react-native';
class FlipView extends Component {
static propTypes = {
...ViewPropTypes,
front: PropTypes.node.isRequired,
back: PropTypes.node.isRequired,
flipDuration: PropTypes.number,
flipEasing: PropTypes.func,
flipAxis: PropTypes.oneOf(['x', 'y']),
perspective: PropTypes.number,
onFlipStart: PropTypes.func,
onFlipEnd: PropTypes.func,
isFlipped: PropTypes.bool,
};
static defaultProps = {
flipDuration: 500,
flipEasing: Easing.out(Easing.ease),
flipAxis: 'y',
perspective: 1000,
onFlipStart: () => {},
onFlipEnd: () => {},
isFlipped: false,
};
constructor(props) {
super(props);
this._flipAnim = new Animated.Value(props.isFlipped ? 1 : 0);
this.state = { isFlipped: props.isFlipped };
}
componentWillReceiveProps(nextProps) {
if (nextProps.isFlipped !== this.props.isFlipped) {
this.flip();
}
}
flip() {
this.props.onFlipStart(this.state.isFlipped);
const nextIsFlipped = !this.state.isFlipped;
Animated.timing(
this._flipAnim,
{
toValue: nextIsFlipped ? 1 : 0,
duration: this.props.flipDuration,
easing: this.props.flipEasing,
useNativeDriver: true,
},
).start(({ finished }) => {
if (!finished) {
return;
}
this.setState({ isFlipped: nextIsFlipped });
this.props.onFlipEnd(nextIsFlipped);
});
}
render() {
const rotateProperty = this.props.flipAxis === 'y' ? 'rotateY' : 'rotateX';
return (
<View {...this.props}>
<Animated.View
pointerEvents={this.state.isFlipped ? 'none' : 'auto'}
style={[
StyleSheet.absoluteFill,
styles.flippableView,
{
transform: [
{ perspective: this.props.perspective },
{ [rotateProperty]: this._flipAnim.interpolate({ inputRange: [0, 1], outputRange: ['0deg', '180deg'] }) },
],
},
// Android does not support backfaceVisibility
// https://github.com/facebook/react-native/issues/9718
Platform.select({
android: {
opacity: this._flipAnim.interpolate({ inputRange: [0, 0.5, 0.5], outputRange: [1, 1, 0] }),
},
}),
]}
>
{this.props.front}
</Animated.View>
<Animated.View
pointerEvents={this.state.isFlipped ? 'auto' : 'none'}
style={[
StyleSheet.absoluteFill,
styles.flippableView,
{
transform: [
{ perspective: this.props.perspective },
{ [rotateProperty]: this._flipAnim.interpolate({ inputRange: [0, 1], outputRange: ['180deg', '360deg'] }) },
],
},
// Android does not support backfaceVisibility
// https://github.com/facebook/react-native/issues/9718
Platform.select({
android: {
opacity: this._flipAnim.interpolate({ inputRange: [0.5, 0.5, 1], outputRange: [0, 1, 1] }),
},
}),
]}
>
{this.props.back}
</Animated.View>
</View>
);
}
}
const styles = StyleSheet.create({
flippableView: {
backfaceVisibility: 'hidden',
},
});
export default FlipView;