reactjs - 如何重置 react 原生动画?

标签 reactjs react-native

我使用 react-native 中的 Animated 制作了一些简单的动画 动画在第一次 onPress 后按预期工作,但我不知道如何重置它,使其适用于下一次点击。

constructor(props) {
    super(props);
    this.spinValue = new Animated.Value(0);
    // Second interpolate beginning and end values (in this case 0 and 1)
    this.spin = this.spinValue.interpolate({
        inputRange: [0, 1, 2, 3, 4],
        outputRange: ['0deg', '4deg', '0deg', '-4deg', '0deg']
    });
}

handlePress() {
    // First set up animation 
    Animated.timing(
        this.spinValue,
        {
            toValue: 4,
            duration: 300,
        }
    ).start();
}

在渲染中

<TouchableWithoutFeedback onPress={() => { this.handlePress(); }} >
                    <Animated.Image
                        source={someImg}
                        style={{ height: undefined, width: undefined, flex: 1, transform: [{ rotate: this.spin }, { perspective: 1000 }] }}
                        resizeMode={'contain'}
                        resizeMethod="resize"
                    />
</TouchableWithoutFeedback>

有什么建议吗?谢谢

最佳答案

您可以使用this.spinValue.setValue(0)来重置它。您可以将其添加到handlePress 的开头。

关于reactjs - 如何重置 react 原生动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51712114/

相关文章:

react-native - 无法编译android代码-无法解析java.lang.Object错误

css - 设置半颗星的颜色

javascript - 在reactjs中保留表列的缓存

reactjs - 代理错误: Could not proxy request/payment from localhost:3000 to https://localhost:5000/

javascript - 如何使用所有登录注册和 api 调用构建 React 应用程序

react-native - React Native 博览会-facebook : Error ‘Given URL is not allowed by the Application Configuration.’

javascript - 从数组中删除项目(React-Native + Redux)

WebStorm + React Native?

javascript - 使用 react-native-router-flux 将数据从屏幕发送到 Tab 屏幕

javascript - 使用非子组件的 ReactJS 中的子数组唯一键