React Native填坑之旅--LayoutAnimation篇
比较精细的动画可以用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()
方法里指定,不需要在点击事件里指定。
完整代码
//@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篇的更多相关文章
- React Native填坑之旅--Flow篇(番外)
flow不是React Native必会的技能,但是作为正式的产品开发优势很有必要掌握的技能之一.所以,算是RN填坑之旅系列的番外篇. Flow是一个静态的检查类型检查工具,设计之初的目的就是为了可以 ...
- React Native填坑之旅--布局篇
代码在这里: https://github.com/future-challenger/petshop/tree/master/client/petshop/src/controller 回头看看RN ...
- React Native填坑之旅--Navigation篇
React Native的导航有两种,一种是iOS和Android通用的叫做Navigator,一种是支持iOS的叫做NavigatorIOS.我们这里只讨论通用的Navigator.会了Naviga ...
- React Native填坑之旅--ListView篇
列表显示数据,基本什么应用都是必须.今天就来从浅到深的看看React Native的ListView怎么使用.笔者写作的时候RN版本是0.34. 最简单的 //@flow import React f ...
- React Native填坑之旅--Button篇
从React过来,发现React Native(以下简称RN)居然没有Button.隔壁的iOS是有UIButton的,隔壁的隔壁的Android里也是有的.没有Button,就没有点击效果啊.这还真 ...
- React Native填坑之旅--与Native通信之iOS篇
终于开始新一篇的填坑之旅了.RN厉害的一个地方就是RN可以和Native组件通信.这个Native组件包括native的库和自定义视图,我们今天主要设计的内容是native库方面的只是.自定义视图的使 ...
- React Native填坑之旅--组件生命周期
这次我们来填React Native生命周期的坑.这一点非常重要,需要有一个清晰的认识.如果你了解Android或者iOS的话,你会非常熟悉我们今天要说的的内容. 基本上一个React Native的 ...
- React Native填坑之旅--重新认识RN
如同黑夜里的一道光一样,就这么知道了F8. F8是每年一次Facebook每年一次的开发者大会.每次大会都会release相应的APP,iOS.Android都有.之前都是用Native开发的,但是2 ...
- React Native填坑之旅--动画
动画是提高用户体验不可缺少的一个元素.恰如其分的动画可以让用户更明确的感知当前的操作是什么. 无疑在使用React Native开发应用的时候也需要动画.这就需要知道RN都给我们提供了那些动画,和每个 ...
随机推荐
- Apache配置简单http认证
首先要说明的是这种认证是不安全的,密码是明文传输,因此很容易被各种嗅探软件嗅探到密码,只能用于简单的认证.今天上午把ownCloud卸载了,这玩 意儿中看不中用啊,原来10M的访问速度被限制成了几百K ...
- EXT学习之——EXT下拉框默认绑定第一个值
//默认第一个下拉框绑定值if (this.moduleCombo.store.getAt(0) != undefined) { this.moduleCombo.setValue(this.modu ...
- Python图像处理库:Pillow 初级教程
Python图像处理库:Pillow 初级教程 2014-09-14 翻译 http://pillow.readthedocs.org/en/latest/handbook/tutorial.html ...
- linux网络完全与防护
7.1 网络封包联机进入主机的流程 7.1.1 封包进入主机的流程 1.经过防火墙的分析 iptables 主要功能是封包过滤 主要分析TCP/IP的封包表头来进行过滤的机制 分析的是OSI的第二 ...
- 【PowerOJ1740&网络流24题 圆桌聚餐】(最大流)
题意: 来自n个不同国家的代表开会,每个国家代表数为ci 会场有m张圆桌,每张桌子可容纳mi人 不希望有同一个国家的代表在同一张桌子上就餐 设计一个合法方案 (n,m<=300) 思路:最大流, ...
- libcurl多线程超时设置不安全(转)
from http://www.cnblogs.com/kex1n/p/4135263.html (1), 超时(timeout) libcurl 是 一个很不错的库,支持http,ftp等很多的协议 ...
- VS调试在Win7(vista系列)操作系统下 HttpListener拒绝访问解决办法
一. VS调试在Win7(vista系列)操作系统下 HttpListener无法绑定多个 指定IP.端口问题 来自:http://www.cnblogs.com/ryhan/p/4195693.ht ...
- MySQL中如何插入反斜杠,反斜杠被吃掉,反斜杠转义
问题描述:mysql中带有反斜杠的内容入库后,发现反斜杠无故失踪了(俗话说被吃掉了) 例:插入insert into tb('url') values('absc\eeee'); 结果数据库里的内容是 ...
- 技术英文单词贴--G
G generator 发电机,发生器,生产者
- Linux命令(1)- grep
1.grep 功能:查找文件里符合条件的字符串. 语法:grep [-abcEFGhHilLnqrsvVwxy][-A<显示列数>][-B<显示列数>][-C<显示列数& ...