Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Create a Draggable Card with React Native Pan Responder, and Animated.decay

    react-nativeReact Native
    0.35 - 0.39

    In this lesson we'll create a PanResponder to allow us to drag a card around the screen. We'll use Animated.event to automatically set the values of our Animated.Value and when the card is released we'll use Animated.decay to decelerate it to a stop.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    We'll start by importing Animated and PanResponder from React Native. Next, we'll create a componentWillMount lifecycle method, then type this._panResponder is equal to the PanResponder that we create.

    This PanResponder will then give us panHandlers, which we can then spread this._panResponder.panHandlers onto the view that we want to attach touch events to. Now we'll go create our animated value by typing this._animatedValue = new Animated.ValueXY.

    Next, we'll replace our onPanResponderMove with Animated.event, which is a function that takes an array. Animated will then traverse all of the arguments and assign those arguments to our animated values that we pass in. We'll ignore the first event.

    We'll map the delta x, the change in the X location, to this._animatedValue.x and the delta y to this._animatedValue.y. Next we'll switch our view from a regular view to an animated view. Then we'll create const, which is an object.

    We'll say transform: this._animatedValue.getTranslateTransform, which is a helper function to return our TranslateTransform. Then we'll attach that to our animated view. Now, when we refresh our emulator, we can drag the square around the screen.

    However, if I drop it and then try to drag it again, the animated value will be set to the delta of the new touch, which will cause the square to jump. Because we're dealing with the delta of the drag, we need to attach a listener by saying this._animatedValue.addListener, which is a function that takes a function.

    The first argument is value. Then we'll assign it to this._value=value. We'll set up our default at this._value = {x: 0y: 0In our PanResponderGrant, we'll say this._animatedValue.setOffset({x: this._value.x, y: this._value.y}).

    Additionally, we'll set this._animatedValue.setValue({x: 0y: 0to clear out our animation. Now when we refresh our emulator, we can drag the square across the screen, drop it, and then pick it up, and drag it again.

    In our onPanResponderRelease, which is when we release our touch, we'll say this._animatedValue.flattenOffset, which will flatten our offset delta into our animated value to prepare us for another drag.

    Then we'll use Animated.decay, which is a function that takes this._animatedValue and a configuration object. It takes a deceleration, which we'll say is 0997. Then we'll give it the velocity of our final touch, which is an object that takes an X direction which it can get from gestureState.vx and our y from gestureState.vy.

    Now, we'll call start on our animation. If we refresh our emulator, we can drag our square across the screen. Then we release it. It will decay its acceleration in the direction that we released it.