forked from wassgha/react-native-rotating-view
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·121 lines (106 loc) · 2.67 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
/**
* @providesModule RotatingView
*/
'use strict';
import _ from 'lodash';
var React = require('react');
var createReactClass = require('create-react-class');
var {
View,
Animated,
StyleSheet
} = require('react-native');
var Orientation = require('react-native-orientation')
var RotatingView = createReactClass({
getDefaultProps: function() {
return {
duration: 120,
};
},
getInitialState: function() {
return {
orientation: Orientation.getInitialOrientation() == 'LANDSCAPE' ? 'LANDSCAPE-RIGHT' : 'PORTRAIT',
animation: new Animated.Value(0)
};
},
componentDidMount: function() {
Orientation.addSpecificOrientationListener(this._orientationDidChange);
},
componentWillUnmout: function() {
Orientation.removeSpecificOrientationListener(this._orientationDidChange);
},
_orientationDidChange: function(orientation) {
if (orientation === 'LANDSCAPE-LEFT' ||
orientation === 'LANDSCAPE-RIGHT' ||
orientation === 'PORTRAIT') {
this.setState({
orientation: orientation
});
}
if (orientation === 'LANDSCAPE-LEFT') {
Animated.timing(
this.state.animation,
{
toValue: 90,
duration: this.props.duration,
}
).start();
} else if (orientation === 'LANDSCAPE-RIGHT') {
Animated.timing(
this.state.animation,
{
toValue: -90,
duration: this.props.duration,
}
).start();
} else if (orientation === 'PORTRAIT') {
Animated.timing(
this.state.animation,
{
toValue: 0,
duration: this.props.duration,
}
).start();
}
},
render: function() {
var { orientation, animation } = this.state;
var {
style,
portraitStyle,
landscapeLeftStyle,
landscapeRightStyle,
children,
...props
} = this.props;
var interpolatedRotateAnimation = animation.interpolate({
inputRange: [-90, 0, 90],
outputRange: ['-90deg', '0deg', '90deg']
});
var content = (
<Animated.View
style={[
style,
styles.container,
{
transform: [{rotate: interpolatedRotateAnimation}]
},
orientation == 'PORTRAIT' ? portraitStyle : null,
orientation == 'LANDSCAPE-LEFT' ? landscapeLeftStyle : null,
orientation == 'LANDSCAPE-RIGHT' ? landscapeRightStyle : null
]}
{...props}
>
{children}
</Animated.View>
);
return content;
}
});
var styles = StyleSheet.create({
container: {
overflow: 'hidden',
position: 'relative',
}
});
module.exports = RotatingView;