forked from jeffrey1995/MyBlog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMySlider.js
214 lines (185 loc) · 6.12 KB
/
MySlider.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
import React from 'react';
import {
Dimensions,
View,
Image,
StyleSheet,
PanResponder,
} from 'react-native';
import PropTypes from 'prop-types';
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
},
});
const ScreenWidth = Dimensions.get('window').width;
export default class MySlider extends React.Component {
static propTypes = {
height: PropTypes.number, // 控件高度
width: PropTypes.number, // 控件宽度
maximumTrackTintColor: PropTypes.string, // 进度条背景颜色
minimumTrackTintColor: PropTypes.string, // 进度条进度部分颜色
onChange: PropTypes.func, // 进度值发生改变时的回调
onAfterChange: PropTypes.func, // 拖动结束时的回调
defaultValue: PropTypes.number, // 默认的进度值
min: PropTypes.number.isRequired, // 进度范围最小值
max: PropTypes.number.isRequired, // 进度范围最大值
step: PropTypes.number.isRequired, // 步长(进度变化的最小单位)
disabled: PropTypes.bool, // 是否可以拖动
thumbSize: PropTypes.number, // 滑块的尺寸
thumbImage: PropTypes.number, // 滑块的图片
processHeight: PropTypes.number, // 进度条高度
};
static defaultProps = {
height: 60,
width: ScreenWidth,
onChange: () => {},
onAfterChange: () => {},
defaultValue: 0,
disabled: false,
thumbSize: 30,
thumbImage: null,
maximumTrackTintColor: '#dcdbdb',
minimumTrackTintColor: '#577BFF',
processHeight: 7,
};
state = {
process: 0,
processWidth: 0,
};
constructor(props) {
super(props);
this._onPanResponderGrant = this._onPanResponderGrant.bind(this);
this._onPanResponderEnd = this._onPanResponderEnd.bind(this);
this._onPanResponderMove = this._onPanResponderMove.bind(this);
}
componentWillMount() {
this.watcher = PanResponder.create({
// 建立监视器
onStartShouldSetPanResponder: () => true,
onPanResponderGrant: this._onPanResponderGrant, // 按下
onPanResponderMove: this._onPanResponderMove, // 移动
onPanResponderEnd: this._onPanResponderEnd, // 结束
});
const {
defaultValue, min, max, thumbSize,
} = this.props;
const process = defaultValue / (max - min);
this.setState({
process,
processWidth: ScreenWidth - thumbSize,
});
}
_onPanResponderGrant(e, gestureState) {
const { thumbSize } = this.props;
const { processWidth } = this.state;
const process = (gestureState.x0 - thumbSize / 2) / processWidth;
this._changeProcess(process);
}
_onPanResponderMove(e, gestureState) {
const { thumbSize } = this.props;
const { processWidth } = this.state;
const process = (gestureState.x0 - thumbSize / 2 + gestureState.dx) / processWidth;
this._changeProcess(process);
}
_onPanResponderEnd(e, gestureState) {
const { onAfterChange } = this.props;
if (onAfterChange) {
onAfterChange(gestureState.x0);
}
}
_changeProcess(changeProcess) {
// 判断滑动开关
const { disabled } = this.props;
if (disabled) return;
const {
min, max, step, onChange,
} = this.props;
const { process } = this.state;
if (changeProcess >= 0 && changeProcess <= 1) {
onChange(changeProcess);
// 按步长比例变化刻度
const v = changeProcess * (max - min);
const newValue = Math.round(v / step) * step;
const newProcess = newValue / (max - min);
if (process !== newProcess) {
this.setState({
process: newProcess,
});
}
}
}
_getThumbView() {
const { thumbImage, thumbSize } = this.props;
const { process, processWidth } = this.state;
if (thumbImage) {
return (
<Image
style={{
width: thumbSize,
height: thumbSize,
position: 'absolute',
left: process * processWidth,
}}
source={thumbImage}
/>
);
}
return (
<View
style={{
width: thumbSize,
height: thumbSize,
position: 'absolute',
left: process * processWidth,
borderRadius: thumbSize / 2,
backgroundColor: '#808080',
}}
/>
);
}
render() {
const {
height,
width,
maximumTrackTintColor,
minimumTrackTintColor,
thumbSize,
processHeight,
} = this.props;
const { process, processWidth } = this.state;
return (
<View
style={[
styles.container,
{
height,
width,
},
]}
{...this.watcher.panHandlers}
>
<View
style={{
backgroundColor: minimumTrackTintColor,
width: process * processWidth,
height: processHeight,
marginLeft: thumbSize / 2,
}}
/>
<View
style={{
backgroundColor: maximumTrackTintColor,
flex: 1,
height: processHeight,
marginRight: thumbSize / 2,
}}
/>
{this._getThumbView()}
</View>
);
}
}