-
-
Notifications
You must be signed in to change notification settings - Fork 987
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[macOS] Add
FlingGestureHandler
(#3028)
## Description This PR adds `FlingGestureHandler` on `macOS`. From now, all handlers, except`ForceTouchGestureHandler`, are available on this platform 🥳 ## Test plan Tested on newly added example.
- Loading branch information
Showing
7 changed files
with
369 additions
and
19 deletions.
There are no files selected for viewing
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,90 @@ | ||
import { StyleSheet, View } from 'react-native'; | ||
import { | ||
Directions, | ||
Gesture, | ||
GestureDetector, | ||
} from 'react-native-gesture-handler'; | ||
import Animated, { | ||
interpolateColor, | ||
useAnimatedStyle, | ||
useSharedValue, | ||
withTiming, | ||
} from 'react-native-reanimated'; | ||
|
||
const AnimationDuration = 100; | ||
|
||
const Colors = { | ||
Initial: '#0a2688', | ||
Active: '#6fcef5', | ||
}; | ||
|
||
export default function FlingExample() { | ||
const isActive = useSharedValue(false); | ||
const colorProgress = useSharedValue(0); | ||
const color1 = useSharedValue(Colors.Initial); | ||
const color2 = useSharedValue(Colors.Active); | ||
|
||
const animatedStyles = useAnimatedStyle(() => { | ||
const backgroundColor = interpolateColor( | ||
colorProgress.value, | ||
[0, 1], | ||
[color1.value, color2.value] | ||
); | ||
|
||
return { | ||
transform: [ | ||
{ | ||
scale: withTiming(isActive.value ? 1.2 : 1, { | ||
duration: AnimationDuration, | ||
}), | ||
}, | ||
], | ||
backgroundColor, | ||
}; | ||
}); | ||
|
||
const g = Gesture.Fling() | ||
.direction(Directions.LEFT | Directions.UP) | ||
.onBegin(() => { | ||
console.log('onBegin'); | ||
}) | ||
.onStart(() => { | ||
console.log('onStart'); | ||
isActive.value = true; | ||
colorProgress.value = withTiming(1, { | ||
duration: AnimationDuration, | ||
}); | ||
}) | ||
.onEnd(() => console.log('onEnd')) | ||
.onFinalize((_, success) => { | ||
console.log('onFinalize', success); | ||
|
||
isActive.value = false; | ||
|
||
colorProgress.value = withTiming(0, { | ||
duration: AnimationDuration, | ||
}); | ||
}); | ||
|
||
return ( | ||
<View style={styles.container}> | ||
<GestureDetector gesture={g}> | ||
<Animated.View style={[styles.box, animatedStyles]} /> | ||
</GestureDetector> | ||
</View> | ||
); | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
container: { | ||
flex: 1, | ||
justifyContent: 'space-around', | ||
alignItems: 'center', | ||
}, | ||
|
||
box: { | ||
width: 100, | ||
height: 100, | ||
borderRadius: 20, | ||
}, | ||
}); |
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,3 +1,4 @@ | ||
#import "../RNGHVector.h" | ||
#import "RNGestureHandler.h" | ||
|
||
@interface RNFlingGestureHandler : RNGestureHandler | ||
|
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,31 @@ | ||
// | ||
// RNGHVector.h | ||
// Pods | ||
// | ||
// Created by Michał Bert on 05/08/2024. | ||
// | ||
|
||
#import "RNGestureHandlerDirection.h" | ||
|
||
#ifndef RNGHVector_h | ||
#define RNGHVector_h | ||
|
||
@interface Vector : NSObject | ||
|
||
@property (atomic, readonly, assign) double x; | ||
@property (atomic, readonly, assign) double y; | ||
@property (atomic, readonly, assign) double unitX; | ||
@property (atomic, readonly, assign) double unitY; | ||
@property (atomic, readonly, assign) double magnitude; | ||
|
||
+ (Vector *_Nonnull)fromDirection:(RNGestureHandlerDirection)direction; | ||
+ (Vector *_Nonnull)fromVelocityX:(double)vx withVelocityY:(double)vy; | ||
- (nonnull instancetype)initWithX:(double)x withY:(double)y; | ||
- (double)computeSimilarity:(Vector *_Nonnull)other; | ||
- (BOOL)isSimilar:(Vector *_Nonnull)other withThreshold:(double)threshold; | ||
|
||
@end | ||
|
||
static double MINIMAL_RECOGNIZABLE_MAGNITUDE = 0.1; | ||
|
||
#endif /* RNGHVector_h */ |
Oops, something went wrong.