1 | import React, { Component, } from 'react';
|
2 | import { View, PanResponder, Animated, Platform, } from 'react-native';
|
3 | export class PropsGaea {
|
4 | constructor() {
|
5 | this.gaeaName = '图片手势操作';
|
6 | this.gaeaIcon = 'square-o';
|
7 | this.gaeaUniqueKey = 'nt-image-zoom';
|
8 | }
|
9 | }
|
10 | export class Props extends PropsGaea {
|
11 | constructor() {
|
12 | super(...arguments);
|
13 | this.onClick = () => {
|
14 | };
|
15 | this.onLongPress = () => {
|
16 | };
|
17 | this.panToMove = true;
|
18 | this.pinchToZoom = true;
|
19 | this.cropWidth = 100;
|
20 | this.cropHeight = 100;
|
21 | this.imageWidth = 100;
|
22 | this.imageHeight = 100;
|
23 | this.source = '';
|
24 | this.longPressTime = 800;
|
25 | this.leaveStayTime = 100;
|
26 | this.leaveDistance = 10;
|
27 | this.maxOverflow = 100;
|
28 | this.horizontalOuterRangeOffset = () => {
|
29 | };
|
30 | this.responderRelease = () => {
|
31 | };
|
32 | this.onDoubleClick = () => {
|
33 | };
|
34 | }
|
35 | }
|
36 | export class State {
|
37 | constructor() {
|
38 | this.centerX = 0.5;
|
39 | this.centerY = 0.5;
|
40 | }
|
41 | }
|
42 |
|
43 |
|
44 |
|
45 | const styles = {
|
46 | container: {
|
47 | justifyContent: 'center',
|
48 | alignItems: 'center',
|
49 | overflow: 'hidden',
|
50 |
|
51 | backgroundColor: 'transparent',
|
52 | },
|
53 | };
|
54 | const isMobile = () => {
|
55 | if (Platform.OS === 'web') {
|
56 | return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i
|
57 | .test(navigator.userAgent);
|
58 | }
|
59 | else {
|
60 | return true;
|
61 | }
|
62 | };
|
63 |
|
64 | export default class ImageViewer extends Component {
|
65 | constructor() {
|
66 | super(...arguments);
|
67 | this.state = new State();
|
68 |
|
69 | this.lastPositionX = null;
|
70 | this.positionX = 0;
|
71 | this.animatedPositionX = new Animated.Value(0);
|
72 |
|
73 | this.lastPositionY = null;
|
74 | this.positionY = 0;
|
75 | this.animatedPositionY = new Animated.Value(0);
|
76 |
|
77 | this.scale = 1;
|
78 | this.animatedScale = new Animated.Value(1);
|
79 | this.zoomLastDistance = null;
|
80 | this.zoomCurrentDistance = 0;
|
81 |
|
82 | this.horizontalWholeOuterCounter = 0;
|
83 |
|
84 | this.horizontalWholeCounter = 0;
|
85 | this.verticalWholeCounter = 0;
|
86 |
|
87 | this.centerDiffX = 0;
|
88 | this.centerDiffY = 0;
|
89 |
|
90 | this.lastClickTime = 0;
|
91 |
|
92 | this.doubleClickX = 0;
|
93 | this.doubleClickY = 0;
|
94 |
|
95 | this.isDoubleClickScale = false;
|
96 | }
|
97 | componentWillMount() {
|
98 | const setResponder = isMobile();
|
99 | this.imagePanResponder = PanResponder.create({
|
100 |
|
101 | onStartShouldSetPanResponder: () => setResponder,
|
102 | onStartShouldSetPanResponderCapture: (event, gestureState) => {
|
103 |
|
104 | return event && setResponder && gestureState.dx !== 0 && gestureState.dy !== 0;
|
105 | },
|
106 | onMoveShouldSetPanResponder: () => setResponder,
|
107 | onMoveShouldSetPanResponderCapture: (event, gestureState) => {
|
108 |
|
109 | return event && setResponder && gestureState.dx !== 0 && gestureState.dy !== 0;
|
110 | },
|
111 | onPanResponderTerminationRequest: () => false,
|
112 | onPanResponderGrant: (evt) => {
|
113 |
|
114 | this.lastPositionX = null;
|
115 | this.lastPositionY = null;
|
116 | this.zoomLastDistance = null;
|
117 | this.horizontalWholeCounter = 0;
|
118 | this.verticalWholeCounter = 0;
|
119 | this.lastTouchStartTime = new Date().getTime();
|
120 | this.isDoubleClickScale = false;
|
121 | if (evt.nativeEvent.changedTouches.length > 1) {
|
122 | this.centerDiffX = (evt.nativeEvent.changedTouches[0].pageX
|
123 | + evt.nativeEvent.changedTouches[1].pageX) / 2 - this.props.cropWidth / 2;
|
124 | this.centerDiffY = (evt.nativeEvent.changedTouches[0].pageY
|
125 | + evt.nativeEvent.changedTouches[1].pageY) / 2 - this.props.cropHeight / 2;
|
126 | }
|
127 |
|
128 | if (this.longPressTimeout) {
|
129 | clearTimeout(this.longPressTimeout);
|
130 | }
|
131 | this.longPressTimeout = setTimeout(() => {
|
132 | this.props.onLongPress();
|
133 | }, this.props.longPressTime);
|
134 | if (evt.nativeEvent.changedTouches.length <= 1) {
|
135 |
|
136 | if (new Date().getTime() - this.lastClickTime < 150) {
|
137 |
|
138 | this.lastClickTime = 0;
|
139 | this.props.onDoubleClick();
|
140 |
|
141 | clearTimeout(this.longPressTimeout);
|
142 |
|
143 | this.doubleClickX = evt.nativeEvent.changedTouches[0].pageX;
|
144 | this.doubleClickY = evt.nativeEvent.changedTouches[0].pageY;
|
145 |
|
146 | this.isDoubleClickScale = true;
|
147 | if (this.scale > 1 || this.scale < 1) {
|
148 |
|
149 | this.scale = 1;
|
150 | this.positionX = 0;
|
151 | this.positionY = 0;
|
152 | }
|
153 | else {
|
154 |
|
155 |
|
156 |
|
157 | const beforeScale = this.scale;
|
158 |
|
159 | this.scale = 2;
|
160 |
|
161 | const diffScale = this.scale - beforeScale;
|
162 |
|
163 |
|
164 | this.positionX = (this.props.cropWidth / 2
|
165 | - this.doubleClickX) * diffScale / this.scale;
|
166 | this.positionY = (this.props.cropHeight / 2
|
167 | - this.doubleClickY) * diffScale / this.scale;
|
168 | }
|
169 | Animated.parallel([
|
170 | Animated.timing(this.animatedScale, {
|
171 | toValue: this.scale,
|
172 | duration: 100,
|
173 | }),
|
174 | Animated.timing(this.animatedPositionX, {
|
175 | toValue: this.positionX,
|
176 | duration: 100,
|
177 | }),
|
178 | Animated.timing(this.animatedPositionY, {
|
179 | toValue: this.positionY,
|
180 | duration: 100,
|
181 | }),
|
182 | ]).start();
|
183 | }
|
184 | else {
|
185 | this.lastClickTime = new Date().getTime();
|
186 | }
|
187 | }
|
188 | },
|
189 | onPanResponderMove: (evt, gestureState) => {
|
190 | if (evt.nativeEvent.changedTouches.length <= 1) {
|
191 |
|
192 | let diffX = gestureState.dx - this.lastPositionX;
|
193 | if (this.lastPositionX === null) {
|
194 | diffX = 0;
|
195 | }
|
196 |
|
197 | let diffY = gestureState.dy - this.lastPositionY;
|
198 | if (this.lastPositionY === null) {
|
199 | diffY = 0;
|
200 | }
|
201 |
|
202 | this.lastPositionX = gestureState.dx;
|
203 | this.lastPositionY = gestureState.dy;
|
204 | this.horizontalWholeCounter += diffX;
|
205 | this.verticalWholeCounter += diffY;
|
206 | if (Math.abs(this.horizontalWholeCounter) > 5 ||
|
207 | Math.abs(this.verticalWholeCounter) > 5) {
|
208 |
|
209 | clearTimeout(this.longPressTimeout);
|
210 | }
|
211 | if (this.props.panToMove) {
|
212 |
|
213 |
|
214 | if (this.props.imageWidth * this.scale > this.props.cropWidth) {
|
215 |
|
216 | if (this.horizontalWholeOuterCounter > 0) {
|
217 | if (diffX < 0) {
|
218 | if (this.horizontalWholeOuterCounter > Math.abs(diffX)) {
|
219 |
|
220 | this.horizontalWholeOuterCounter += diffX;
|
221 | diffX = 0;
|
222 | }
|
223 | else {
|
224 |
|
225 | diffX += this.horizontalWholeOuterCounter;
|
226 | this.horizontalWholeOuterCounter = 0;
|
227 | this.props.horizontalOuterRangeOffset(0);
|
228 | }
|
229 | }
|
230 | else {
|
231 | this.horizontalWholeOuterCounter += diffX;
|
232 | }
|
233 | }
|
234 | else if (this.horizontalWholeOuterCounter < 0) {
|
235 | if (diffX > 0) {
|
236 | if (Math.abs(this.horizontalWholeOuterCounter) > diffX) {
|
237 |
|
238 | this.horizontalWholeOuterCounter += diffX;
|
239 | diffX = 0;
|
240 | }
|
241 | else {
|
242 |
|
243 | diffX += this.horizontalWholeOuterCounter;
|
244 | this.horizontalWholeOuterCounter = 0;
|
245 | this.props.horizontalOuterRangeOffset(0);
|
246 | }
|
247 | }
|
248 | else {
|
249 | this.horizontalWholeOuterCounter += diffX;
|
250 | }
|
251 | }
|
252 | else {
|
253 |
|
254 | }
|
255 |
|
256 | this.positionX += diffX / this.scale;
|
257 |
|
258 |
|
259 | const horizontalMax = (this.props.imageWidth *
|
260 | this.scale - this.props.cropWidth) / 2 / this.scale;
|
261 | if (this.positionX < -horizontalMax) {
|
262 | this.positionX = -horizontalMax;
|
263 |
|
264 | this.horizontalWholeOuterCounter += -1 / 1e10;
|
265 | }
|
266 | else if (this.positionX > horizontalMax) {
|
267 | this.positionX = horizontalMax;
|
268 |
|
269 | this.horizontalWholeOuterCounter += 1 / 1e10;
|
270 | }
|
271 | this.animatedPositionX.setValue(this.positionX);
|
272 | }
|
273 | else {
|
274 |
|
275 | this.horizontalWholeOuterCounter += diffX;
|
276 | }
|
277 |
|
278 | if (this.horizontalWholeOuterCounter > this.props.maxOverflow) {
|
279 | this.horizontalWholeOuterCounter = this.props.maxOverflow;
|
280 | }
|
281 | else if (this.horizontalWholeOuterCounter < -this.props.maxOverflow) {
|
282 | this.horizontalWholeOuterCounter = -this.props.maxOverflow;
|
283 | }
|
284 | if (this.horizontalWholeOuterCounter !== 0) {
|
285 |
|
286 | this.props.horizontalOuterRangeOffset(this.horizontalWholeOuterCounter);
|
287 | }
|
288 | if (this.props.imageHeight * this.scale > this.props.cropHeight) {
|
289 |
|
290 | this.positionY += diffY / this.scale;
|
291 | this.animatedPositionY.setValue(this.positionY);
|
292 | }
|
293 | }
|
294 | }
|
295 | else {
|
296 |
|
297 |
|
298 | if (this.longPressTimeout) {
|
299 | clearTimeout(this.longPressTimeout);
|
300 | }
|
301 | if (this.props.pinchToZoom) {
|
302 |
|
303 | let minX;
|
304 | let maxX;
|
305 | if (evt.nativeEvent.changedTouches[0].locationX >
|
306 | evt.nativeEvent.changedTouches[1].locationX) {
|
307 | minX = evt.nativeEvent.changedTouches[1].pageX;
|
308 | maxX = evt.nativeEvent.changedTouches[0].pageX;
|
309 | }
|
310 | else {
|
311 | minX = evt.nativeEvent.changedTouches[0].pageX;
|
312 | maxX = evt.nativeEvent.changedTouches[1].pageX;
|
313 | }
|
314 | let minY;
|
315 | let maxY;
|
316 | if (evt.nativeEvent.changedTouches[0].locationY >
|
317 | evt.nativeEvent.changedTouches[1].locationY) {
|
318 | minY = evt.nativeEvent.changedTouches[1].pageY;
|
319 | maxY = evt.nativeEvent.changedTouches[0].pageY;
|
320 | }
|
321 | else {
|
322 | minY = evt.nativeEvent.changedTouches[0].pageY;
|
323 | maxY = evt.nativeEvent.changedTouches[1].pageY;
|
324 | }
|
325 | const widthDistance = maxX - minX;
|
326 | const heightDistance = maxY - minY;
|
327 | const diagonalDistance = Math.sqrt(widthDistance * widthDistance
|
328 | + heightDistance * heightDistance);
|
329 | this.zoomCurrentDistance = Number(diagonalDistance.toFixed(1));
|
330 | if (this.zoomLastDistance !== null) {
|
331 | const distanceDiff = (this.zoomCurrentDistance - this.zoomLastDistance) / 200;
|
332 | let zoom = this.scale + distanceDiff;
|
333 | if (zoom < 0.6) {
|
334 | zoom = 0.6;
|
335 | }
|
336 | if (zoom > 10) {
|
337 | zoom = 10;
|
338 | }
|
339 |
|
340 | const beforeScale = this.scale;
|
341 |
|
342 | this.scale = zoom;
|
343 | this.animatedScale.setValue(this.scale);
|
344 |
|
345 |
|
346 | const diffScale = this.scale - beforeScale;
|
347 |
|
348 |
|
349 | this.positionX -= this.centerDiffX * diffScale / this.scale;
|
350 | this.positionY -= this.centerDiffY * diffScale / this.scale;
|
351 | this.animatedPositionX.setValue(this.positionX);
|
352 | this.animatedPositionY.setValue(this.positionY);
|
353 | }
|
354 | this.zoomLastDistance = this.zoomCurrentDistance;
|
355 | }
|
356 | }
|
357 | },
|
358 | onPanResponderRelease: (evt, gestureState) => {
|
359 |
|
360 | if (this.isDoubleClickScale) {
|
361 | return;
|
362 | }
|
363 | if (this.scale < 1) {
|
364 |
|
365 | this.scale = 1;
|
366 | Animated.timing(this.animatedScale, {
|
367 | toValue: this.scale,
|
368 | duration: 100,
|
369 | }).start();
|
370 | }
|
371 | if (this.props.imageWidth * this.scale <= this.props.cropWidth) {
|
372 |
|
373 | this.positionX = 0;
|
374 | Animated.timing(this.animatedPositionX, {
|
375 | toValue: this.positionX,
|
376 | duration: 100,
|
377 | }).start();
|
378 | }
|
379 | if (this.props.imageHeight * this.scale <= this.props.cropHeight) {
|
380 |
|
381 | this.positionY = 0;
|
382 | Animated.timing(this.animatedPositionY, {
|
383 | toValue: this.positionY,
|
384 | duration: 100,
|
385 | }).start();
|
386 | }
|
387 |
|
388 |
|
389 | if (this.props.imageHeight * this.scale > this.props.cropHeight) {
|
390 |
|
391 | const verticalMax = (this.props.imageHeight *
|
392 | this.scale - this.props.cropHeight) / 2 / this.scale;
|
393 | if (this.positionY < -verticalMax) {
|
394 | this.positionY = -verticalMax;
|
395 | }
|
396 | else if (this.positionY > verticalMax) {
|
397 | this.positionY = verticalMax;
|
398 | }
|
399 | Animated.timing(this.animatedPositionY, {
|
400 | toValue: this.positionY,
|
401 | duration: 100,
|
402 | }).start();
|
403 | }
|
404 |
|
405 | if (this.scale === 1) {
|
406 | this.positionX = 0;
|
407 | this.positionY = 0;
|
408 | Animated.timing(this.animatedPositionX, {
|
409 | toValue: this.positionX,
|
410 | duration: 100,
|
411 | }).start();
|
412 | Animated.timing(this.animatedPositionY, {
|
413 | toValue: this.positionY,
|
414 | duration: 100,
|
415 | }).start();
|
416 | }
|
417 |
|
418 | this.horizontalWholeOuterCounter = 0;
|
419 |
|
420 | if (this.longPressTimeout) {
|
421 | clearTimeout(this.longPressTimeout);
|
422 | }
|
423 |
|
424 | const stayTime = new Date().getTime() - this.lastTouchStartTime;
|
425 | const moveDistance = Math.sqrt(gestureState.dx
|
426 | * gestureState.dx + gestureState.dy * gestureState.dy);
|
427 | if (evt.nativeEvent.changedTouches.length === 1 && stayTime < this.props.leaveStayTime && moveDistance < this.props.leaveDistance) {
|
428 | this.props.onClick();
|
429 | }
|
430 | else {
|
431 | this.props.responderRelease(gestureState.vx, this.scale);
|
432 | }
|
433 | },
|
434 | onPanResponderTerminate: () => {
|
435 | },
|
436 | });
|
437 | }
|
438 | |
439 |
|
440 |
|
441 | reset() {
|
442 | this.scale = 1;
|
443 | this.animatedScale.setValue(this.scale);
|
444 | this.positionX = 0;
|
445 | this.animatedPositionX.setValue(this.positionX);
|
446 | this.positionY = 0;
|
447 | this.animatedPositionY.setValue(this.positionY);
|
448 | }
|
449 | render() {
|
450 | const animateConf = {
|
451 | transform: [{
|
452 | scale: this.animatedScale,
|
453 | }, {
|
454 | translateX: this.animatedPositionX,
|
455 | }, {
|
456 | translateY: this.animatedPositionY,
|
457 | }],
|
458 | };
|
459 | return (React.createElement(View, Object.assign({ style: [styles.container, { width: this.props.cropWidth, height: this.props.cropHeight, backgroundColor: '#000' }] }, this.imagePanResponder.panHandlers),
|
460 | React.createElement(Animated.View, { style: animateConf },
|
461 | React.createElement(View, { onLayout: this.handleLayout, style: { width: this.props.imageWidth, height: this.props.imageHeight } }, this.props.children))));
|
462 | }
|
463 | }
|
464 | ImageViewer.defaultProps = new Props();
|
465 |
|
\ | No newline at end of file |