react-native - 如何为平面列表中的单个项目设置动画?
问题描述
我如何为 flatlist 中的单个项目设置动画?
在 flatlist 上实现了 onpress 缩放动画,但 onpress 动画应用于 flatlist 中的所有项目。我只想将动画应用于一项。
动画代码在这里
const scaleInAnimated = useRef(new Animated.Value(0)).current;
const SCALE = {
getScaleTransformationStyle(animated: Animated.Value, startSize: number = 1, endSize: number = 0.95) {
const interpolation = animated.interpolate({
inputRange: [0, 1],
outputRange: [startSize, endSize],
});
return {
transform: [
{ scale: interpolation },
],
};
},
pressInAnimation(animated: Animated.Value, duration: number = 100) {
animated.setValue(0);
Animated.timing(animated, {
toValue: 1,
duration,
useNativeDriver: true,
}).start();
},
pressOutAnimation(animated: Animated.Value, duration: number = 500) {
animated.setValue(1);
Animated.spring(animated, {
toValue: 0,
duration,
useNativeDriver: true,
}).start();
},
};
平面列表代码
<FlatList
onEndReachedThreshold={1}
onEndReached={_handleLoadMore}
data={trending}
renderItem={({ item,index }) =>(<TouchableOpacity activeOpacity={0.9} onPress={()=>{navigation.navigate('DetailMovie',{id:item.id})}}
onPressIn={() => {SCALE.pressInAnimation(scaleInAnimated);}}
onPressOut={() => {SCALE.pressOutAnimation(scaleInAnimated);}}
style={SCALE.getScaleTransformationStyle(scaleInAnimated)}
>
<Animated.View>
<ImageBackground
source={{uri:`${imageurl}${item.poster_path}`}}
style={[styles.cardimage]}
imageStyle={styles.cardItemImage}>
<LinearGradient colors={['transparent','rgba(0,0,0,0.8)']} style={styles.background}>
<Text style={styles.cardTitle} numberOfLines={1}>{item.title}{item.name} </Text>
<Text style={styles.cardMeta}><MaterialCommunityIcons name='star' size={14} color={colors.yellow}/>{item.vote_average}</Text>
</LinearGradient>
</ImageBackground>
</Animated.View>
</TouchableOpacity>)
}
keyExtractor={(item) =>item.id.toString()}
horizontal
showsHorizontalScrollIndicator={false}
ListHeaderComponent={Blank}
ListHeaderComponentStyle={styles.blank}
/>
怎么修 ?谢谢
解决方案
推荐阅读
- r - 悬停时将趋势线数据添加到 Shiny Plotly
- symfony - Symfony 4 - 多断言困难
- jsf - Jboss EAP 7.2 下 FacesContextHelper 中的 NPE
- laravel - 如何能够编辑和删除卡片内的内容?Laravel - vue.js
- sql - 删除关系表中的重复行
- django - Python django 'NoneType' 对象没有属性 'indicador_set'
- php - Vue Axios 不保存在 Laravel 控制器中
- unity3d - 将 Unity 2d 动画导出为 spritesheet
- json - 从使用 python 读取 JSON 文件中选择随机问候语
- command-line - 使用命令行向其他团队用户发送文件