比较精细的动画可以用Animated来控制。但是,在一些简单的界面切换、更新的时候所做的动画里再去计算开始值、结束值和插值器如何运作绝对是浪费时间。

RN正好给我们提供了LayoutAnimation来解决这个问题。按照官方的说法:LayoutAnimation就是用于在下一个绘制或者布局周期(render/layout cycle)里处理界面中全部视图的动画的。

下面看一个例子:

  1. export default class DemoLayoutAnimation extends React.Component {
  2. constructor(props) {
  3. super(props);
  4. this.state = {
  5. width: 100,
  6. height: 100,
  7. };
  8. this._onPress = this._onPress.bind(this);
  9. }
  10. componentWillMount() {
  11. LayoutAnimation.spring();
  12. }
  13. _onPress() {
  14. LayoutAnimation.spring();
  15. this.setState({width: this.state.width + 20, height: this.state.height + 20});
  16. }
  17. render() {
  18. return (
  19. <View style={styles.container}>
  20. <View style={[styles.box, {width: this.state.width, height: this.state.height}]} />
  21. <TouchableOpacity onPress={this._onPress}>
  22. <View style={styles.button}>
  23. <Text style={styles.buttonText}>Press me!</Text>
  24. </View>
  25. </TouchableOpacity>
  26. </View>
  27. );
  28. }
  29. };



效果就是这样的。

使用的时候也非常简单,只需要在更新State之前调用一下LayoutAnimation.sprint()这么一行代码。

LayoutAnimation默认的提供了三种动画:linear , springeaseInEaseOut。 当然,RN也留出了自定义的接口。你可以按照自己需要的自定义动画效果。

下面看看如何自定义:

  1. import //...略...
  2. const customAnim = {
  3. customSpring: {
  4. duration: 400,
  5. create: {
  6. type: LayoutAnimation.Types.spring,
  7. property: LayoutAnimation.Properties.scaleXY,
  8. springDamping: 0.6
  9. },
  10. update: {
  11. type: LayoutAnimation.Types.spring,
  12. springDamping: 0.6
  13. }
  14. },
  15. customLinear: {
  16. duration: 200,
  17. create: {
  18. type: LayoutAnimation.Types.linear,
  19. property: LayoutAnimation.Properties.opacity,
  20. },
  21. update: {
  22. type: LayoutAnimation.Types.easeInEaseOut
  23. }
  24. }
  25. };
  26. export default class DemoLayoutAnimation extends React.Component {
  27. componentWillUpdate() {
  28. LayoutAnimation.configureNext(customAnim.customLinear);
  29. }
  30. _onPress() {
  31. // LayoutAnimation.spring();
  32. this.setState({ width: this.state.width + 20, height: this.state.height + 20 });
  33. }
  34. //...略...
  35. };

为了直入主题,部分内容省略。后面有完整的代码。

自定义非常简单,当然限制也不少。只需要指定动画的durationcreateupdate

另外一个本例与上例不同的地方在于LayoutAnimation可以只在componentWillUpdate()方法里指定,不需要在点击事件里指定。

完整代码

  1. //@flow
  2. import React from 'react';
  3. import {
  4. View,
  5. Text,
  6. TouchableOpacity,
  7. LayoutAnimation,
  8. StyleSheet,
  9. } from 'react-native';
  10. const customAnim = {
  11. customSpring: {
  12. duration: 400,
  13. create: {
  14. type: LayoutAnimation.Types.spring,
  15. property: LayoutAnimation.Properties.scaleXY,
  16. springDamping: 0.6
  17. },
  18. update: {
  19. type: LayoutAnimation.Types.spring,
  20. springDamping: 0.6
  21. }
  22. },
  23. customLinear: {
  24. duration: 200,
  25. create: {
  26. type: LayoutAnimation.Types.linear,
  27. property: LayoutAnimation.Properties.opacity,
  28. },
  29. update: {
  30. type: LayoutAnimation.Types.easeInEaseOut
  31. }
  32. }
  33. };
  34. export default class DemoLayoutAnimation extends React.Component {
  35. constructor(props) {
  36. super(props);
  37. this.state = {
  38. width: 100,
  39. height: 100,
  40. };
  41. this._onPress = this._onPress.bind(this);
  42. this._createAnimation = this._createAnimation.bind(this);
  43. }
  44. // componentWillMount() {
  45. // LayoutAnimation.spring();
  46. // }
  47. componentWillUpdate() {
  48. LayoutAnimation.configureNext(customAnim.customLinear);
  49. }
  50. _onPress() {
  51. // LayoutAnimation.spring();
  52. this.setState({ width: this.state.width + 20, height: this.state.height + 20 });
  53. }
  54. render() {
  55. return (
  56. <View style={styles.container}>
  57. <View style={[styles.box, { width: this.state.width, height: this.state.height }]} />
  58. <TouchableOpacity onPress={this._onPress}>
  59. <View style={styles.button}>
  60. <Text style={styles.buttonText}>Press me!</Text>
  61. </View>
  62. </TouchableOpacity>
  63. </View>
  64. );
  65. }
  66. };
  67. const styles = StyleSheet.create({
  68. container: {
  69. flex: 1,
  70. alignItems: 'center',
  71. justifyContent: 'center'
  72. },
  73. box: {
  74. backgroundColor: 'red'
  75. },
  76. button: {
  77. marginTop: 10,
  78. paddingVertical: 10,
  79. paddingHorizontal: 20,
  80. backgroundColor: 'black'
  81. },
  82. buttonText: {
  83. color: 'white',
  84. fontSize: 16,
  85. fontWeight: 'bold'
  86. }
  87. });

