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都给我们提供了那些动画,和每个 ...
随机推荐
- Windows、VS 与 .net
原文地址:https://msdn.microsoft.com/en-us/library/bb822049(v=vs.110).aspx .NET Framework version CLR ver ...
- 五、selecting with the API
1. 命令通常从selection list中得到input, 调用MGlobal::getActiveSelectionList(MSelectionList &dest, bool ord ...
- AX2012 常用表关系(客户地址,联系信息)
//客户地址信息 static void CustAddressInformation(Args _args) { CustTable custTable; DirPartyTable dirPart ...
- Ms sql将首字母大写
--辅助表 create table a ( a int ) declare @b int begin insert into a values(@b) end; go --表数据 ),id int) ...
- C++学习基础八——重载输入和输出操作符
一.重载输入操作符的要点: 1.返回值为istream &. 2.第一个参数为istream &in. 3.第二个参数为自定义类型的引用对象(例如Sales_Item &ite ...
- maven仓库有jar包,还是找不到类
开始,网上的所有方法都没用. 我用的eclipse-32位的,jdk也是.然后今天换了个sts和jdk.64位的.然后就没有那个问题了.
- C语言的time.h解释python的time
clock_t clock(void),对比python的clock() 返回程序执行的时间,clock_t的实际类型是long #include<Windows.h> Sleep(); ...
- docker中安装ssh服务
系统:Debian Docker 目标:在docker(debian系统)中安装ssh服务,实现远程登陆和控制docker 步骤: 初始状态:通过docker pull debian得到的一个debi ...
- 模拟ATM机将输入的数据插入数据库
ATM抽象类 public abstract class ATM { private double balance; private String idcard; private String pas ...
- css颜色大全-转载
FFFFFF #DDDDDD #AAAAAA #888888 #666666 #444444 #000000 #FFB7DD #FF88C2 #FF44AA #FF0088 #C10066 #A ...