-
-
Notifications
You must be signed in to change notification settings - Fork 981
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add corner area detection to Fling gesture. (#2807)
## Description This PR implements a requested feature to activate fling on corners of two adjacent activated directions. ## Test plan - In project root run `yarn` - Go to `example/` and run `yarn` - Paste Fling example from Gesture Handler docs into EmptyExample.tsx - Run `yarn web` for Web or `yarn start` for IOS and Android
- Loading branch information
Showing
11 changed files
with
166 additions
and
27 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
android/src/main/java/com/swmansion/gesturehandler/core/DiagonalDirections.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.swmansion.gesturehandler.core | ||
|
||
object DiagonalDirections { | ||
const val DIRECTION_RIGHT_UP = GestureHandler.DIRECTION_RIGHT or GestureHandler.DIRECTION_UP | ||
const val DIRECTION_RIGHT_DOWN = GestureHandler.DIRECTION_RIGHT or GestureHandler.DIRECTION_DOWN | ||
const val DIRECTION_LEFT_UP = GestureHandler.DIRECTION_LEFT or GestureHandler.DIRECTION_UP | ||
const val DIRECTION_LEFT_DOWN = GestureHandler.DIRECTION_LEFT or GestureHandler.DIRECTION_DOWN | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import React from 'react'; | ||
import { StyleSheet, View } from 'react-native'; | ||
import { | ||
Directions, | ||
Gesture, | ||
GestureDetector, | ||
} from 'react-native-gesture-handler'; | ||
import Animated, { | ||
useSharedValue, | ||
useAnimatedStyle, | ||
withTiming, | ||
Easing, | ||
} from 'react-native-reanimated'; | ||
|
||
export default function Example() { | ||
const position = useSharedValue(0); | ||
const beginPosition = useSharedValue(0); | ||
|
||
const flingGesture = Gesture.Fling() | ||
.direction(Directions.LEFT | Directions.RIGHT) | ||
.onBegin((e) => { | ||
beginPosition.value = e.x; | ||
}) | ||
.onStart((e) => { | ||
const direction = Math.sign(e.x - beginPosition.value); | ||
position.value = withTiming(position.value + direction * 50, { | ||
duration: 300, | ||
easing: Easing.bounce, | ||
}); | ||
}); | ||
|
||
const animatedStyle = useAnimatedStyle(() => ({ | ||
transform: [{ translateX: position.value }], | ||
})); | ||
|
||
return ( | ||
<View style={styles.centerView}> | ||
<GestureDetector gesture={flingGesture}> | ||
<Animated.View style={[styles.box, animatedStyle]} /> | ||
</GestureDetector> | ||
</View> | ||
); | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
centerView: { | ||
flex: 1, | ||
justifyContent: 'center', | ||
alignItems: 'center', | ||
}, | ||
box: { | ||
height: 120, | ||
width: 120, | ||
backgroundColor: '#b58df1', | ||
marginBottom: 30, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,26 @@ | ||
const RIGHT = 1; | ||
const LEFT = 2; | ||
const UP = 4; | ||
const DOWN = 8; | ||
|
||
// public interface | ||
export const Directions = { | ||
RIGHT: 1, | ||
LEFT: 2, | ||
UP: 4, | ||
DOWN: 8, | ||
RIGHT: RIGHT, | ||
LEFT: LEFT, | ||
UP: UP, | ||
DOWN: DOWN, | ||
} as const; | ||
|
||
// internal interface | ||
export const DiagonalDirections = { | ||
UP_RIGHT: UP | RIGHT, | ||
DOWN_RIGHT: DOWN | RIGHT, | ||
UP_LEFT: UP | LEFT, | ||
DOWN_LEFT: DOWN | LEFT, | ||
} as const; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-redeclare -- backward compatibility; it can be used as a type and as a value | ||
export type Directions = typeof Directions[keyof typeof Directions]; | ||
// eslint-disable-next-line @typescript-eslint/no-redeclare | ||
export type DiagonalDirections = | ||
typeof DiagonalDirections[keyof typeof DiagonalDirections]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters