question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

V2 alpha: Unable to run TapGestureHandler-based animation more than once

See original GitHub issue

Description

Unsure if this is a bug, but when using <TapGestureHandler /> and reanimated v2 (which is amazing!), I’m unable to re-trigger my animation after it finishes. It’s pretty simple - on tap start, set the scale from 1.0 to 0.9. On tap end, reset back to 1.0. It works once, then not again. Having migrated from v1 to v2, I suspect it may be an underlying issue with the clock? I didn’t see anything documentation suggesting that anything other than what’s below is required for triggering this kind of simple animation more than once. The basic random width example works fine.

Code

import React from 'react'
import { View } from 'react-native'
import Animated, {
  useSharedValue,
  withSpring,
  useAnimatedStyle,
  useAnimatedGestureHandler,
} from 'react-native-reanimated';
import { TapGestureHandler } from 'react-native-gesture-handler';

const styles = {
  container: {
    flex: 1,
    backgroundColor: 'rgba(200, 0, 0, 0.5)',
    justifyContent: 'center',
    alignItems: 'center'
  },
  box: {
    width: 140,
    height: 300,
    backgroundColor: 'rgba(200, 200, 200, 0.6)',
    borderRadius: 26
  }
}

export function ReanimatedExample() {
  const s = useSharedValue(1);

  const tapGestureHandler = useAnimatedGestureHandler({
    onStart: (event, ctx) => {
      s.value = withSpring(0.9);
    },
    onEnd: (event, ctx) => {
      s.value = withSpring(1);
    },
  });

  const animatedStyle = useAnimatedStyle(() => {
    return {
      transform: [
        { scale: s.value },
      ],
    };
  });

  return (
    <View style={styles.container}>
      <TapGestureHandler onGestureEvent={tapGestureHandler}>
        <Animated.View style={[
          styles.box,
          animatedStyle
        ]} />
      </TapGestureHandler>
    </View>
  );
}

Package versions

  • React: v16.11.0
  • React Native: v0.62.2
  • React Native Reanimated: v2.0.0-alpha.1
  • React Native Gesture Handler: v1.6.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:17 (9 by maintainers)

github_iconTop GitHub Comments

6reactions
terrysahaidakcommented, Jun 5, 2020

After some testing, I can clearly say this is a bug in reanimated’s useAnimatedGestureHandler.

The problem that it uses this condition for “onStart” check:

if (event.oldState === UNDETERMINED && handlers.onStart) {
  handlers.onStart(event, context);
}

But it should use this (at least for tap for sure) instead:

if (event.state === BEGAN && handlers.onStart) {
  handlers.onStart(event, context);
}

I will submit a fix later today.

1reaction
drewandrecommented, Jun 20, 2020

@divykj and @terrysahaidak 2.0.0-alpha.3 release seemed to solve the issue I was experiencing above and these changes worked great against the newest alpha release. I’m now able to re-run TapGestureHandler-based animations more than once. Thanks for your help!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Events | React Native Reanimated - Software Mansion
We use the TapGestureHandler component from react-native-gesture-handler library to wrap the main View in order to tell the framework which of the rendered ......
Read more >
Unable to resolve module `react-native-reanimated`
Run the following command: npm install react-native start react-native run-android. Then build your app. When it completes the build ...
Read more >
Add gestures - Expo Documentation
The React Native Gesture Handler library provides a way to interact with the native platform's gesture response system. To animate between gesture states,...
Read more >
Reanimated 2 - the new approach to creating animations in ...
Our aim is to take a look at Reanimated 2 hooks and learn how to use them in code. We are going to...
Read more >
Pan and Double Tap Gesture Animations in React Native
Gesture-based controls working with animations give some liveliness to ... Every time a user moves one or more fingers across the screen, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found