问题描述
大家好,我遇到了重新动画回调的问题,当在 call () 中调用异步函数时,它会再次重新启动我的动画以开始,它似乎重置了我的状态。某种方式让我的动画保持它所处的状态并在后台运行 test () ?像什么?
函数运行正常,我的权限在 app.js 中。
返回块([ 条件( state.finished,调用([],() => (a = 0)) ),条件( 回来了, 调用([],() => { 一个++,测试(); }) ),
完整代码:
import Animated,{
Clock,Value,eq,cond,useCode,add,block,set,stopClock,startClock,not,clockRunning,call,timing,spring,SpringUtils,and,diffClamp,} from "react-native-reanimated";
import { useEffect,useState,useRef } from "react";
import { State } from "react-native-gesture-handler";
import { Text,View,StyleSheet,TouchableOpacity } from "react-native";
import * as Location from "expo-location";
import * as TaskManager from "expo-task-manager";
const LOCATION_TASK_NAME = "background-location-task";
export const snapProgress = (value,gesture,isBack,point) => {
const [region,setRegion] = useState(null);
const offset = new Value(0);
const clock = new Clock();
const state = {
time: new Value(0),veLocity: new Value(0),position: new Value(0),finished: new Value(0),};
const config = {
tovalue: new Value(0),damping: 26,mass: 1,stiffness: 170,overshootClamping: false,restSpeedThreshold: 0.01,restdisplacementThreshold: 0.01,};
let a = 0;
test = async () => {
if (a == 2) {
console.log("finished 1");
a++;
console.log("getlocationasync");
await Location.startLocationUpdatesAsync(LOCATION_TASK_NAME,{
enableHighAccuracy: true,distanceInterval: 5,});
location = await Location.watchPositionAsync(
{
enableHighAccuracy: true,},(newLocation) => {
let { coords } = newLocation;
let region = {
latitude: coords.latitude,longitude: coords.longitude,latitudeDelta: 0.00155,longitudeDelta: 0.00155,};
setRegion(region);
},(error) => console.log(error)
);
}
};
useEffect(() => {
TaskManager.isTaskRegisteredAsync(LOCATION_TASK_NAME).then((val) => {
if (!val) {
TaskManager.defineTask(LOCATION_TASK_NAME,({ data,error }) => {
if (error) {
console.log(error);
return;
}
if (data) {
console.log(data);
try {
const { locations } = data;
console.log("locations: ",+data);
} catch (e) {
console.log(e);
}
}
});
}
});
},[]);
return block([
cond(
state.finished,call([],() => (a = 0))
),cond(
isBack,() => {
a++,test();
})
),cond(
eq(gesture,State.ACTIVE),[
cond(
clockRunning(clock),[stopClock(clock),set(offset,state.position)],set(state.position,diffClamp(add(offset,value),1))
),],[
cond(not(clockRunning(clock)),[
set(state.time,0),set(state.finished,set(config.tovalue,point),startClock(clock),]),spring(clock,state,config),cond(and(eq(state.finished,1),clockRunning(clock)),[
set(isBack,stopClock(clock),]
),state.position,]);
};
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)