首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >useAnimatedGestureHandler不工作在反应-本机-再生库在反应-本地

useAnimatedGestureHandler不工作在反应-本机-再生库在反应-本地
EN

Stack Overflow用户
提问于 2022-04-13 04:21:50
回答 1查看 792关注 0票数 0

我是ReactNative的新手,我为我的项目做SwipeButton。但我对useAnimatedGestureHandler有如下问题:

代码语言:javascript
运行
复制
TypeError: (0, _reactNativeReanimated.useAnimatedGestureHandler) is not a function. (In '(0, _reactNativeReanimated.useAnimatedGestureHandler)(0)', '(0, _reactNativeReanimated.useAnimatedGestureHandler)' is undefined)

我想可能是因为它的版本是还原的-v2,而我使用的是reactivev1,这里有人能为我把这个代码转换为rev1吗,

我的代码如下所示:

代码语言:javascript
运行
复制
import { View, Text, ViewBase } from 'react-native';
import { PanGestureHandler } from 'react-native-gesture-handler';
import Animated, {
    interpolate,
    useAnimatedGestureHandler,
    useSharedValue,
    useAnimatedStyle
} from 'react-native-reanimated';

const SampleTest = (props) => {
    const buttonVal = useSharedValue(0);

    const handleGesture = useAnimatedGestureHandler({
        onStart: (_, ctx) => {
            ctx.x = buttonVal.value;
        },
        onActive: ({ translationX }, ctx) => {
            buttonVal.value = translationX + ctx.x;
        },
        onEnd: ({ translationX }) => {
            if (translationX > _WIDTH / 3) {
                buttonVal.value = withTiming(_WIDTH * 0.85);
            } else {
                buttonVal.value = withTiming(0);
            }
        }
    });
    const pathAnimatedStyle = useAnimatedStyle(() => {
        return {
            width: interpolate(
                buttonVal.value,
                [0, _WIDTH * 0.85],
                [50, _WIDTH * 0.85]
            ),
            height: 50,
            borderRadius: 25,
            backgroundColor: 'gray',
            position: 'absolute'
        };
    });
    return (
        <>
            <PanGestureHandler onGestureEvent={handleGesture}>
                <Animated.View
                    style={{
                        width: '85%',
                        height: 50,
                        borderRadius: 25,
                        backgroundColor: 'yellow',
                        shadowColor: 'gray',
                        justifyContent: 'center',
                        shadowOffset: { width: 0, height: 2 },
                        shadowOpacity: 0.25,
                        shadowRadius: 3.84,
                        elevation: 5
                    }}
                >
                    <Text
                        style={{
                            textAlign: 'center',
                            opacity: 0.9,
                            color: 'white',

                            fontWeight: 'bold'
                        }}
                    >
                        Swipe button
                    </Text>
                    <Animated.View style={pathAnimatedStyle}></Animated.View>
                </Animated.View>
            </PanGestureHandler>
        </>
    );
};

谢谢各位。

EN

回答 1

Stack Overflow用户

发布于 2022-10-19 13:13:43

<PanGestureHandler/>处理程序包装<GestureHandlerRootView/>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71851849

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档