Here is my model.
I would like this animation :
- When I swipe left, the month March takes the central place, and April replaces March in the right
- When I swipe right, the month January takes central place, and December replaces January in the left
I literally don’t know where to begin, or how to achieve this.
For the code used in the image, here it is :
JavaScript
x
90
90
1
import React from 'react';
2
import {View, StyleSheet, Text, TouchableOpacity} from 'react-native';
3
4
const MonthSlider = () => {
5
return (
6
<View
7
style={{
8
flexDirection: 'row',
9
flex: 0.2,
10
paddingBottom: 100,
11
}}>
12
<View
13
style={{
14
flexDirection: 'column',
15
flex: 0.25,
16
alignItems: 'center',
17
marginTop: 10,
18
}}>
19
<TouchableOpacity
20
style={{alignItems: 'center'}}
21
onPress={() => alert('January clicked')}>
22
<View style={styles.nonActiveCircle} />
23
<Text style={styles.nonActiveMonth}>January</Text>
24
</TouchableOpacity>
25
</View>
26
<View
27
style={{
28
flexDirection: 'column',
29
flex: 0.5,
30
alignItems: 'center',
31
marginTop: 10,
32
}}>
33
<View style={styles.activeCircle} />
34
<Text style={styles.year}>2021</Text>
35
<Text style={styles.activeMonth}>February</Text>
36
</View>
37
<View
38
style={{
39
flexDirection: 'column',
40
flex: 0.25,
41
marginTop: 10,
42
alignItems: 'center',
43
}}>
44
<TouchableOpacity
45
style={{alignItems: 'center'}}
46
onPress={() => alert('March clicked')}>
47
<View style={styles.nonActiveCircle} />
48
<Text style={styles.nonActiveMonth}>March</Text>
49
</TouchableOpacity>
50
</View>
51
</View>
52
);
53
};
54
55
const styles = StyleSheet.create({
56
nonActiveMonth: {
57
fontSize: 20,
58
color: '#8BA8C3',
59
fontWeight: 'bold',
60
},
61
activeMonth: {
62
fontSize: 30,
63
color: 'white',
64
fontWeight: 'bold',
65
},
66
nonActiveCircle: {
67
width: 8,
68
height: 8,
69
borderRadius: 8 / 2,
70
backgroundColor: '#8BA8C3',
71
marginTop: 10,
72
},
73
activeCircle: {
74
width: 25,
75
height: 25,
76
borderRadius: 25 / 2,
77
backgroundColor: 'white',
78
borderWidth: 5,
79
borderColor: '#175287',
80
bottom: 20,
81
marginBottom: -20,
82
},
83
year: {
84
fontSize: 20,
85
color: '#8BA8C3',
86
},
87
});
88
89
export default MonthSlider;
90
Advertisement
Answer
Maybe a good start would be use ‘react-view-slider’ or ‘ScrollView’ and do something like this :
JavaScript
1
104
104
1
import React, { useState } from 'react';
2
import {View, StyleSheet, Text, TouchableOpacity} from 'react-native';
3
import Swiper from 'react-native-swiper';
4
5
const MonthSlider = () => {
6
7
// Months
8
const months = ["January","February","March","April","May","June","July","August","September","October","November","December"];
9
10
// State iMonths
11
const [ iMonth, setIMonth ] = useState(1);
12
13
// Month click
14
const MonthClick = (i) => {
15
alert( months[i] +' clicked')
16
setIMonth(i);
17
};
18
19
// This function renders the view at the given index.
20
const renderView = ({ index, active }) => (
21
months.map( (month,i) =>
22
<View key={i} style={styles.month + ( active == i ) ? styles.active : styles.inactive }>
23
<TouchableOpacity
24
style={styles.bt}
25
onPress={() => MonthClick(i)}
26
>
27
<View style={ active == i ? styles.activeCircle : styles.nonActiveCircle } />
28
<Text style={ active == i ? styles.activeMonth : styles.nonActiveMonth }>{month}</Text>
29
</TouchableOpacity>
30
</View>
31
)
32
);
33
34
return (
35
<Swiper style={styles.monthWrapper} showsButtons={false} horizontal={true} showsPagination={false}>
36
{renderView(0,0)}
37
</Swiper>
38
);
39
};
40
41
const styles = StyleSheet.create({
42
43
/* New styles */
44
monthWrapper:{
45
flex:0.5,
46
display:'flex',
47
flexDirection: 'row',
48
height:'100px',
49
textAlign:'center',
50
},
51
bt:{
52
textAlign:"center"
53
},
54
month:{
55
alignItems: 'center',
56
backgroundColor: '#9DD6EB'
57
},
58
active:{
59
color:'#000',
60
flex: 0.5,
61
opacity:1,
62
fontSize: 30,
63
color: 'white',
64
fontWeight: 'bold',
65
},
66
inactive: {
67
fontSize: 20,
68
color: '#8BA8C3',
69
fontWeight: 'bold',
70
},
71
72
/* Old styles */
73
nonActiveMonth: {
74
fontSize: 20,
75
color: '#8BA8C3',
76
fontWeight: 'bold',
77
},
78
activeMonth: {
79
fontSize: 30,
80
color: 'white',
81
fontWeight: 'bold',
82
},
83
nonActiveCircle: {
84
width: 12,
85
height: 12,
86
borderRadius: '100%',
87
backgroundColor: '#8BA8C3',
88
marginTop: 10,
89
alignSelf:'center'
90
},
91
activeCircle: {
92
width: 40,
93
height: 40,
94
borderRadius: '100%',
95
backgroundColor: 'white',
96
borderWidth: 5,
97
borderColor: '#175287',
98
bottom: 20,
99
marginBottom: -20,
100
},
101
});
102
103
export default MonthSlider;
104