gpt4 book ai didi

react-native flatlist 在项目删除/向上滑动时设置动画

转载 作者:行者123 更新时间:2023-12-04 04:05:25 31 4
gpt4 key购买 nike

我想在从 FlatList 中删除项目时制作动画。

我有一个自定义卡片组件作为 FlatList 中的项目。我垂直显示它。

现在,我想为元素的移除制作动画。项目可以从任何地方/索引中删除。

移除时的动画是,项目应该隐藏,下面的项目应该慢慢向上滑动。应该是顺畅的,我做过正常的不顺畅。我可以制作不透明度动画,但 translateY 无法按卡上的要求工作。

使用下面的动画隐藏已删除的卡片:

Animated.timing(this.animation, {
toValue: 1,
duration: 600,
// easing: Easing.linear,
delay: this.props.index * 1000,
}).start();

const animatedStyle = {
opacity: this.animation,
// transform: [
// {
// translateY: this.animation.interpolate({
// inputRange: [0, 1],
// outputRange: [0, 300],
// }),
// },
// ],
}

在卡片渲染中()

<Animated.View style={[animatedStyle]}>
......
// mycode
</Animated.View>

无法控制/动画化 FlatList 的重新渲染/滚动/向上滚动行为。

有人可以帮助我吗?

最佳答案

我已经使用以下逻辑实现了。

我在平面列表中的卡片组件/renderItem 上应用了以下动画。

  • 有两个动画1-淡出2-滑动
  • 淡化是通过不透明度
  • 通过Aninated实现的滑动动画,根据卡片高度计时。没有使用 transform-translateY 作为平面列表移动元素比动画更快并且没有获得适当的滑动效果。
//initialize two animated variables
this.animation = new Animated.Value(1);
this.slide = new Animated.Value(1);

const cardAnimationHeight = AppSizes.isTablet ? 194 : 360;
//interpolating height to get slide animation
const animatedStyle = {
opacity: this.animation,
height: this.slide.interpolate({
inputRange: [0, 1],
outputRange: [0, cardAnimationHeight],
}),
};

render() {
return (
<Animated.View style={this.state.isThisCardSelected ? [animatedStyle] : null}>
// rest of card
</Animated.View>
)}

//start animation
startAnimation = () => {
this.setState({ isThisCardSelected: true }, () => {
//setting isThisCardSelected to true to apply the above style which will trigger fade & after fading is done, applying height animation which will give the slide effect.
Animated.timing(this.animation, {
toValue: 0,
timing: 1000,
}).start(() => {
Animated.timing(this.slide, {
toValue: 0,
duration: 500,
}).start(() => {
//do you logic/actions
this.setState({ isThisCardSelected: false }, () => {
this.slide.setValue(1);
});
});
});
});
};

每当您需要淡入淡出+滑动动画时调用startAnimation()

关于react-native flatlist 在项目删除/向上滑动时设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62590573/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com