我使用react-native-reanimated
和react-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
正在启动。