简体   繁体   English

如何让启动画面保持更长时间? (世博会cli)

[英]How to keep splash screen longer on? (expo cli)

I've read here several questions, but I did not find a response to my question: basically I need to keep splash screen longer on.我在这里阅读了几个问题,但我没有找到对我的问题的回应:基本上我需要让启动画面保持更长时间。

In one of the articles I've seen that there was said to use the AppLoading element, until all the resources are loaded.在我看到的一篇文章中,据说使用 AppLoading 元素,直到所有资源都被加载。 This is fine by me, so I've created a test, with a timer to see if it really works, but it doesnt.这对我来说很好,所以我创建了一个测试,带有一个计时器来查看它是否真的有效,但它没有。

export default class LoadingScreen extends React.Component {
  state = {
    isReady: false,
  };

  render() {
    if (!this.state.isReady) {
      return (
        <AppLoading 
          startAsync={() => this.requestAsync()}
          onFinish={() => this.setState({ isReady: true })}
          onError={console.warn}
        />
      ); }
    else {
      return (
        <StartScreen />
      );}
  }

  requestAsync = async() => {
    await Promise.resolve(setTimeout(() => (console.log("done!")), 10000));
  }
}

I have a loading screen class, which is called in:我有一个加载屏幕 class,它被调用:

export default function App() {

  console.log('Application started');
  
  return (
     <LoadingScreen />
  );
}

So my idea to have this loading screen or a splash screen on for a time that the resources are loading, in this case i just set a timer to 10 sec.所以我的想法是在加载资源的时候打开这个加载屏幕或启动屏幕,在这种情况下,我只需将计时器设置为 10 秒。 But anyways the splash screen goes very fast away... and yeah after 10 second a do get a console.log 'done', but I'm already on the next screen...但无论如何,启动画面很快就消失了……是的,10 秒后确实得到了一个 console.log '完成',但我已经在下一个屏幕上……

Any idea, what am I doing wrong in this case?任何想法,在这种情况下我做错了什么?

Or maybe I get the idea of AppLoading not correct?或者,也许我认为 AppLoading 不正确?

Thanks in advance.提前致谢。

As referred in the documentation you can use Expo's SplashScreen library to keep and hide the splash screen.文档中所述,您可以使用 Expo 的SplashScreen库来保留和隐藏启动屏幕。

import { useEffect } from 'react'
import { Text, SafeAreaView } from 'react-native'
import * as SplashScreen from 'expo-splash-screen'

export default () => {
  useEffect(() => {
    const prepare = async () => {
      // keep splash screen visible
      await SplashScreen.preventAutoHideAsync()

      // pre-load your stuff
      await new Promise(resolve => setTimeout(resolve, 3000))

      // hide splash screen
      await SplashScreen.hideAsync()
    }
    prepare()
  }, [])

  return (
    <SafeAreaView>
      <Text>app is ready</Text>
    </SafeAreaView>
  )
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM