javascript - react native 动画延迟不尊重给定值

标签 javascript react-native animation react-animated

我正在尝试从屏幕顶部弹出一条消息,停留一段时间然后返回到屏幕之外。问题是当我尝试添加延迟时,无论我设置的延迟值是什么,它总是延迟大约 5 秒。这是一个例子:

    import React, { Component } from "react";
    import { Animated, View, Text, StyleSheet, Dimensions } from "react-native";

    export default class PopupModal extends Component {
        constructor(props) {
            super(props);

            this.state = {
                message: "Hello!",
                yTranslation: new Animated.Value(0.1)
            };
        }

        render() {
            return (
                <Animated.View style={{ ...styles.container, transform: [{ translateY: this.state.yTranslation }] }}>
                    <View style={styles.innerContainer}>
                        <Text>{this.state.message}</Text>
                    </View>
                </Animated.View>
            );
        }

        componentDidMount() {
            Animated.sequence([
                Animated.timing(this.state.yTranslation, {
                    toValue: 130,
                    duration: 500,
                    useNativeDriver: true
                }),
                Animated.timing(this.state.yTranslation, {
                    toValue: 0,
                    delay: 10, // <-- Here it doesn't matter which value I choose, it always delays for about 5 seconds.
                    duration: 500,
                    useNativeDriver: true
                })
            ]).start();
        }
    }

    const win = Dimensions.get("window");

    const styles = StyleSheet.create({
        container: {
            position: "absolute",
            bottom: win.height,
            left: 60,
            right: 60,
            alignItems: "center",
            justifyContent: "center"
        },
        innerContainer: {
            paddingHorizontal: 10,
            paddingVertical: 5,
            backgroundColor: "white",
            borderColor: "#444",
            borderWidth: 5,
            borderRadius: 10
        }
    });

react 版本:16.8.3

React Native 版本:0.59.9

设备:Pixel 2 (API 28) 安卓模拟器

最佳答案

解决方案:

显然这是一个临时的环境问题。我尝试了这个简单的 javascript 片段:

console.log("before: ", new Date());
setTimeout(() => {
    console.log("after: ", new Date());
}, 10);

哪个输出:

before:  Wed Jul 24 2019 12:37:21 GMT+0200 (centraleuropeisk sommartid)
after:  Wed Jul 24 2019 12:37:27 GMT+0200 (centraleuropeisk sommartid)

所以这 10 毫秒大约用了 6 秒。然后我卸载了该应用程序,重新启动了模拟器(完全重启)并重新安装了该应用程序,然后它就可以正常工作了。我不确定究竟是什么修复了它,但我猜是重新启动模拟器。

关于javascript - react native 动画延迟不尊重给定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57178762/

相关文章:

node.js - 在 selenium-webdriver 和 appium 中通过可访问性标签选择元素的有效方法是什么?

javascript - 在 [Timestamp_1, Timestamp_2] 期间将对象从 Position_1 移动到 Position_2

animation - Raphael 对象上的同步动画 - 最后一个动画覆盖对象的先前动画

javascript - jQuery检查ID值是否有数字

javascript - 从数组中获取相似/最接近的数字

javascript - 无法读取 CKEditor ASP.NET 的 null 属性 'unselectable'

javascript - 为什么我在 Javascript 中收到错误消息对象没有方法?

react-native - 在 react-native 中动态隐藏/显示标题

javascript - 在react-native中创建stackNavigator后导航到页面时出错

c# - 跳过 Aero 窗口动画