比较精细的动画可以用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 , springeaseInEaseOut。 当然,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 });
} //...略...
};

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

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

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

完整代码

//@flow

import 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'
}
});

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. SAMEORIGIN

    http://www.css88.com/archives/5141 Response.AppendHeader("X-Frame-Options", "SAMEORIG ...

  2. .Net的Excel 导出 格式设置

    添加引用:Microsoft   Excel   11.0   Object   Library ; 添加:using Microsoft.Office.Interop.Excel; 一.打开Exce ...

  3. http的header参数有关

    1.读文件 a.如果经过php处理:变成mime:text/html,在浏览器可以打开.否则是下载 b.phpui如果加上参数resid=01,那么返回的数据的Content-Type:applica ...

  4. [原创] 更新Ubuntu自带的python2.X版本 ImportError: No module named pip;ImportError: No module named _sqlite3

    Ubuntu14.04自带的Python2版本,是2.7.6的,想更新为最新的2.7.11,操作如下: 1. 从python官网下载2.7.11的source源码包 Python-2.7.11.tgz ...

  5. java对话框形式实现加减乘除

    import javax.swing.JOptionPane; // import class JOptionPane public class sumit{ public static void m ...

  6. WebForm---增删改(内置对象)

    一.添加 前台代码: <body> <form id="form1" runat="server"> <h1>用户添加< ...

  7. UVM的factory机制

    在UVM中使用工厂模式基本上分为三个步骤: 1. 注册 当定义一个类的时候,它的类型必须要注册,UVM已经提供了专用的宏. `uvm_component_utils(class_type_name) ...

  8. 错误:创建 cachingConfiguration 的配置节处理程序时出错: 未能加载文件或程序集“Microsoft.Practices.EnterpriseLibrary.Caching,

    问题: 错误:创建 cachingConfiguration 的配置节处理程序时出错: 未能加载文件或程序集“Microsoft.Practices.EnterpriseLibrary.Caching ...

  9. 解决IE上登陆oracle OEM时报:“证书错误,导航已阻止”的错误

    今天在IE上登陆OEM时,报证书错误,导航已阻止,我选择:继续浏览此网站(不推荐),但是点了之后还没有反应,在网上搜了很多,原因基本都是windows的问题,最后发现问题是:oracle oem证书的 ...

  10. AngularJS学习---REST和自定义服务(REST and Custom Services) ngResource step 11

    1.切换目录 git checkout step- npm start 2.效果图 效果图和step 10的没有什么差别,这里主要的改动都是代码,代码做了很多优化,这里效果图就不再贴出来了. 3.实现 ...