-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathindex.js
139 lines (127 loc) · 3.81 KB
/
index.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
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { View, StyleSheet, PanResponder, ViewPropTypes } from 'react-native';
// Fallback when RN version is < 0.44
const viewPropTypes = ViewPropTypes || View.propTypes;
export default class PinchZoomView extends Component {
static propTypes = {
...viewPropTypes,
scalable: PropTypes.bool,
minScale: PropTypes.number,
maxScale: PropTypes.number
};
static defaultProps = {
scalable: true,
minScale: 0.5,
maxScale: 2
};
constructor(props) {
super(props);
this.state = {
scale: 1,
lastScale: 1,
offsetX: 0,
offsetY: 0,
lastX: 0,
lastY: 0,
lastMovePinch: false
};
this.distant = 150;
}
componentWillMount() {
this.gestureHandlers = PanResponder.create({
onStartShouldSetPanResponder: this._handleStartShouldSetPanResponder,
onMoveShouldSetPanResponder: this._handleMoveShouldSetPanResponder,
onPanResponderGrant: this._handlePanResponderGrant,
onPanResponderMove: this._handlePanResponderMove,
onPanResponderRelease: this._handlePanResponderEnd,
onPanResponderTerminationRequest: evt => true,
onShouldBlockNativeResponder: evt => false
});
}
_handleStartShouldSetPanResponder = (e, gestureState) => {
// don't respond to single touch to avoid shielding click on child components
return false;
};
_handleMoveShouldSetPanResponder = (e, gestureState) => {
return (
this.props.scalable &&
(Math.abs(gestureState.dx) > 2 ||
Math.abs(gestureState.dy) > 2 ||
gestureState.numberActiveTouches === 2)
);
};
_handlePanResponderGrant = (e, gestureState) => {
if (gestureState.numberActiveTouches === 2) {
let dx = Math.abs(
e.nativeEvent.touches[0].pageX - e.nativeEvent.touches[1].pageX
);
let dy = Math.abs(
e.nativeEvent.touches[0].pageY - e.nativeEvent.touches[1].pageY
);
let distant = Math.sqrt(dx * dx + dy * dy);
this.distant = distant;
}
};
_handlePanResponderEnd = (e, gestureState) => {
this.setState({
lastX: this.state.offsetX,
lastY: this.state.offsetY,
lastScale: this.state.scale
});
};
_handlePanResponderMove = (e, gestureState) => {
// zoom
if (gestureState.numberActiveTouches === 2) {
let dx = Math.abs(
e.nativeEvent.touches[0].pageX - e.nativeEvent.touches[1].pageX
);
let dy = Math.abs(
e.nativeEvent.touches[0].pageY - e.nativeEvent.touches[1].pageY
);
let distant = Math.sqrt(dx * dx + dy * dy);
let scale = (distant / this.distant) * this.state.lastScale;
//check scale min to max hello
if (scale < this.props.maxScale && scale > this.props.minScale) {
this.setState({ scale, lastMovePinch: true });
}
}
// translate
else if (gestureState.numberActiveTouches === 1) {
if (this.state.lastMovePinch) {
gestureState.dx = 0;
gestureState.dy = 0;
}
let offsetX = this.state.lastX + gestureState.dx / this.state.scale;
let offsetY = this.state.lastY + gestureState.dy / this.state.scale;
// if ( offsetX < 0 || offsetY < 0 )
this.setState({ offsetX, offsetY, lastMovePinch: false });
}
};
render() {
return (
<View
{...this.gestureHandlers.panHandlers}
style={[
styles.container,
this.props.style,
{
transform: [
{ scaleX: this.state.scale },
{ scaleY: this.state.scale },
{ translateX: this.state.offsetX },
{ translateY: this.state.offsetY }
]
}
]}
>
{this.props.children}
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1
}
});