-
Notifications
You must be signed in to change notification settings - Fork 8
/
SecondTabScreen.js
82 lines (77 loc) · 2.05 KB
/
SecondTabScreen.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
import React, {Component} from 'react';
import { View, Text, Animated, StyleSheet } from 'react-native';
import {
PanGestureHandler,
ScrollView,
State,
} from 'react-native-gesture-handler';
export class DraggableBox extends Component {
constructor(props) {
super(props);
this._translateX = new Animated.Value(0);
this._translateY = new Animated.Value(0);
this._lastOffset = { x: 0, y: 0 };
this._onGestureEvent = Animated.event(
[
{
nativeEvent: {
translationX: this._translateX,
translationY: this._translateY,
},
},
],
{ useNativeDriver: true }
);
}
_onHandlerStateChange = event => {
if (event.nativeEvent.oldState === State.ACTIVE) {
this._lastOffset.x += event.nativeEvent.translationX;
this._lastOffset.y += event.nativeEvent.translationY;
this._translateX.setOffset(this._lastOffset.x);
this._translateX.setValue(0);
this._translateY.setOffset(this._lastOffset.y);
this._translateY.setValue(0);
}
};
render() {
return (
<PanGestureHandler
{...this.props}
onGestureEvent={this._onGestureEvent}
onHandlerStateChange={this._onHandlerStateChange}
id="dragbox">
<Animated.View
style={[
styles.box,
{
transform: [
{ translateX: this._translateX },
{ translateY: this._translateY },
],
},
]}
/>
</PanGestureHandler>
);
}
}
const SecondScreen = () => {
console.log('SecondScreen');
return (
<View>
<Text>SecondScreen</Text>
<DraggableBox minDist={30} />
</View>
);
};
const styles = StyleSheet.create({
box: {
width: 150,
height: 150,
alignSelf: 'center',
backgroundColor: 'plum',
margin: 10,
zIndex: 200,
},
});
export default SecondScreen;