微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

应用程序进入后台状态时如何暂停时钟

如何解决应用程序进入后台状态时如何暂停时钟

我使用react-native-reanimatedreact-native-redash创建了一个进度条,该进度条运行良好,但是现在我想在应用程序进入后台状态以及应用程序进入后台状态时暂停进度或计时处于活动状态时,应使用最后一个位置和时间来恢复。

clock config


const runTiming = (clock,quizDurationTiming) => {
  const state = {
    finished: new Value(0),position: new Value(0),frameTime: new Value(0),time: new Value(0),}
  const config = {
    tovalue: new Value(1),duration: quizDurationTiming * 10,easing: Easing.in(Easing.ease),}

  return block([
    cond(
      not(clockRunning(clock)),set(state.time,0),timing(clock,state,config)
    ),cond(eq(state.finished,1),stopClock(clock)),state.position,])
}

const SpecialTestTimer = ({
  preparationTime,appState,quizDurationTiming,}) => {
  const [isCompleted,setIsCompleted] = useState(false)
  const clock = useClock()
  const progress = useValue(0)
  const [play,setPlay] = useState(true)
  const isProgress = useValue(0)

  useEffect(() => {
    if (appState == "active") setPlay(true)
    else setPlay(false)
  },[appState])

  useCode(() => set(isProgress,play ? 1 : 0),[play])

  useCode(
    () => [
      cond(and(progress,not(clockRunning(clock))),startClock(clock)),cond(and(not(progress),clockRunning(clock)),set(progress,runTiming(clock,quizDurationTiming)),],[]
  )
}

我写了这段代码,但是不能正常工作。当我进入屏幕clock尚未启动,但是当我进入background状态后进入应用程序时,则clock正在启动。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。