-
Notifications
You must be signed in to change notification settings - Fork 0
/
InteractiveFlatList.ios.js
385 lines (365 loc) · 12.4 KB
/
InteractiveFlatList.ios.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import {
View,
StyleSheet,
Animated,
FlatList,
Dimensions,
StatusBar,
Platform
} from 'react-native'
import {
NativeViewGestureHandler,
PanGestureHandler,
TapGestureHandler,
PinchGestureHandler,
State
} from 'react-native-gesture-handler'
import { getBoundaryOffset, parallelWithoutScaleAnimated, parallelAnimated } from './Utils'
export default class InteractiveFlatList extends Component {
constructor(props) {
super(props)
this.AnimatedFlatList = null
this.scale = 1
this.lastScale = 1
this.focusPointX = 0
this.isSingleReleasePan = true
this.isDoubleReleasePan = true
this.isSingleReleasePinch = true
this.lastTranslationX = null
this.lastTranslationY = null
this.lastPointX = null
this.lastPointY = null
this.singleTapY = null
this.doubleTapX = null
this.doubleTapY = null
this.focusPointX = null
this.focusPointY = null
this.isAnimated = false
this.isSetSingleFocus = false
this.isSetDoubleFocus = false
this.animatedScale = new Animated.Value(1, { useNativeDriver: true })
this.animatedOffsetX = new Animated.Value(0, { useNativeDriver: true })
this.animatedOffsetY = new Animated.Value(0, { useNativeDriver: true })
this.getBoundaryOffset = getBoundaryOffset.bind(this)
this.parallelAnimated = parallelAnimated.bind(this)
this.parallelWithoutScaleAnimated = parallelWithoutScaleAnimated.bind(this)
this.enableTap = false
this.offsetX = 0
this.offsetY = 0
this.previousScale = 1
}
componentWillMount() {
StatusBar.setHidden(true)
}
onFlatList = event => {
if (event.nativeEvent.state === State.END && event.nativeEvent.oldState === State.ACTIVE) {
this.enableTap = false
} else {
this.enableTap = true
}
}
onDoubleTap = event => {
if (event.nativeEvent.state === State.BEGAN) {
this.doubleTapX = (event.nativeEvent.absoluteX - width/2)
this.doubleTapY = (event.nativeEvent.absoluteY - height/2)
}
if (event.nativeEvent.state === State.ACTIVE) {
const dx = this.doubleTapX - (event.nativeEvent.absoluteX - width/2)
const dy = this.doubleTapY - (event.nativeEvent.absoluteY - height/2)
const distance = Math.sqrt(dx * dx + dy * dy)
if (distance < 30) {
this.isAnimated = true
if (this.animatedScale._value === 1 ) {
const done = result => {
if (result.finished) {
this.lastScale = 2
this.isAnimated = false
}
}
this.parallelAnimated({
scale: 2,
offsetX: -this.doubleTapX/2,
offsetY: -this.doubleTapY/2,
done: done
})
} else if (this.animatedScale._value > 1 ) {
const done = result => {
if (result.finished) {
this.lastScale = 1
this.isAnimated = false
}
}
this.parallelAnimated({
scale: 1,
offsetX: 0,
offsetY: 0,
done: done
})
}
}
}
}
onSingleTap = event => {
if (event.nativeEvent.state === State.BEGAN) {
this.singleTapY = event.nativeEvent.absoluteY
}
if (this.animatedScale._value === 1 && event.nativeEvent.state === State.ACTIVE) {
if (this.singleTapY <= height/3) {
this.props.onSingleClickTopArea && this.props.onSingleClickTopArea()
} else if (this.singleTapY > height/3 && this.singleTapY < height*2/3) {
this.props.onSingleClickMiddleArea && this.props.onSingleClickMiddleArea()
} else {
this.props.onSingleClickBottomArea && this.props.onSingleClickBottomArea()
}
}
}
onPan = event => {
if (event.nativeEvent.numberOfTouches === 2) {
// 雙指平移
if (this.isSingleReleasePan || this.isDoubleReleasePan) {
this.isSingleReleasePan = false
this.isDoubleReleasePan = false
this.isSetSingleFocus = false
if (this.isAnimated) {
this.animatedScale.stopAnimation(scaleValue => {
this.animatedOffsetX.stopAnimation(xValue => {
this.animatedOffsetY.stopAnimation(yValue => {
this.scale = scaleValue
this.lastScale = scaleValue
this.isAnimated = false
})
})
})
}
}
if (!this.isAnimated) {
if (!this.isSetDoubleFocus) {
this.isSetDoubleFocus = true
this.focusPointX = (event.nativeEvent.absoluteX - width/2)/this.scale - this.animatedOffsetX._value
this.focusPointY = (event.nativeEvent.absoluteY - height/2)/this.scale - this.animatedOffsetY._value
}
const magnifierCenterX = event.nativeEvent.absoluteX - width/2 // 目前雙手中心
const magnifierCenterY = event.nativeEvent.absoluteY - height/2 // 目前雙手中心
this.offsetX = magnifierCenterX/this.scale-this.focusPointX // 關注點到雙手中心需要的偏移量
this.offsetY = magnifierCenterY/this.scale-this.focusPointY // 關注點到雙手中心需要的偏移量
this.AnimatedFlatList.setNativeProps({style: {transform: [{scale: this.scale},{translateX: this.offsetX},{translateY: this.offsetY}]}})
}
} else {
if (!(!this.isDoubleReleasePan && this.isSingleReleasePan)) {
if (!this.isDoubleReleasePan && !this.isSingleReleasePan && !this.isAnimated) {
this.releaseAnimated()
}
this.isDoubleReleasePan = false
this.isSingleReleasePan = true
this.isSetDoubleFocus = false
}
if (!this.isAnimated) {
if (!this.isSetSingleFocus) {
this.isSetSingleFocus = true
this.lastTranslationX = this.animatedOffsetX._value
this.lastTranslationY = this.animatedOffsetY._value
this.lastPointX = event.nativeEvent.absoluteX
this.lastPointY = event.nativeEvent.absoluteY
}
if (this.scale > 1) {
// 單指平移
const diffX = (event.nativeEvent.absoluteX - this.lastPointX)/3
const diffY = (event.nativeEvent.absoluteY - this.lastPointY)/3
const offsetX = diffX + this.lastTranslationX
const offsetY = diffY + this.lastTranslationY
const offsetBoundaryX = (this.scale*width/2-width/4)/this.scale
const offsetBoundaryY = (this.scale*height/2-height/4)/this.scale
this.offsetX = Math.abs(offsetX) >= offsetBoundaryX ? offsetBoundaryX*Math.sign(offsetX) + ((offsetX - offsetBoundaryX*Math.sign(offsetX))/(this.scale*5)) : offsetX
this.offsetY = Math.abs(offsetY) >= offsetBoundaryY ? offsetBoundaryY*Math.sign(offsetY) + ((offsetY - offsetBoundaryY*Math.sign(offsetY))/(this.scale*1.5)) : offsetY
this.AnimatedFlatList.setNativeProps({style: {transform: [{scale: this.scale},{translateX: this.offsetX},{translateY: this.offsetY}]}})
}
}
}
}
onPinchStart = event => {
this.props.onPinchStart && this.props.onPinchStart()
this.AnimatedFlatList.setNativeProps({scrollEnabled: false})
}
onPinch = event => {
if (!this.isAnimated) {
if (event.nativeEvent.numberOfTouches === 2) {
let scale = event.nativeEvent.scale*this.lastScale
if (scale > 3) {
scale = 3
} else if (scale < 0.5) {
scale = 0.5
}
this.scale = scale
}
}
}
onPinchEnd = event => {
this.AnimatedFlatList.setNativeProps({scrollEnabled: true})
this.isDoubleReleasePan = true
this.isSingleReleasePan = true
this.isSetSingleFocus = false
this.isSetDoubleFocus = false
if (!this.isAnimated) {
this.releaseAnimated()
}
}
releaseAnimated = () => {
this.isAnimated = true
this.animatedScale.setValue(this.scale)
this.animatedOffsetX.setValue(this.offsetX)
this.animatedOffsetY.setValue(this.offsetY)
if (this.animatedScale._value < 1) {
const done = result => {
if (result.finished) {
this.scale = 1
this.lastScale = 1
this.offsetX = 0
this.offsetY = 0
this.isAnimated = false
}
}
this.parallelAnimated({
scale: 1,
offsetX: 0,
offsetY: 0,
done: done
})
} else if (this.animatedScale._value == 1) {
const done = result => {
if (result.finished) {
this.scale = 1
this.lastScale = 1
this.offsetX = 0
this.offsetY = 0
this.isAnimated = false
}
}
this.parallelWithoutScaleAnimated({
offsetX: 0,
offsetY: 0,
done: done
})
} else if (this.animatedScale._value > 1 && this.animatedScale._value <= 2) {
const { offsetX, offsetY } = this.getBoundaryOffset(this.animatedScale._value)
const done = result => {
if (result.finished) {
this.scale = this.animatedScale._value
this.lastScale = this.animatedScale._value
this.offsetX = offsetX
this.offsetY = offsetY
this.isAnimated = false
}
}
this.parallelWithoutScaleAnimated({
offsetX: offsetX,
offsetY: offsetY,
done: done
})
} else {
const { offsetX, offsetY } = this.getBoundaryOffset(2)
const done = result => {
if (result.finished) {
this.scale = 2
this.lastScale = 2
this.offsetX = offsetX
this.offsetY = offsetY
this.isAnimated = false
}
}
this.parallelAnimated({
scale: 2,
offsetX: offsetX,
offsetY: offsetY,
done: done
})
}
}
getFlatList = () => {
return(this.AnimatedFlatList.getNode())
}
render() {
return (
<View style={styles.comicbook}>
<TapGestureHandler
onHandlerStateChange={this.onSingleTap}
waitFor={["double_tap","pinch","pan","flastlist"]}
numberOfTaps={1}
>
<TapGestureHandler
id="double_tap"
waitFor={["pinch","pan","flastlist"]}
onHandlerStateChange={this.onDoubleTap}
numberOfTaps={2}
>
<PanGestureHandler
id="pan"
simultaneousHandlers="pinch"
//onActivated={this.onPanStart}
onGestureEvent={this.onPan}
//onEnded={this.onPanEnd}
avgTouches
minPointers={2}
maxPointers={2}
minDeltaX={0}
minDeltaY={0}
minOffsetX={0}
minOffsetY={0}
minVelocityX={0}
minVelocityY={0}
minDist={0}
>
<PinchGestureHandler
id="pinch"
simultaneousHandlers="pan"
onActivated={this.onPinchStart}
onGestureEvent={this.onPinch}
onEnded={this.onPinchEnd}
>
<NativeViewGestureHandler
id="flatlist"
>
<AnimatedFlatList
{...this.props}
ref={ref => this.AnimatedFlatList = ref}
style={{
transform: [
{scale: this.animatedScale},
{translateX: this.animatedOffsetX},
{translateY: this.animatedOffsetY}
]
}}
scrollEventThrottle={16}
showsVerticalScrollIndicator={false}
horizontal={false}
/>
</NativeViewGestureHandler>
</PinchGestureHandler>
</PanGestureHandler>
</TapGestureHandler>
</TapGestureHandler>
</View>
)
}
}
InteractiveFlatList.propTypes = {
data: PropTypes.array,
renderItem: PropTypes.func.isRequired,
onSingleClickTopArea: PropTypes.func,
onSingleClickMiddleArea: PropTypes.func,
onSingleClickBottomArea: PropTypes.func,
onDoubleClick: PropTypes.func,
onPinchStart: PropTypes.func
}
InteractiveFlatList.defaultProps = {
data: []
}
const { width, height } = Dimensions.get('window')
const AnimatedFlatList = Animated.createAnimatedComponent(FlatList)
const styles = StyleSheet.create({
comicbook: {
flex: 1,
backgroundColor: '#000000'
}
})