React Native填坑之旅--LayoutAnimation篇的更多相关文章

  1. React Native填坑之旅--Flow篇(番外)

    flow不是React Native必会的技能,但是作为正式的产品开发优势很有必要掌握的技能之一.所以,算是RN填坑之旅系列的番外篇. Flow是一个静态的检查类型检查工具,设计之初的目的就是为了可以 ...

  2. React Native填坑之旅--布局篇

    代码在这里: https://github.com/future-challenger/petshop/tree/master/client/petshop/src/controller 回头看看RN ...

  3. React Native填坑之旅--Navigation篇

    React Native的导航有两种,一种是iOS和Android通用的叫做Navigator,一种是支持iOS的叫做NavigatorIOS.我们这里只讨论通用的Navigator.会了Naviga ...

  4. React Native填坑之旅--ListView篇

    列表显示数据,基本什么应用都是必须.今天就来从浅到深的看看React Native的ListView怎么使用.笔者写作的时候RN版本是0.34. 最简单的 //@flow import React f ...

  5. React Native填坑之旅--Button篇

    从React过来,发现React Native(以下简称RN)居然没有Button.隔壁的iOS是有UIButton的,隔壁的隔壁的Android里也是有的.没有Button,就没有点击效果啊.这还真 ...

  6. React Native填坑之旅--与Native通信之iOS篇

    终于开始新一篇的填坑之旅了.RN厉害的一个地方就是RN可以和Native组件通信.这个Native组件包括native的库和自定义视图,我们今天主要设计的内容是native库方面的只是.自定义视图的使 ...

  7. React Native填坑之旅--组件生命周期

    这次我们来填React Native生命周期的坑.这一点非常重要,需要有一个清晰的认识.如果你了解Android或者iOS的话,你会非常熟悉我们今天要说的的内容. 基本上一个React Native的 ...

  8. React Native填坑之旅--重新认识RN

    如同黑夜里的一道光一样,就这么知道了F8. F8是每年一次Facebook每年一次的开发者大会.每次大会都会release相应的APP,iOS.Android都有.之前都是用Native开发的,但是2 ...

  9. React Native填坑之旅--动画

    动画是提高用户体验不可缺少的一个元素.恰如其分的动画可以让用户更明确的感知当前的操作是什么. 无疑在使用React Native开发应用的时候也需要动画.这就需要知道RN都给我们提供了那些动画,和每个 ...

随机推荐

  1. Apache配置简单http认证

    首先要说明的是这种认证是不安全的,密码是明文传输,因此很容易被各种嗅探软件嗅探到密码,只能用于简单的认证.今天上午把ownCloud卸载了,这玩 意儿中看不中用啊,原来10M的访问速度被限制成了几百K ...

  2. EXT学习之——EXT下拉框默认绑定第一个值

    //默认第一个下拉框绑定值if (this.moduleCombo.store.getAt(0) != undefined) { this.moduleCombo.setValue(this.modu ...

  3. Python图像处理库:Pillow 初级教程

    Python图像处理库:Pillow 初级教程 2014-09-14 翻译 http://pillow.readthedocs.org/en/latest/handbook/tutorial.html ...

  4. linux网络完全与防护

    7.1 网络封包联机进入主机的流程   7.1.1 封包进入主机的流程 1.经过防火墙的分析 iptables 主要功能是封包过滤 主要分析TCP/IP的封包表头来进行过滤的机制 分析的是OSI的第二 ...

  5. 【PowerOJ1740&网络流24题 圆桌聚餐】(最大流)

    题意: 来自n个不同国家的代表开会,每个国家代表数为ci 会场有m张圆桌,每张桌子可容纳mi人 不希望有同一个国家的代表在同一张桌子上就餐 设计一个合法方案 (n,m<=300) 思路:最大流, ...

  6. libcurl多线程超时设置不安全(转)

    from http://www.cnblogs.com/kex1n/p/4135263.html (1), 超时(timeout) libcurl 是 一个很不错的库,支持http,ftp等很多的协议 ...

  7. VS调试在Win7(vista系列)操作系统下 HttpListener拒绝访问解决办法

    一. VS调试在Win7(vista系列)操作系统下 HttpListener无法绑定多个 指定IP.端口问题 来自:http://www.cnblogs.com/ryhan/p/4195693.ht ...

  8. MySQL中如何插入反斜杠,反斜杠被吃掉,反斜杠转义

    问题描述:mysql中带有反斜杠的内容入库后,发现反斜杠无故失踪了(俗话说被吃掉了) 例:插入insert into tb('url') values('absc\eeee'); 结果数据库里的内容是 ...

  9. 技术英文单词贴--G

    G generator 发电机,发生器,生产者

  10. Linux命令(1)- grep

    1.grep 功能:查找文件里符合条件的字符串. 语法:grep [-abcEFGhHilLnqrsvVwxy][-A<显示列数>][-B<显示列数>][-C<显示列数& ...