-
Notifications
You must be signed in to change notification settings - Fork 25
/
path.js
237 lines (221 loc) · 6.21 KB
/
path.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/**
* Project - RNProjectPlayground
* Author : ljunb
* Date : 2018/1/23 上午9:48
* Description : Path 动画菜单效果
*/
import React, { Component } from 'react';
import {
View,
TouchableOpacity,
StyleSheet,
Animated,
Text,
} from 'react-native';
import PropTypes, { element } from 'prop-types';
const MenuItemWH = 30;
const SwitchBtnWH = 40;
const styles = StyleSheet.create({
root: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
centerBtn: {
backgroundColor: 'red',
justifyContent: 'center',
alignItems: 'center',
height: SwitchBtnWH,
width: SwitchBtnWH,
borderRadius: SwitchBtnWH / 2,
},
item: {
height: MenuItemWH,
width: MenuItemWH,
borderRadius: MenuItemWH / 2,
backgroundColor: 'red',
position: 'absolute',
},
itemText: {
backgroundColor: 'transparent',
color: '#fff',
},
menuBtn: {
height: MenuItemWH,
width: MenuItemWH,
borderRadius: MenuItemWH / 2,
justifyContent: 'center',
alignItems: 'center',
},
});
export default class PathMenuAnimation extends Component {
static propTypes = {
icons: PropTypes.array,
radius: PropTypes.number,
itemWidth: PropTypes.number,
switchButtonWidth: PropTypes.number,
animationScale: PropTypes.number,
onSelect: PropTypes.func,
};
static defaultProps = {
icons: [0, 1, 2, 3, 4],
radius: 120,
itemWidth: 30,
switchButtonWidth: 40,
animationScale: 1.5,
};
animatedValue = new Animated.Value(0);
clickedValue = new Animated.Value(0);
isFold = true;
state = {
origin: {
x: 0,
y: 0,
width: 0,
height: 0,
},
clickMenu: null,
};
handleShowOrHideMenu = () => {
this.actionBtn && this.actionBtn.measure((ox, oy, width, height, px, py) => {
this.setState({ origin: { width, height, x: px, y: py } }, () => {
if (this.isFold) {
this.spreadMenus();
} else {
this.foldMenus();
}
});
});
};
/**
* 展开菜单
*/
spreadMenus = () => {
Animated.spring(this.animatedValue, {
toValue: 1,
duration: 200,
friction: 5,
}).start(() => this.isFold = false);
};
/**
* 折叠菜单
*/
foldMenus = () => {
Animated.sequence([
Animated.timing(this.animatedValue, {
toValue: 1.3,
duration: 150,
}),
Animated.timing(this.animatedValue, {
toValue: 0,
duration: 300,
}),
]).start(() => this.isFold = true);
};
hanldePressItem = index => {
this.setState({ clickMenu: index }, () => {
Animated.parallel([
Animated.timing(this.clickedValue, {
toValue: 1,
duration: 450,
}),
Animated.sequence([
Animated.timing(this.animatedValue, {
toValue: 1.3,
duration: 150,
}),
Animated.timing(this.animatedValue, {
toValue: 0,
duration: 300,
}),
])
]).start(() => {
this.props.onSelect && this.props.onSelect(index);
this.resetToDefaultConfig();
});
});
};
resetToDefaultConfig = () => {
this.isFold = true;
this.setState({ clickMenu: null }, () => this.clickedValue.setValue(0));
};
get buildAnimatedValues() {
const { icons, itemWidth, radius } = this.props;
// 每个按钮之间的间距角度
const marginAngle = Math.PI / 180 * itemWidth;
const restAngle = (Math.PI / 180 * 180 - (icons.length - 1) * itemWidth * Math.PI / 180) / 2;
return icons.map((icon, index) => {
const top = -radius * Math.sin(restAngle + index * marginAngle);
const left = radius * Math.cos(restAngle + index * marginAngle);
return { top, left };
});
}
render() {
const { itemWidth, switchButtonWidth, animationScale } = this.props;
const { origin } = this.state;
const rotate = this.animatedValue.interpolate({
inputRange: [0, 1],
outputRange: ['0deg', '-90deg'],
});
const originCenter = {
x: origin.x + origin.width / 2,
y: origin.y + origin.height / 2,
};
return (
<View style={styles.root}>
{this.props.icons.map((icon, index) => {
const animatedItem = this.buildAnimatedValues[index];
const isClicked = this.state.clickMenu === index;
let top = this.animatedValue.interpolate({
inputRange: [0, 1],
outputRange: [origin.y + (switchButtonWidth - itemWidth) / 2, originCenter.y + animatedItem.top],
});
let left = this.animatedValue.interpolate({
inputRange: [0, 1],
outputRange: [origin.x + (switchButtonWidth - itemWidth) / 2, originCenter.x - animatedItem.left - itemWidth / 2],
});
let rotate = this.animatedValue.interpolate({
inputRange: [0, 1],
outputRange: ['0deg', '3600deg'],
});
let scale = this.animatedValue.interpolate({
inputRange: [0, 0.2, 1],
outputRange: [0, 0.5, 1],
extrapolateRight: 'clamp',
});
if (isClicked) {
top = originCenter.y + animatedItem.top;
left = originCenter.x - animatedItem.left - itemWidth / 2;
rotate = '0deg';
scale = this.clickedValue.interpolate({
inputRange: [0, 1],
outputRange: [1, animationScale],
});
}
return (
<Animated.View
key={`Item_${index}`}
style={[styles.item, { top, left, transform: [{ rotate }, { scale }] }]}
>
<TouchableOpacity
activeOpacity={0.75}
onPress={() => this.hanldePressItem(index)}
style={styles.menuBtn}
>
<Text style={styles.itemText}>{index}</Text>
</TouchableOpacity>
</Animated.View>
);
})}
<TouchableOpacity
ref={r => this.actionBtn = r}
activeOpacity={1}
style={styles.centerBtn}
onPress={this.handleShowOrHideMenu}
>
<Animated.Text style={[{ color: '#fff' }, { transform: [{ rotate }] }]}>+</Animated.Text>
</TouchableOpacity>
</View>
);
}
}