javascript - 如何在array.map上仅对按下的索引进行动画处理
问题描述
我有一个 Views 列表,每个视图都包含删除按钮。
我只想在按下的视图上开始缩放动画。
现在,使用下一个代码,当我按下删除按钮时,此列表中的所有视图都是动画的。
这是我的功能组件:
export const SubTasksView = ({subTasks, onAddSubTask, subTaskValue, setSubTaskValue, onPressDeleteSubTask}) => {
const removeAnim = new Animated.Value(1);
const startRemoveAnimation = () => {
Animated.timing(
removeAnim,
{
toValue: 0,
duration: 300,
useNativeDriver: true
}
).start();
};
const onPressDeleteSubTaskHandler = index => {
startRemoveAnimation();
setTimeout(() => {
onPressDeleteSubTask(index);
}, 300);
};
return (
<View style={styles.mainContainer}>
<View style={styles.textInputContainerStyle}>
<TextInput
style={styles.textInputStyle}
placeholder={strings.PLACEHOLDER_SUB_TASK}
value={subTaskValue}
onChangeText={setSubTaskValue}
/>
<TouchableOpacity style={styles.addButtonStyle} onPress={onAddSubTask}>
<Ionicons name={icons.ICON_ADD} size={35} color={color.ORANGE}/>
</TouchableOpacity>
</View>
{subTasks.map((subTask, index) => {
return (
<Animated.View key={subTask + 'd' + index}
style={[styles.subTasksContainer,
{
transform: [
{scale: removeAnim}
]
}
]}>
<Text style={styles.subTaskText}>{subTask}</Text>
<TouchableOpacity onPress={() => {
onPressDeleteSubTaskHandler(index)
}}>
<Ionicons name={icons.ICON_TRASH} size={20} color={color.DARK_GREY}/>
</TouchableOpacity>
</Animated.View>
);
})}
</View>
);
};
解决方案
Ok I fixed it within next way:
Just created new component and rendered it with FlatList
export const SubTaskItem = ({subTask, renderedIndex, onPressDeleteButton}) => {
const removeAnim = new Animated.Value(1);
const onDeletePressHandler = () => {
Animated.timing(
removeAnim,
{
toValue: 0,
duration: 100,
useNativeDriver: true
}
).start(() => {
onPressDeleteButton(renderedIndex)
});
};
return(
<Animated.View style={[styles.subTasksContainer,
{
transform: [
{scale: removeAnim}
]
}]}>
<Text style={styles.subTaskText}>{subTask}</Text>
<TouchableOpacity onPress={() => onDeletePressHandler()} >
<Ionicons name={icons.ICON_TRASH} size={20} color={color.DARK_GREY}/>
</TouchableOpacity>
</Animated.View>
)
};
推荐阅读
- r - 在 dplyr mutate 工作流程中引用和索引其他数据帧
- php - Laravel Ajax 请求,没有“Access-Control-Allow-Origin”标头
- javascript - 如何在 keydown 上启动计时器并在 keyup 上停止?
- memory-management - 如何在不初始化的情况下在 Windows 中分配 RAM 页面?
- reactjs - 组件之间的 React 过渡组动画(淡入组件代替另一个)
- android - 模拟器 Android 15 - 黑屏
- amazon-web-services - 您可以使用基于 AWS Route 53 延迟的路由来解析 AWS 之外的服务器吗?
- r - 如何检查 r 中数据框中的列的值是否对应于数据框每一行的另一列的单次出现?
- docker - windows/amd64 没有匹配的清单
- ruby-on-rails - 尝试诊断 ActiveRecord::ConcurrentMigrationError 时缺少 pg_locks 和 pg_stat_activity 表