-
Notifications
You must be signed in to change notification settings - Fork 18
/
Example.js
128 lines (116 loc) · 2.79 KB
/
Example.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
import React, { Component } from 'react';
import {
View,
Text,
StyleSheet,
Animated,
TouchableOpacity,
} from 'react-native';
import InfiniteCarousel from './InfiniteCarousel';
const animals = [
{
name: 'Leon',
color: '#2969B0',
},
{
name: 'Cat',
color: '#FBA026',
},
{
name: 'Elephant',
color: '#E14938',
},
{
name: 'Unicorn',
color: '#9365B8',
},
];
const HEIGHT = 300;
const Animal = ({ name, color, animatedScale, index, style }) => (
<View style={[{ backgroundColor: color }, styles.animal]}>
<TouchableOpacity onPress={() => console.log(`Selected index is ${index}`)}>
<Animated.View
style={[
{
transform: [{ scale: animatedScale }],
},
style,
styles.animalAnimatedBox,
]}>
<Text style={{ color, fontWeight: 'bold' }}>{name}</Text>
</Animated.View>
</TouchableOpacity>
</View>
);
class Example extends Component {
state = {
dimensions: {},
};
_isSameMeasure = (measurement1, measurement2) =>
measurement1.width === measurement2.width &&
measurement1.height === measurement2.height;
_onLayout = ({ nativeEvent }) => {
const dimensions = nativeEvent.layout;
if (!this._isSameMeasure(this.state.dimensions, dimensions)) {
this.setState({ dimensions });
}
};
render() {
const dynamicContainerStyle = {
height: HEIGHT,
width: this.state.dimensions.width,
};
const RECTANGLE_RATIO = 0.8;
const MIN_SCALE = 0.7;
const MAX_SCALE = 1;
// we will pass an array of functions as children
const pages = animals.map((animal, index) =>
(animatedPosition, pageWidth, pageOffset) => {
const height = pageWidth * 0.8;
const width = height * RECTANGLE_RATIO;
return (
<Animal
{...animal}
index={index}
style={{ width, height }}
animatedScale={animatedPosition.interpolate({
inputRange: [
pageOffset - pageWidth,
pageOffset,
pageOffset + pageWidth,
],
outputRange: [MIN_SCALE, MAX_SCALE, MIN_SCALE],
})}
/>
);
});
return (
<View style={styles.container} onLayout={this._onLayout}>
<View style={dynamicContainerStyle}>
<InfiniteCarousel>
{pages}
</InfiniteCarousel>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
horizontalWrapper: {
flexDirection: 'row',
},
animal: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
animalAnimatedBox: {
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#ffffff',
},
});
export default Example;