在 React Native 中对对象数组进行动画处理
Animate an array of objects in React Native
在 React 中,动画似乎总是绑定到 this.state
中的属性。但是,如果我在一个视图中有多个对象需要动画化,该怎么办?例如,如果我有一个包含多个图像的列表视图,并且我想在加载到列表视图时对这些图像的不透明度进行动画处理,该怎么办?
render() {
//var _scrollView: ScrollView;
return (
<View style={styles.container}>
<ScrollView
style={styles.scrollView}>
<ListView
initialListSize={1}
dataSource={this.state.dataSource}
renderRow={this.renderRow.bind(this)}
style={styles.postList}
/>
</ScrollView>
</View>
);
}
renderRow(post) {
//var postItemHeight = windowSize / 2
return (
<View style={styles.postItem}>
<Image
style={styles.postImage}
source={{uri: post.cover_image}}
onLoad={(e) => this.imageLoaded()}>
</Image>
</View>
);
}
imageLoaded() {
// now animate the image opacity
// for every image that is loaded into the listview
}
没有理由Animated
值需要存在于组件状态中 - 这就是示例显示如何做到这一点的方式。如果需要,您可以将Animated
值数组保留在该状态中,将它们放入 Flux 存储中,或者随心所欲地执行此操作。
但是,在您的特定情况下,最简单的解决方案是创建一个表示ListView
的单个图像行的组件。然后,您可以使用该组件的单个状态来管理其动画。
例如:
const FadeImage = React.createClass({
displayName: 'FadeImage',
propTypes: Image.propTypes,
getInitialState() {
return {
opacity: new Animated.Value(0)
};
},
setNativeProps(nativeProps) {
this._image.setNativeProps(nativeProps);
},
fadeIn() {
Animated.spring(this.state.opacity, {
toValue: 1,
friction: 10,
tension: 60
}).start();
},
render() {
return (
<Animated.View style={{opacity: this.state.opacity}}>
<Image {...this.props} onLoad={this.fadeIn} ref={component => this._image = component} />
</Animated.View>
);
}
});
它是Image
的直接替代品,因此您可以像使用常规图像组件一样使用它。
相关文章:
- 使用不同的超时对数组中的每个项目进行角度动画处理
- 图表.js 2,从右到左(不是自上而下)进行动画处理
- 使用 JavaScript 对 HTML5 进度条进行动画处理
- 使用 JQuery 对 Spin SVG 元素进行动画处理
- 使用 GSAP 对模糊滤镜进行动画处理
- 使用 jQuery 对窗口进行动画处理
- 如何使用jQuery对固定元素的位置进行动画处理
- jQuery 动画处理最大宽度溢出问题
- 悬停不起作用时对不透明度更改进行动画处理
- 在两个值之间插值或“补间”(但不进行动画处理)
- 尝试在画布中对形状进行动画处理时,它会显示,但不移动
- 如何在不分隔跨度字母的情况下对文本进行动画处理
- 对链接的 svg 进行动画处理不起作用;内联时工作正常
- 防止对某些事件进行进一步的动画处理
- Snap.svg:在动画中使用相同的种类对多个零件进行动画处理
- 如何使用if语句jquery对数字进行动画处理
- 按随机顺序对元素进行动画处理
- 元素在使用 jQuery 对相对包装宽度进行动画处理时消失
- brush.event 在对画笔事件进行动画处理时会做什么
- 使用ScrollMagic按顺序对多个场景进行动画处理