我用react-native中的Animated制作了一些简单的动画

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开头加上它.

React-native相关问答推荐

react 本机函数不通过usContext触发

原生react :顶部的滑块位置

React Native - 如何在从另一个屏幕导航后将 ScrollView 滚动到给定位置

如何在 react-native 中设置 Onpress On Textinput

TextInput 防止 ScrollView 滚动

React Native 转换与枢轴点

带有 SafeAreaView-wrapper 的 react-native Modal 不起作用

Firebase JavaScript SDK 和 react-native-firebase 有什么区别

在使用 redux-persist 重新水化存储之前加载初始状态的默认值

如何将参数传递给graphql片段?

cmd : react-native run-android 在每次文件更改时

Typescript:onPress 类型

React Native:如何获取文件大小、mime 类型和扩展名?

props验证中缺少 React Navigation 'navigation'

如何在 react-native 中从 AsyncStorage 中删除元素

区别需要MainQueueSetup 和dispatch_get_main_queue?

我可以从react-native元素中删除 tvOS 吗?

React Native 将状态中的数组值应用为 Picker 项

如何在 React Native 中从底部滑入和滑出

React Context:TypeError:render is not a function