最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Position resets to 0 while using PanGestureHandler from react-native-gesture-handler - Stack Overflow

programmeradmin1浏览0评论
import React, { useRef, useState, useCallback } from 'react';
import { View, Animated } from 'react-native';
import { PanGestureHandler } from 'react-native-gesture-handler';


const InfoBox = ({ info }: Props) => {
  const [currenPos, setCurrentPos] = useState<number>(0);
  const translateY = useRef(new Animated.Value(0)).current;

  const onGestureEvent = useCallback(
    Animated.event(
      [
        {
          nativeEvent: {
            translationY: translateY,
          },
        },
      ],
      {
        useNativeDriver: true,
      },
    ),
    [],
  );

  const handleTransformStyle = {
    transform: [
      {
        translateY,
      },
      {
        translateX: -55,
      },
    ],
  };

  return (
    <View style={styles.container}>
      <PanGestureHandler
        onGestureEvent={onGestureEvent}
      >
        <Animated.View style={[styles.handleBar, handleTransformStyle]}>
          <View style={styles.separator} />
        </Animated.View>
      </PanGestureHandler>
    </View>
  );
};

export default InfoBox;

I have provided a very simple example of PanGestureHandler. On moving the box for the first time, it moves to a position and stays at that position. But, if I try to move it again, it starts from position zero instead of starting from the same position where I had left it.

Any help would be much appreciated.

EDIT

I have realized this occurs due to the offset getting reset.

import React, { useRef, useState, useCallback } from 'react';
import { View, Animated } from 'react-native';
import { PanGestureHandler } from 'react-native-gesture-handler';


const InfoBox = ({ info }: Props) => {
  const [currenPos, setCurrentPos] = useState<number>(0);
  const translateY = useRef(new Animated.Value(0)).current;

  const onGestureEvent = useCallback(
    Animated.event(
      [
        {
          nativeEvent: {
            translationY: translateY,
          },
        },
      ],
      {
        useNativeDriver: true,
      },
    ),
    [],
  );

  const handleTransformStyle = {
    transform: [
      {
        translateY,
      },
      {
        translateX: -55,
      },
    ],
  };

  return (
    <View style={styles.container}>
      <PanGestureHandler
        onGestureEvent={onGestureEvent}
      >
        <Animated.View style={[styles.handleBar, handleTransformStyle]}>
          <View style={styles.separator} />
        </Animated.View>
      </PanGestureHandler>
    </View>
  );
};

export default InfoBox;

I have provided a very simple example of PanGestureHandler. On moving the box for the first time, it moves to a position and stays at that position. But, if I try to move it again, it starts from position zero instead of starting from the same position where I had left it.

Any help would be much appreciated.

EDIT

I have realized this occurs due to the offset getting reset.

Share Improve this question edited Jul 17, 2020 at 20:46 shet_tayyy asked Jul 16, 2020 at 22:24 shet_tayyyshet_tayyy 5,75512 gold badges53 silver badges93 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

I have found a solution to this problem.

Add onHandlerStateChange to PanGestureHandler:

 <PanGestureHandler
    onGestureEvent={onPanGestureEvent}
    onHandlerStateChange={onHandlerStateChange}
 >
...
</PanGestureHandler>

Then, create a onHandlerStateChange function like so:

  const onHandlerStateChange = useCallback(() => {
    translateY.extractOffset();
  }, []);

translateY.extractOffset(); performed the magic. It sets the offset value. Phew!

发布评论

评论列表(0)

  1. 暂无评论