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

时间:2020-09-18 10:23:57

标签: react-native react-native-reanimated

我使用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)), stopClock(clock)),

      set(progress, runTiming(clock, quizDurationTiming)),
    ],
    []
  )
}

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

0 个答案:

没有答案
相关问题