比較精細的動畫能夠用Animated
來控制。可是,在一些簡單的界面切換、更新的時候所作的動畫裏再去計算開始值、結束值和插值器如何運做絕對是浪費時間。react
RN正好給咱們提供了LayoutAnimation
來解決這個問題。按照官方的說法:LayoutAnimation
就是用於在下一個繪製或者佈局週期(render/layout cycle)裏處理界面中所有視圖的動畫的。spring
下面看一個例子:react-native
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()
這麼一行代碼。flex
LayoutAnimation
默認的提供了三種動畫:linear
, spring
和easeInEaseOut
。 固然,RN也留出了自定義的接口。你能夠按照本身須要的自定義動畫效果。動畫
下面看看如何自定義:this
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 }); } //...略... };
爲了直入主題,部份內容省略。後面有完整的代碼。spa
自定義很是簡單,固然限制也很多。只須要指定動畫的duration
、create
和update
。code
另一個本例與上例不一樣的地方在於LayoutAnimation能夠只在componentWillUpdate()
方法裏指定,不須要在點擊事件裏指定。component
//@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' } });