React Native填坑之旅--LayoutAnimation篇
来源:互联网 发布:顾客旅程 绘图软件 编辑:程序博客网 时间:2024/05/21 14:16
比较精细的动画可以用Animated
来控制。但是,在一些简单的界面切换、更新的时候所做的动画里再去计算开始值、结束值和插值器如何运作绝对是浪费时间。
RN正好给我们提供了LayoutAnimation
来解决这个问题。按照官方的说法:LayoutAnimation
就是用于在下一个绘制或者布局周期(render/layout cycle)里处理界面中全部视图的动画的。
下面看一个例子:
export default class DemoLayoutAnimation extends React.Component { constructor(props) { super(props); this.state = { width: 100, height: 100, }; this._onPress = this._onPress.bind(this); } componentWillMount() { LayoutAnimation.spring(); } _onPress() { LayoutAnimation.spring(); this.setState({width: this.state.width + 20, height: this.state.height + 20}); } render() { return ( <View style={styles.container}> <View style={[styles.box, {width: this.state.width, height: this.state.height}]} /> <TouchableOpacity onPress={this._onPress}> <View style={styles.button}> <Text style={styles.buttonText}>Press me!</Text> </View> </TouchableOpacity> </View> ); }};
效果就是这样的。
使用的时候也非常简单,只需要在更新State
之前调用一下LayoutAnimation.sprint()
这么一行代码。
LayoutAnimation
默认的提供了三种动画:linear
, spring
和easeInEaseOut
。 当然,RN也留出了自定义的接口。你可以按照自己需要的自定义动画效果。
下面看看如何自定义:
import //...略...const customAnim = { customSpring: { duration: 400, create: { type: LayoutAnimation.Types.spring, property: LayoutAnimation.Properties.scaleXY, springDamping: 0.6 }, update: { type: LayoutAnimation.Types.spring, springDamping: 0.6 } }, customLinear: { duration: 200, create: { type: LayoutAnimation.Types.linear, property: LayoutAnimation.Properties.opacity, }, update: { type: LayoutAnimation.Types.easeInEaseOut } }};export default class DemoLayoutAnimation extends React.Component { componentWillUpdate() { LayoutAnimation.configureNext(customAnim.customLinear); } _onPress() { // LayoutAnimation.spring(); this.setState({ width: this.state.width + 20, height: this.state.height + 20 }); } //...略...};
为了直入主题,部分内容省略。后面有完整的代码。
自定义非常简单,当然限制也不少。只需要指定动画的duration
、create
和update
。
另外一个本例与上例不同的地方在于LayoutAnimation可以只在componentWillUpdate()
方法里指定,不需要在点击事件里指定。
完整代码
//@flowimport React from 'react';import { View, Text, TouchableOpacity, LayoutAnimation, StyleSheet,} from 'react-native';const customAnim = { customSpring: { duration: 400, create: { type: LayoutAnimation.Types.spring, property: LayoutAnimation.Properties.scaleXY, springDamping: 0.6 }, update: { type: LayoutAnimation.Types.spring, springDamping: 0.6 } }, customLinear: { duration: 200, create: { type: LayoutAnimation.Types.linear, property: LayoutAnimation.Properties.opacity, }, update: { type: LayoutAnimation.Types.easeInEaseOut } }};export default class DemoLayoutAnimation extends React.Component { constructor(props) { super(props); this.state = { width: 100, height: 100, }; this._onPress = this._onPress.bind(this); this._createAnimation = this._createAnimation.bind(this); } // componentWillMount() { // LayoutAnimation.spring(); // } componentWillUpdate() { LayoutAnimation.configureNext(customAnim.customLinear); } _onPress() { // LayoutAnimation.spring(); this.setState({ width: this.state.width + 20, height: this.state.height + 20 }); } render() { return ( <View style={styles.container}> <View style={[styles.box, { width: this.state.width, height: this.state.height }]} /> <TouchableOpacity onPress={this._onPress}> <View style={styles.button}> <Text style={styles.buttonText}>Press me!</Text> </View> </TouchableOpacity> </View> ); }};const styles = StyleSheet.create({ container: { flex: 1, alignItems: 'center', justifyContent: 'center' }, box: { backgroundColor: 'red' }, button: { marginTop: 10, paddingVertical: 10, paddingHorizontal: 20, backgroundColor: 'black' }, buttonText: { color: 'white', fontSize: 16, fontWeight: 'bold' }});
0 0
- React Native填坑之旅--LayoutAnimation篇
- react native 填坑之旅
- react-native 填坑之旅
- React Native填坑之旅--Button篇
- React Native填坑之旅--动画篇
- React Native填坑之旅--HTTP请求篇
- React Native填坑之旅--ListView篇
- React Native填坑之旅--Flow篇(番外)
- React Native填坑之旅--Navigation篇
- React Native填坑之旅--布局篇
- React Native填坑之旅--与Native通信之iOS篇
- React Native填坑之旅--class(番外篇)
- React Native填坑之旅--ListView的Section Header
- React Native填坑之旅--组件生命周期
- React Native填坑之旅--与Android模块通信
- react-native 填坑之旅 数据库存储 realm
- [React Native]动画-LayoutAnimation
- React Native LayoutAnimation动画
- 组件改变生活_揭开Vue组件的神秘面纱
- MVC/MVP/MVVM
- Python写入excel提示“excel中有不可读的内容”
- html5 canvas学习--缩放图形
- 对比微信小程序和Android开发
- React Native填坑之旅--LayoutAnimation篇
- html5 canvas学习--图形的组合与裁切
- QT---QT中的布局
- JQuery对象
- 字体式样的设置(Typeface)与对Canvas的使用
- 大数据项目开发步骤
- C++ string函数 与 C字符串处理函数(整理)
- Faster R-CNN的安装、测试经历
- 2016/10/7