-
Notifications
You must be signed in to change notification settings - Fork 6
/
FlipCard.js
71 lines (62 loc) · 1.87 KB
/
FlipCard.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
import React,{Component} from "react";
import {
Animated,
Easing,
TouchableOpacity,
Text,
View
} from "react-native";
const styles = {
animatedContainer: {
flex: 1
}
};
class FlipCard extends Component{
constructor(){
super();
this._flipToggleCard = this._flipToggleCard.bind(this);
this._flippedCard = this._flippedCard.bind(this);
this.flippedCardView = this.flippedCardView.bind(this);
this.state = {
animatedValue: new Animated.Value(0),
isFlipped: true,
};
};
componentDidUpdate(prevProp, prevState) {
if (this.state.isFlipped !== prevState.isFlipped) {
this._flippedCard();
}
};
_flipToggleCard() {
this.setState({isFlipped: !this.state.isFlipped});
};
_flippedCard() {
Animated.spring(this.state.animatedValue, {
toValue: 0, // Returns to the start
velocity: this.props.velocity, // Velocity makes it move
tension: this.props.tension, // Slow
friction: this.props.friction, // Oscillate a lot
}).start();
};
render() {
const rotateX = this.state.animatedValue.interpolate({
inputRange: [0, 0.5, 1],
outputRange: ['0deg', '360deg', '0deg']
});
return (
<TouchableOpacity onPress={this._flipToggleCard} style={styles.animatedContainer}>
<Animated.View
style={[styles.animatedContainer,{transform: [{rotateX}]}]}>
{this.flippedCardView(this.state.isFlipped)}
</Animated.View>
</TouchableOpacity>);
};
flippedCardView(isFlipped) {
if (isFlipped) {
return this.props.renderFront;
} else {
return this.props.renderBack;
}
}
}
module.exports = FlipCard;