问题描述
我正在为我的本科论文创建一个简单的应用程序,我有这些用户可以移动的 PNG 图像。
图像还可以更改 tintColor,效果很好,而且它们以原始颜色开始。当我设置 tintColor 然后再次尝试“重置”它时会出现问题,然后图像就会变得不可见。
Image when it is added Image after changing to a color then to null again
如下图所示,图片还在,只是看不到了。
在样式中设置 tintColor 的代码如下所示:
<Animated.Image
source={source}
resizeMode={'contain'}
style={[
styles.item,tintColor === null ? {} : { tintColor: tintColor },{
transform: [{ scale: imgScale }],},]}
/>
当图像加载并且值为空时,它不会设置任何 tintColor,但是如果我设置了 tintColor 然后返回 null,它只会完全删除其上的任何颜色。我尝试了不同的东西,到目前为止没有任何效果。我希望用户能够“重置”回原始的 PNG 颜色,尤其是对于其中一些已经设置了自己的颜色的用户。
当我添加图像时,图像是从列表中动态加载的,并且是可拖动的。
解决方法
如果您不想重置,请给他们一个密钥={Math.random}。但是如果你想着色,你必须给它们一个静态键。 这将解决您的问题。
编辑:key={this.state.tintIt?Math.random:'a12'}