React Native填坑之旅--LayoutAnimation篇

比较精细的动画能够用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 , springeaseInEaseOut。 固然,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

自定义很是简单,固然限制也很多。只须要指定动画的durationcreateupdatecode

另一个本例与上例不一样的地方在于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'
  }
});
相关文章
相关标签/搜索