|
| 1 | +import React from 'react'; |
| 2 | +import { |
| 3 | + Gesture, |
| 4 | + GestureDetector, |
| 5 | + GestureType, |
| 6 | + MouseButton, |
| 7 | + Directions, |
| 8 | + ScrollView, |
| 9 | +} from 'react-native-gesture-handler'; |
| 10 | +import { StyleSheet, View, Text } from 'react-native'; |
| 11 | + |
| 12 | +const COLORS = ['darkmagenta', 'darkgreen', 'darkblue', 'crimson', 'pink']; |
| 13 | + |
| 14 | +type TestProps = { |
| 15 | + name: string; |
| 16 | + gestureHandlers: GestureType[]; |
| 17 | +}; |
| 18 | + |
| 19 | +function Test({ name, gestureHandlers }: TestProps) { |
| 20 | + return ( |
| 21 | + <View style={styles.center}> |
| 22 | + <Text>{name}</Text> |
| 23 | + <View |
| 24 | + style={[ |
| 25 | + { margin: 10, width: '100%', flexDirection: 'row' }, |
| 26 | + styles.center, |
| 27 | + ]}> |
| 28 | + {gestureHandlers.map((handler, index) => { |
| 29 | + return ( |
| 30 | + <GestureDetector gesture={handler} key={index}> |
| 31 | + <View style={[styles.box, { backgroundColor: COLORS[index] }]} /> |
| 32 | + </GestureDetector> |
| 33 | + ); |
| 34 | + })} |
| 35 | + </View> |
| 36 | + </View> |
| 37 | + ); |
| 38 | +} |
| 39 | + |
| 40 | +function TapTests() { |
| 41 | + const leftTap = Gesture.Tap() |
| 42 | + .mouseButton(MouseButton.LEFT) |
| 43 | + .onEnd(() => console.log('Tap with left')); |
| 44 | + |
| 45 | + const middleTap = Gesture.Tap() |
| 46 | + .mouseButton(MouseButton.MIDDLE) |
| 47 | + .onEnd(() => console.log('Tap with middle')); |
| 48 | + |
| 49 | + const rightTap = Gesture.Tap() |
| 50 | + .mouseButton(MouseButton.RIGHT) |
| 51 | + .onEnd(() => console.log('Tap with right')); |
| 52 | + |
| 53 | + const leftRightTap = Gesture.Tap() |
| 54 | + .mouseButton(MouseButton.LEFT | MouseButton.RIGHT) |
| 55 | + .onEnd(() => console.log('Tap with left | right')); |
| 56 | + |
| 57 | + const allTap = Gesture.Tap() |
| 58 | + .mouseButton(MouseButton.ALL) |
| 59 | + .onEnd(() => console.log('Tap with any button')); |
| 60 | + |
| 61 | + const gestureHandlers = [leftTap, middleTap, rightTap, leftRightTap, allTap]; |
| 62 | + |
| 63 | + return <Test name={'Tap'} gestureHandlers={gestureHandlers} />; |
| 64 | +} |
| 65 | + |
| 66 | +function PanTests() { |
| 67 | + const leftPan = Gesture.Pan() |
| 68 | + .mouseButton(MouseButton.LEFT) |
| 69 | + .onChange(() => console.log('Panning with left')); |
| 70 | + |
| 71 | + const middlePan = Gesture.Pan() |
| 72 | + .mouseButton(MouseButton.MIDDLE) |
| 73 | + .onChange(() => console.log('Panning with middle')); |
| 74 | + |
| 75 | + const rightPan = Gesture.Pan() |
| 76 | + .mouseButton(MouseButton.RIGHT) |
| 77 | + .onChange(() => console.log('Panning with right')); |
| 78 | + |
| 79 | + const leftRightPan = Gesture.Pan() |
| 80 | + .mouseButton(MouseButton.LEFT | MouseButton.RIGHT) |
| 81 | + .onChange(() => console.log('Panning with left | right')); |
| 82 | + |
| 83 | + const allPan = Gesture.Pan() |
| 84 | + .mouseButton(MouseButton.ALL) |
| 85 | + .onChange(() => console.log('Panning with any button')); |
| 86 | + |
| 87 | + const gestureHandlers = [leftPan, middlePan, rightPan, leftRightPan, allPan]; |
| 88 | + |
| 89 | + return <Test name={'Pan'} gestureHandlers={gestureHandlers} />; |
| 90 | +} |
| 91 | + |
| 92 | +function LongPressTests() { |
| 93 | + const leftLongPress = Gesture.LongPress() |
| 94 | + .mouseButton(MouseButton.LEFT) |
| 95 | + .onStart(() => console.log('LongPress with left')); |
| 96 | + |
| 97 | + const middleLongPress = Gesture.LongPress() |
| 98 | + .mouseButton(MouseButton.MIDDLE) |
| 99 | + .onStart(() => console.log('LongPress with middle')); |
| 100 | + |
| 101 | + const rightLongPress = Gesture.LongPress() |
| 102 | + .mouseButton(MouseButton.RIGHT) |
| 103 | + .onStart(() => console.log('LongPress with right')); |
| 104 | + |
| 105 | + const leftRightLongPress = Gesture.LongPress() |
| 106 | + .mouseButton(MouseButton.LEFT | MouseButton.RIGHT) |
| 107 | + .onStart(() => console.log('LongPress with left | right')); |
| 108 | + |
| 109 | + const allLongPress = Gesture.LongPress() |
| 110 | + .mouseButton(MouseButton.ALL) |
| 111 | + .onStart(() => console.log('LongPress with any button')); |
| 112 | + |
| 113 | + const gestureHandlers = [ |
| 114 | + leftLongPress, |
| 115 | + middleLongPress, |
| 116 | + rightLongPress, |
| 117 | + leftRightLongPress, |
| 118 | + allLongPress, |
| 119 | + ]; |
| 120 | + |
| 121 | + return <Test name={'LongPress'} gestureHandlers={gestureHandlers} />; |
| 122 | +} |
| 123 | + |
| 124 | +function FlingTests() { |
| 125 | + const leftFling = Gesture.Fling() |
| 126 | + .direction(Directions.LEFT | Directions.RIGHT) |
| 127 | + .mouseButton(MouseButton.LEFT) |
| 128 | + .onStart(() => console.log('Fling with left')); |
| 129 | + |
| 130 | + const middleFling = Gesture.Fling() |
| 131 | + .direction(Directions.LEFT | Directions.RIGHT) |
| 132 | + .mouseButton(MouseButton.MIDDLE) |
| 133 | + .onStart(() => console.log('Fling with middle')); |
| 134 | + |
| 135 | + const rightFling = Gesture.Fling() |
| 136 | + .direction(Directions.LEFT | Directions.RIGHT) |
| 137 | + .mouseButton(MouseButton.RIGHT) |
| 138 | + .onStart(() => console.log('Fling with right')); |
| 139 | + |
| 140 | + const leftRightFling = Gesture.Fling() |
| 141 | + .direction(Directions.LEFT | Directions.RIGHT) |
| 142 | + .mouseButton(MouseButton.LEFT | MouseButton.RIGHT) |
| 143 | + .onStart(() => console.log('Fling with left | right')); |
| 144 | + |
| 145 | + const allFling = Gesture.Fling() |
| 146 | + .direction(Directions.LEFT | Directions.RIGHT) |
| 147 | + .mouseButton(MouseButton.ALL) |
| 148 | + .onStart(() => console.log('Fling with any button')); |
| 149 | + |
| 150 | + const gestureHandlers = [ |
| 151 | + leftFling, |
| 152 | + middleFling, |
| 153 | + rightFling, |
| 154 | + leftRightFling, |
| 155 | + allFling, |
| 156 | + ]; |
| 157 | + |
| 158 | + return <Test name={'Fling'} gestureHandlers={gestureHandlers} />; |
| 159 | +} |
| 160 | + |
| 161 | +export default function Buttons() { |
| 162 | + return ( |
| 163 | + <ScrollView style={{ flex: 1 }}> |
| 164 | + <TapTests /> |
| 165 | + <PanTests /> |
| 166 | + <LongPressTests /> |
| 167 | + <FlingTests /> |
| 168 | + </ScrollView> |
| 169 | + ); |
| 170 | +} |
| 171 | + |
| 172 | +const styles = StyleSheet.create({ |
| 173 | + center: { |
| 174 | + alignItems: 'center', |
| 175 | + justifyContent: 'space-around', |
| 176 | + }, |
| 177 | + box: { |
| 178 | + width: 75, |
| 179 | + height: 75, |
| 180 | + }, |
| 181 | +}); |
0 commit comments