React Native FlatList动画:在删除/向上滑动项目时进行动画

3
我希望在FlatList中删除item时实现动画效果。 我有一个自定义的card组件作为FlatList中的item,并以垂直方式显示它。 现在,我想对item的删除进行动画处理。item可以从任何位置/索引中删除。 删除时的动画效果是,item应该隐藏,下面的items应该缓慢向上滑动。这应该是平稳的,我已经完成了正常的操作,但不够平稳。我能够对透明度进行动画处理,但translateY在卡片上工作不如预期。 使用以下动画隐藏删除的card:
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],
    //         }),
    //     },
    // ],
}

在card render()函数中

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

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

有人可以帮帮我吗?

1个回答

1
我使用以下逻辑实现了功能。
我在我的卡片组件/flatlist中的renderItem上应用了以下动画。
  • 有两种动画 1- 淡入淡出 2- 滑动
  • 淡入淡出是通过不透明度实现的
  • 滑动动画是通过Aninated和timing在卡片高度上实现的。没有使用transform-translateY,因为flatlist移动元素比动画更快,无法获得正确的滑动效果。
//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() 函数。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接