Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[iOS: Expo 47] velocity is 0 in useAnimatedGestureHandler #3758

Open
jjenzz opened this issue Nov 9, 2022 · 8 comments
Open

[iOS: Expo 47] velocity is 0 in useAnimatedGestureHandler #3758

jjenzz opened this issue Nov 9, 2022 · 8 comments
Labels
Needs review Issue is ready to be reviewed by a maintainer Platform: iOS This issue is specific to iOS Repro provided A reproduction with a snippet of code, snack or repo is provided

Comments

@jjenzz
Copy link

jjenzz commented Nov 9, 2022

Description

With Expo 47 on iOS, I'm always getting a 0 value when trying to access the event.velocityY property in useAnimatedGestureHandler.

Steps to reproduce

  1. npx create-expo-app reanimated-velocity
  2. npx expo install react-native-reanimated
  3. npx expo install react-native-gesture-handler
  4. update babel.config.js to include react-native-reanimated/plugin
  5. add the following code to your App.js and then npm run ios
  6. click "Open" and then swipe down on the red box, velocity will alert as 0
import * as React from "react";
import * as ReactNative from "react-native";
import * as GestureHandler from "react-native-gesture-handler";
import Animated, * as Reanimated from "react-native-reanimated";

export default function App() {
  const [open, setOpen] = React.useState(false);
  const translateY = Reanimated.useSharedValue(0);

  const handleGestureEvent = Reanimated.useAnimatedGestureHandler({
    onActive: (event) => (translateY.value = event.translationY),
    onEnd: (event) => {
      Reanimated.runOnJS(alert)(event.velocityY);
      translateY.value = Reanimated.withSpring(0, null, () => {
        return Reanimated.runOnJS(setOpen)(false);
      });
    },
  });

  const boxStyle = Reanimated.useAnimatedStyle(() => ({
    transform: [{ translateY: translateY.value }],
  }));

  return (
    <GestureHandler.GestureHandlerRootView
      style={{ flex: 1, alignItems: "center", justifyContent: "center" }}
    >
      {open && (
        <GestureHandler.PanGestureHandler onGestureEvent={handleGestureEvent}>
          <Animated.View
            style={[
              boxStyle,
              { width: 300, height: 300, backgroundColor: "red" },
            ]}
          />
        </GestureHandler.PanGestureHandler>
      )}
      <ReactNative.Button title="Open" onPress={() => setOpen(true)} />
    </GestureHandler.GestureHandlerRootView>
  );
}

Snack or a link to a repository

https://github.com/jjenzz/reanimated-velocity

Reanimated version

2.12.0

React Native version

0.70.5

Platforms

iOS

JavaScript runtime

No response

Workflow

Expo managed workflow

Architecture

Fabric (New Architecture)

Build type

Debug mode

Device

iOS simulator

Device model

iPhone X iOS 15.4

Acknowledgements

Yes

@jjenzz jjenzz added the Needs review Issue is ready to be reviewed by a maintainer label Nov 9, 2022
@jjenzz jjenzz changed the title [iOS: Expo 47] onEnd velocity is 0 with useAnimatedGestureHandler [iOS: Expo 47] onEnd velocity is 0 in useAnimatedGestureHandler Nov 9, 2022
@github-actions github-actions bot added Repro provided A reproduction with a snippet of code, snack or repo is provided Platform: iOS This issue is specific to iOS labels Nov 9, 2022
@jjenzz jjenzz changed the title [iOS: Expo 47] onEnd velocity is 0 in useAnimatedGestureHandler [iOS: Expo 47] velocity is 0 in useAnimatedGestureHandler Nov 9, 2022
@stefanoortisi
Copy link

same for me

@tbaustin
Copy link

@jjenzz Any feedback from this?

@jjenzz
Copy link
Author

jjenzz commented Mar 20, 2023

@tbaustin nope, nothing to report unfortunately

@nickrugge
Copy link

nickrugge commented Mar 29, 2023

same for me

    "expo": "~48.0.9",
    "react-native": "0.71.4",
    "react-native-reanimated": "~2.14.4",
    "react-native-gesture-handler": "~2.9.0"

@nemrosim
Copy link

nemrosim commented May 12, 2023

Same for me (expo 48), event.velocityY will always be zero for iOS simulator;

  const gestureHandler = useAnimatedGestureHandler({
    onStart: (_, context: any) => {
      context.startY = offset.value;
    },
    onActive: (event, context) => {
      offset.value = event.translationY + context.startY;
    },
    onEnd: (event) => {
      offset.value = withDecay({
        velocity: event.velocityY / 5,
      });
    },
  });

package.json:

"dependencies": {
    "@babel/plugin-proposal-export-namespace-from": "^7.18.9",
    "expo": "~48.0.15",
    "react": "18.2.0",
    "react-native": "0.71.7",
    "react-native-gesture-handler": "~2.9.0",
    "react-native-reanimated": "~2.14.4",
    "react-native-safe-area-context": "4.5.0",
    "react-native-screens": "~3.20.0",
    "react-native-web": "~0.18.10"
...
  },

babel.config.js

module.exports = function (api) {
  api.cache(true);
  return {
    presets: ['babel-preset-expo'],
    plugins: [
      // required for reanimated - web
      // https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/web-support/
      '@babel/plugin-proposal-export-namespace-from',
      'react-native-reanimated/plugin',
    ],
  };
};

Works without issues on Android/iOS devices and Web.

@CostasCF
Copy link

CostasCF commented Dec 5, 2023

any updates?

@minawalphonce
Copy link

minawalphonce commented Mar 15, 2024

I have managed to make it work by upgrading to sdk 49

    "expo": "49",
    "react-native-gesture-handler": "~2.12.0",
    "react-native-reanimated": "~3.3.0",

@dprevost-LMI
Copy link

dprevost-LMI commented Feb 21, 2025

If your problem is iOS only with velocityX and you are also pulling react-native-pager-view >= 6.2.0, your problem might be similar to mine. See callstack/react-native-pager-view#972

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs review Issue is ready to be reviewed by a maintainer Platform: iOS This issue is specific to iOS Repro provided A reproduction with a snippet of code, snack or repo is provided
Projects
None yet
Development

No branches or pull requests

8 participants