Реанимированный использует декларативный api, а не императивный api по умолчанию Animated
api т.е. отметка времени для каждого кадра.
с помощью этих часов вы можете определить вспомогательные функции, такие как
function runTiming(clock, value, dest) {
const state = {
finished: new Value(0),
position: new Value(0),
time: new Value(0),
frameTime: new Value(0),
};
const config = {
duration: 5000,
toValue: new Value(0),
easing: Easing.inOut(Easing.ease),
};
return block([
cond(
clockRunning(clock),
[
// if the clock is already running we update the toValue, in case a new dest has been passed in
set(config.toValue, dest),
],
[
// if the clock isn't running we reset all the animation params and start the clock
set(state.finished, 0),
set(state.time, 0),
set(state.position, value),
set(state.frameTime, 0),
set(config.toValue, dest),
startClock(clock),
]
),
// we run the step here that is going to update position
timing(clock, state, config),
// if the animation is over we stop the clock
cond(state.finished, debug('stop clock', stopClock(clock))),
// we made the block return the updated position
state.position,
]);
}
, а в компоненте существует несколько способов анимации значения.
я рекомендовал использовать Code или useCode из реанимированного
, вы можете использовать его как
function OpacityButton ({}){
const opacity = new Value(1)
const clock = new Clock()
const clicked = new Value(0)
const buttonState = new Value()
useCode(() => block([cond(eq(clicked,1),[
set(opacity, runTiming(clock, opacity, 0.5)
])]))
return (
<TouchableOpacity onPress={() => clicked.setValue(1)} >
<Animated.View style={[styles.button,{opacity}]} />
</TouchableOpacity>
)
}
это всего лишь 70% кода. Но вы можете расширить его по своему требованию.