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

标签 react-native react-native-flatlist react-native-animatable

我想在从 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/

相关文章:

javascript - React-native 如何设置 PickerItems,如文本颜色、字体大小等,

React-Native 重复组件名称

react-native - 是否有可在 Android 和 IOS 上使用的 native recyclerview (react-native)

reactjs - 有条件的动画组件很卡顿

reactjs - react-native 使状态栏 'barStyle' 可动画化

animation - 在 React Native 中使用用户触摸旋转光盘

javascript - react native : I getting raw data in display page

android - 根据设备访问屏幕高度

reactjs - react native : FlatList index says is undefined

javascript - 如何使用map函数正确显示多个Flatlist?