0

实现这一目标的方法是什么。

带有 Animated.event 的回调工作正常

带块的回调没有反馈:(

import React, { useState, useEffect } from "react";
import { View, Text, FlatList, Dimensions, Image } from "react-native";
import Animated from "react-native-reanimated";
import { PanGestureHandler, TapGestureHandler, State } from "react-native-gesture-handler";
const { Value, event, call, set, add, block } = Animated;

const bottom = new Value(100);

//want to achieve something like this 
const onHandlerStateChange = block([set(bottom, add(bottom, 10))]);

// the line below Does work 
// const onHandlerStateChange = event([{ nativeEvent: { y: bottom } }]); 

const Comp = () => {
    return (
        <TapGestureHandler onHandlerStateChange={onHandlerStateChange}>
            <Animated.View style={{ flex: 1}}>
                <Animated.View style={{ flex: 1, position: "absolute", bottom: bottom }}>
                    <Text> some text</Text>
                </Animated.View>
            </Animated.View>
        </TapGestureHandler>
    );
};

export default Comp;

*const onHandlerStateChange = block([set(bottom, add(bottom, 10))]); *在这里不起作用

4

1 回答 1

0

这是有效的解决方案
提示: 了解如何重新激活评估节点将非常有用
这里是官方文档的链接,可以帮助您理解
https://software-mansion.github.io/react-native-reanimated/about-reanimated.html #at-most-once-evaluation-the-algorithm-

import React, { useState, useEffect } from "react";
import { View, Text, FlatList, Dimensions, Image } from "react-native";
import Animated from "react-native-reanimated";
import {
    PanGestureHandler,
    TapGestureHandler,
    State,
} from "react-native-gesture-handler";
const { Value, event, call, set, add, cond, eq, block } = Animated;

const gstate = new Value(-1);
const onHandlerStateChange = event([{ nativeEvent: { state: gstate } }]);

let bottom = new Value(100);
bottom = cond(eq(gstate, State.END), [set(bottom, add(bottom, 10))], bottom);

const Comp = () => {
    return (
        <TapGestureHandler onHandlerStateChange={onHandlerStateChange}>
            <Animated.View style={{ flex: 1, backgroundColor: "pink" }}>
                <Animated.View
                    style={{ flex: 1, position: "absolute", bottom: bottom }}
                >
                    <Text> some text</Text>
                </Animated.View>
            </Animated.View>
        </TapGestureHandler>
    );
};

export default Comp;
于 2020-04-12T22:25:29.237 回答