forked from greatbsky/react-native-pull
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pullable.js
253 lines (228 loc) · 9.69 KB
/
Pullable.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
'use strict';
import React, { Component } from 'react';
import {
View,
Text,
RefreshControl,
PanResponder,
Animated,
Easing,
Dimensions,
ActivityIndicator
} from 'react-native';
import i18n from './i18n';
import styles from './style/index.js';
const pullOkMargin = 100; //下拉到ok状态时topindicator距离顶部的距离
const defaultDuration = 300;
const defaultTopIndicatorHeight = 60; //顶部刷新指示器的高度
const defaultFlag = { pulling: false, pullok: false, pullrelease: false };
const flagPulling = { pulling: true, pullok: false, pullrelease: false };
const flagPullok = { pulling: false, pullok: true, pullrelease: false };
const flagPullrelease = { pulling: false, pullok: false, pullrelease: true };
const isDownGesture = (x, y) => {
return y > 0 && (y > Math.abs(x));
};
const isUpGesture = (x, y) => {
return y < 0 && (Math.abs(x) < Math.abs(y));
};
const isVerticalGesture = (x, y) => {
return (Math.abs(x) < Math.abs(y));
};
export default class extends Component {
constructor(props) {
super(props);
this.pullable = this.props.refreshControl == null;
this.defaultScrollEnabled = false; //!(this.props.onPulling || this.props.onPullOk || this.props.onPullRelease); //定义onPull***属性时scrollEnabled为false
this.topIndicatorHeight = this.props.topIndicatorHeight ? this.props.topIndicatorHeight : defaultTopIndicatorHeight;
this.defaultXY = { x: 0, y: this.topIndicatorHeight * -1 };
this.pullOkMargin = this.props.pullOkMargin ? this.props.pullOkMargin : pullOkMargin;
this.duration = this.props.duration ? this.props.duration : defaultDuration;
this.state = Object.assign({}, props, {
pullPan: new Animated.ValueXY(this.defaultXY),
scrollEnabled: this.defaultScrollEnabled,
flag: defaultFlag,
height: 0
});
this.gesturePosition = { x: 0, y: 0 };
this.onScroll = this.onScroll.bind(this);
this.onLayout = this.onLayout.bind(this);
this.isPullState = this.isPullState.bind(this);
this.resetDefaultXYHandler = this.resetDefaultXYHandler.bind(this);
this.resolveHandler = this.resolveHandler.bind(this);
this.setFlag = this.setFlag.bind(this);
this.renderTopIndicator = this.renderTopIndicator.bind(this);
this.defaultTopIndicatorRender = this.defaultTopIndicatorRender.bind(this);
this.panResponder = PanResponder.create({
onStartShouldSetPanResponder: this.onShouldSetPanResponder.bind(this),
onMoveShouldSetPanResponder: this.onShouldSetPanResponder.bind(this),
onPanResponderGrant: () => { },
onPanResponderMove: this.onPanResponderMove.bind(this),
onPanResponderRelease: this.onPanResponderRelease.bind(this),
onPanResponderTerminate: this.onPanResponderRelease.bind(this),
});
this.setFlag(defaultFlag);
}
onShouldSetPanResponder(e, gesture) {
if (!this.pullable || !isVerticalGesture(gesture.dx, gesture.dy)) { // 不使用pullable,或非向上 或向下手势不响应
return false;
}
if (!this.state.scrollEnabled) {
this.lastY = this.state.pullPan.y._value;
return gesture.dy >= 0;
} else {
return false;
}
}
onPanResponderMove(e, gesture) {
this.gesturePosition = {
x: this.defaultXY.x,
y: gesture.dy
};
if (isUpGesture(gesture.dx, gesture.dy)) { //向上滑动
if (this.isPullState()) {
this.resetDefaultXYHandler();
} else if (this.props.onPushing) {
this.props.onPushing(this.gesturePosition)
}
} else if (isDownGesture(gesture.dx, gesture.dy)) { //下拉
this.state.pullPan.setValue({
x: this.defaultXY.x,
y: this.lastY + gesture.dy / 2
});
if (gesture.dy < this.topIndicatorHeight + this.pullOkMargin) { //正在下拉
if (!this.flag.pulling) {
if (this.props.onPulling) {
this.props.onPulling();
}
}
this.setFlag(flagPulling);
} else { //下拉到位
if (!this.state.pullok) {
if (this.props.onPullOk) {
this.props.onPullOk();
}
}
this.setFlag(flagPullok);
}
}
}
onPanResponderRelease(e, gesture) {
if (this.flag.pulling) { // 没有下拉到位
this.resetDefaultXYHandler(); // 重置状态
}
if (this.flag.pullok) {
if (!this.flag.pullrelease) {
if (this.props.onPullRelease) {
this.props.onPullRelease(this.resolveHandler);
} else {
setTimeout(() => { this.resetDefaultXYHandler() }, 3000);
}
}
this.setFlag(flagPullrelease); // 完成下拉,已松开
Animated.timing(this.state.pullPan, {
toValue: { x: 0, y: 0 },
easing: Easing.linear,
duration: this.duration
}).start();
}
}
onScroll(e) {
if (e.nativeEvent.contentOffset.y <= 0) {
this.setState({
scrollEnabled: this.defaultScrollEnabled,
});
} else if (!this.isPullState()) {
this.setState({
scrollEnabled: true,
});
}
}
isPullState() {
return this.flag.pulling || this.flag.pullok || this.flag.pullrelease;
}
setFlag(flag) {
if (this.flag != flag) {
this.flag = flag;
this.renderTopIndicator();
}
}
/** 数据加载完成后调用此方法进行重置归位
*/
resolveHandler() {
if (this.flag.pullrelease) { //仅触摸松开时才触发
this.resetDefaultXYHandler();
}
}
resetDefaultXYHandler() {
this.flag = defaultFlag;
Animated.timing(this.state.pullPan, {
toValue: this.defaultXY,
easing: Easing.linear,
duration: this.duration
}).start();
}
componentWillUpdate(nextProps, nextState) {
if (nextProps.isPullEnd && this.state.pullrelease) {
this.resetDefaultXYHandler();
}
}
onLayout(e) {
const { width, height } = e.nativeEvent.layout
// bug fix: 如果列表是 tabs 分组的情况,切换 tab 会引发 View 的二次 layout,这时候取到的高度是 0
if (height != 0 && (this.state.width != width || this.state.height != height)) {
this.scrollContainer.setNativeProps({ style: { width, height } });
this.width = width;
this.height = height;
}
}
render() {
let refreshControl = this.props.refreshControl;
return (
<View style={[styles.wrap, this.props.styles]} onLayout={this.onLayout}>
<Animated.View ref={(c) => { this.ani = c; }} style={[this.state.pullPan.getLayout()]}>
{this.renderTopIndicator()}
<View ref={(c) => { this.scrollContainer = c; }} {...this.panResponder.panHandlers} style={{ width: this.state.width, height: this.state.height }}>
{this.getScrollable(refreshControl)}
</View>
</Animated.View>
</View>
);
}
renderTopIndicator() {
let { pulling, pullok, pullrelease } = this.flag;
if (this.props.topIndicatorRender == null) {
return this.defaultTopIndicatorRender(pulling, pullok, pullrelease, this.gesturePosition, this.topIndicatorHeight);
} else {
return this.props.topIndicatorRender(pulling, pullok, pullrelease, this.gesturePosition, this.topIndicatorHeight);
}
}
/**
使用setNativeProps解决卡顿问题
make changes directly to a component without using state/props to trigger a re-render of the entire subtree
*/
defaultTopIndicatorRender(pulling, pullok, pullrelease, gesturePosition) {
setTimeout(() => {
if (pulling) {
this.txtPulling && this.txtPulling.setNativeProps({ style: styles.show });
this.txtPullok && this.txtPullok.setNativeProps({ style: styles.hide });
this.txtPullrelease && this.txtPullrelease.setNativeProps({ style: styles.hide });
} else if (pullok) {
this.txtPulling && this.txtPulling.setNativeProps({ style: styles.hide });
this.txtPullok && this.txtPullok.setNativeProps({ style: styles.show });
this.txtPullrelease && this.txtPullrelease.setNativeProps({ style: styles.hide });
} else if (pullrelease) {
this.txtPulling && this.txtPulling.setNativeProps({ style: styles.hide });
this.txtPullok && this.txtPullok.setNativeProps({ style: styles.hide });
this.txtPullrelease && this.txtPullrelease.setNativeProps({ style: styles.show });
}
}, 1);
return (
<View style={{ flexDirection: 'row', justifyContent: 'center', alignItems: 'center', height: defaultTopIndicatorHeight }}>
<ActivityIndicator size="small" color="gray" />
<Text ref={(c) => { this.txtPulling = c; }} style={styles.hide}>{i18n.pulling}</Text>
<Text ref={(c) => { this.txtPullok = c; }} style={styles.hide}>{i18n.pullok}</Text>
<Text ref={(c) => { this.txtPullrelease = c; }} style={styles.hide}>{i18n.pullrelease}</Text>
</View>
);
}
}