-
Notifications
You must be signed in to change notification settings - Fork 0
/
Aimated_top_tab.js
221 lines (202 loc) · 6.56 KB
/
Aimated_top_tab.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// import * as React from 'react';
// import { StatusBar, FlatList, Image, ImageBackground, Animated, Text, View, Dimensions, StyleSheet, TouchableOpacity, Easing, SafeAreaViewBase, SafeAreaView } from 'react-native';
// const { width, height } = Dimensions.get('screen');
// import RenderItem from './src/component/RenderItem'
// import faker from 'faker'
// faker.seed(10);
// let i = 0;
// const DATA = [...Array(500).keys()].map((_, i) => {
// i= i+1;
// return {
// key: faker.random.uuid(),
// image: `https://randomuser.me/api/portraits/${faker.helpers.randomize(['women', 'men'])}/${faker.random.number(60)}.jpg`,
// name: faker.name.findName(),
// jobTitle: faker.name.jobTitle(),
// email: i,
// };
// });
// const BG_IMG ='https://images.pexels.com/photos/1231265/pexels-photo-1231265.jpeg';
// const SPACING = 20;
// const AVATAR_SIZE = 70;
// const ITEM_SIZE = AVATAR_SIZE + SPACING+3;
// export default () => {
// const scrollY = React.useRef(new Animated.Value(0)).current;
// return (
// <ImageBackground
// source={{uri:BG_IMG}}
// style={{...StyleSheet.absoluteFillObject}}
// blurRadius={10}
// >
// <StatusBar hidden />
// <Animated.FlatList
// data={DATA}
// onScroll={Animated.event(
// [{nativeEvent: {contentOffset: {y: scrollY}}}],
// {useNativeDriver:true},
// )}
// contentContainerStyle={{
// padding:SPACING,
// paddingTop:StatusBar.height,
// }}
// legacyImplementation={false}
// horizontal={false}
// windowSize={150}
// removeClippedSubviews={true}
// initialNumToRender={55}
// updateCellsBatchingPeriod={5}
// numColumns={1}
// onEndReachedThreshold={.4}
// // refreshing={true}
// maxToRenderPerBatch={100}
// keyExtractor={item => item.key}
// renderItem={({item, index}) => (
// <RenderItem item={item} index={index} />
// )}
// />
// </ImageBackground>
// )
// }
import * as React from 'react';
import { StatusBar,View, Text, Dimensions,findNodeHandle, Animated,StyleSheet, Image, TouchableOpacity,} from 'react-native';
const {width, height} = Dimensions.get('screen')
const images = {
man:
'https://images.pexels.com/photos/3147528/pexels-photo-3147528.jpeg?auto=compress&cs=tinysrgb&dpr=2&w=500',
women:
'https://images.pexels.com/photos/2552130/pexels-photo-2552130.jpeg?auto=compress&cs=tinysrgb&dpr=2&w=500',
kids:
'https://images.pexels.com/photos/5080167/pexels-photo-5080167.jpeg?auto=compress&cs=tinysrgb&dpr=2&w=500',
skullcandy:
'https://images.pexels.com/photos/5602879/pexels-photo-5602879.jpeg?auto=compress&cs=tinysrgb&dpr=2&w=500',
help:
'https://images.pexels.com/photos/2552130/pexels-photo-2552130.jpeg?auto=compress&cs=tinysrgb&dpr=2&w=500',
};
const data = Object.keys(images).map((i) => ({
key: i,
title: i,
image: images[i],
ref:React.createRef()
}));
const Tab = React.forwardRef(({item , onItemPress}, ref) =>{
return(
<TouchableOpacity onPress={onItemPress}>
<View ref={ref} style={{ backgroundColor:"black", borderRadius:5, paddingHorizontal:5, paddingVertical:5 }}>
<Text
style={{
color:'#fff',
fontSize:84/data.length,
fontWeight:'800'
}}>{item.title}
</Text>
</View>
</TouchableOpacity>
)
})
const Indicator = ({measures, scrollX}) =>{
const inputRange = data.map((_,i) => i*width);
const indicatorWidth = scrollX.interpolate({
inputRange,
outputRange:measures.map(measure => measure.width)
})
const translateX = scrollX.interpolate({
inputRange,
outputRange:measures.map(measure => measure.x)
})
return(
<Animated.View
style = {{
position:'absolute',
height:4,
width:indicatorWidth,
left:0,
backgroundColor:"#fff",
bottom:-10,
transform:[{
translateX
}]
}}
/>
)
}
const Tabs = ({data, scrollX, onItemPress}) =>{
const [measures, setMeasures] = React.useState([]);
const containerRef = React.useRef();
React.useEffect(() =>{
let m = [];
data.forEach(item =>{
item.ref.current.measureLayout(
containerRef.current,
(x, y, width, height) =>{
m.push({
x, y, width, height
})
if(m.length === data.length){
setMeasures(m);
}
})
})
},[measures])
return (
<View style={{position:'absolute', top:40 , width }}>
<View
ref={containerRef}
style={{justifyContent:"space-evenly", flex:1, flexDirection:'row',}}>
{data.map((item, index) => {
return <Tab key ={item.key} item={item} ref={item.ref} onItemPress={() => onItemPress(index)} />
})}
</View>
{measures.length > 0 && <Indicator measures={measures} scrollX={scrollX} />}
</View>
)
}
export default function App() {
const scrollX = React.useRef(new Animated.Value(0)).current;
const ref = React.useRef();
const onItemPress = React.useCallback(itemIndex => {
ref?.current?.scrollToOffset({
offset:itemIndex * width
})
})
return (
<View style={styles.container}>
<StatusBar hidden />
<Animated.FlatList
ref={ref}
data={data}
keyExtractor={item => item.key}
horizontal
showsHorizontalScrollIndicator={false}
pagingEnabled
onScroll={Animated.event(
[{ nativeEvent: { contentOffset: { x: scrollX } } }],
{useNativeDriver:false}
)}
bounces={false}
renderItem= {({item}) =>{
return(
<View style={{width, height}}>
<Image
source={{uri:item.image}}
style={{flex:1, resizeMode:"cover"}}
/>
<View style={[StyleSheet.absoluteFillObject,{backgroundColor:"rgba(0,0,0,.3)"}]} />
</View>
)
}}
/>
<Tabs
scrollX={scrollX}
data={data}
onItemPress={onItemPress}
/>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});