React Native填坑之旅--动画篇

CecileChast 7年前
   <p>动画是提高用户体验不可缺少的一个元素。恰如其分的动画可以让用户更明确的感知当前的操作是什么。</p>    <p>无疑在使用React Native开发应用的时候也需要动画。这就需要知道RN都给我们提供了那些动画,和每个动画可以处理的功能有哪些。</p>    <h2><strong>填坑材料Animated</strong></h2>    <p>动画API提供了一些现成的组件: Animated.View , Animated.Text 和 Animated.Image 默认支持动画。动画API会调用iOS或者Android的本地代码来完成这些组件的位移、大小等动画。这样各种动画在视觉上可以非常的流畅。</p>    <h3><strong>一个绕着屏幕转的动画</strong></h3>    <p>绕着屏幕转的动画:</p>    <pre>  <code class="language-javascript"><--<   |  |   V--^</code></pre>    <p>基本代码</p>    <pre>  <code class="language-javascript">export default class AnimatedSquare extends React.Component {      constructor(props) {          super(props);          // 1          this.state = {              pan: new Animated.ValueXY          }      }        getStyle() {          return [              styles.square,              {                  transform: this.state.pan.getTranslateTransform()              }          ];      }        render() {          return (              <View style={styles.container}>                  <Animated.View style={this.getStyle()} />              </View>          );      }  }</code></pre>    <p>解释一下:</p>    <ol>     <li> <p>在 this.state 里用了 Animated.ValueXY 的实例。这样 Animated 动画就知道需要处理的是X和Y两个方向的值。</p> </li>     <li> <p>getStyle() 方法会返回一个数组,因为动画需要 square 和 transform 两个样式值。 getTranslateTransform() 方法会获得一个数组: [{translateX: xValue}, {translateY: yValue}] 。 xValue 和 yValue 就是根据我们开始的时候设置的 Animated.ValueXY 解析出来的。</p> </li>     <li> <p>最后设置 Animated.View 。也就是动画最终作用的元素(或者叫做视图)。</p> </li>    </ol>    <p>依赖和样式</p>    <p>依赖项:</p>    <pre>  <code class="language-javascript">import React from 'react';  import {      Dimensions,      StyleSheet,      View,      Animated  } from 'react-native';    let {      width,      height  } = Dimensions.get('window');    const SQUARE_DIMENSIONS = 30;</code></pre>    <p>样式:</p>    <pre>  <code class="language-javascript">const styles = StyleSheet.create({      container: {          flex: 1      },      square: {          width: SQUARE_DIMENSIONS,          height: SQUARE_DIMENSIONS,          backgroundColor: 'blue'      }  });</code></pre>    <p>动起来</p>    <p>我们准备让这个目标物体,一个方框,从左上角: x = 0, y = 0 到左下角: x = 0, y = (screenHeight - squreHeight) 。</p>    <pre>  <code class="language-javascript">const SPRING_CONFIG = {tension: 2, friction: 3};        componentDidMount() {          Animated.spring(this.state.pan, {              ...SPRING_CONFIG,              toValue: {x: 0, y: height - SQUARE_DIMENSIONS}                        // return to start          }).start();      }</code></pre>    <p>componentDidMount 方法是RN组件的生命周期方法,在组件完成html渲染的时候调用。在组件渲染完成后开始动画。</p>    <p>我们指定了 SPRING_CONFIG 为弹簧动画的弹力和摩擦力,值都不大。所以这个方框会在屏幕的每个角稍微弹几下。</p>    <p>依次的动动动。。。</p>    <p>我们可以让几个动画按照顺序依次执行。这些动画会一个挨一个的执行。 sequence 方法是动画API组织动画的多种方式之一。也可以使用 parallel 来组织,这样几个动画会并行执行。</p>    <pre>  <code class="language-javascript">componentDidMount() {          Animated.sequence([              Animated.spring(this.state.pan, {                  ...SPRING_CONFIG,                  toValue: {x: 0, y: height - SQUARE_DIMENSIONS} //animate to bottom left              }),              Animated.spring(this.state.pan, {                ...SPRING_CONFIG,                toValue: {x: width - SQUARE_DIMENSIONS, y: height - SQUARE_DIMENSIONS} // animated to bottom right              }),              Animated.spring(this.state.pan, {                  ...SPRING_CONFIG,                  toValue: {x: width - SQUARE_DIMENSIONS, y: 0} //animate to top right              }),              Animated.spring(this.state.pan, {                ...SPRING_CONFIG,                toValue: {x: 0, y: 0} // return to start              })          ]).start(cb);      }</code></pre>    <p>我们定义了四个弹簧动画。这个四个一组的动画执行的顺序就是前文指定的顺序。</p>    <p>重复动画</p>    <p>调用动画的 start 方法的时候可以传入一个回调方法作为参数。这个回调方法会在这一组动画执行完成之后调用。在本例中,每次动画执行完之后会再次调用开始动画的方法。</p>    <pre>  <code class="language-javascript">componentDidMount() {          this.startAndRepeat();      }        startAndRepeat() {          this.triggerAnimation(this.startAndRepeat);      }        triggerAnimation(cb) {          Animated.sequence([              Animated.spring(this.state.pan, {                  ...SPRING_CONFIG,                  toValue: {x: 0, y: height - SQUARE_DIMENSIONS} //animate to bottom left              }),              Animated.spring(this.state.pan, {                ...SPRING_CONFIG,                toValue: {x: width - SQUARE_DIMENSIONS, y: height - SQUARE_DIMENSIONS} // animated to bottom right              }),              Animated.spring(this.state.pan, {                  ...SPRING_CONFIG,                  toValue: {x: width - SQUARE_DIMENSIONS, y: 0} //animate to top right              }),              Animated.spring(this.state.pan, {                ...SPRING_CONFIG,                toValue: {x: 0, y: 0} // return to start              })          ]).start(cb);      }</code></pre>    <p>调用 triggerAnimation 方法开始动画,并传入再次开始动画的方法 startAndRepeat 。</p>    <h2><strong>完整代码</strong></h2>    <pre>  <code class="language-javascript">import React from 'react';  import {      Dimensions,      StyleSheet,      View,      Animated  } from 'react-native';    let {      width,      height  } = Dimensions.get('window');    const SQUARE_DIMENSIONS = 30;  const SPRING_CONFIG = {tension: 2, friction: 3};    export default class AnimatedSquare extends React.Component {      constructor(props) {          super(props);          this.state = {              pan: new Animated.ValueXY          }            // bind          this.startAndRepeat = this.startAndRepeat.bind(this);          this.getStyle = this.getStyle.bind(this);          this.startAndRepeat = this.startAndRepeat.bind(this);          this.triggerAnimation = this.triggerAnimation.bind(this);      }        componentDidMount() {          this.startAndRepeat();      }        startAndRepeat() {          this.triggerAnimation(this.startAndRepeat);      }        getStyle() {          return [              styles.square,              {                  transform: this.state.pan.getTranslateTransform()              }          ];      }        triggerAnimation(cb) {          Animated.sequence([              Animated.spring(this.state.pan, {                  ...SPRING_CONFIG,                  toValue: {x: 0, y: height - SQUARE_DIMENSIONS} //animate to bottom left              }),              Animated.spring(this.state.pan, {                ...SPRING_CONFIG,                toValue: {x: width - SQUARE_DIMENSIONS, y: height - SQUARE_DIMENSIONS} // animated to bottom right              }),              Animated.spring(this.state.pan, {                  ...SPRING_CONFIG,                  toValue: {x: width - SQUARE_DIMENSIONS, y: 0} //animate to top right              }),              Animated.spring(this.state.pan, {                ...SPRING_CONFIG,                toValue: {x: 0, y: 0} // return to start              })          ]).start(cb);      }        render() {          return (              <View style={styles.container}>                  <Animated.View style={this.getStyle()} />              </View>          );      }  }    const styles = StyleSheet.create({      container: {          flex: 1      },      square: {          width: SQUARE_DIMENSIONS,          height: SQUARE_DIMENSIONS,          backgroundColor: 'blue'      }  });</code></pre>    <p> </p>    <p>来自:https://segmentfault.com/a/1190000007024556</p>    <p> </p>