我使用创建了一个进度条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
正在启动。