React Native填坑之旅--LayoutAnimation篇

jkbu5077 8年前發布 | 18K 次閱讀 ReactNative 移動開發 React Native

比較精細的動畫可以用 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'
  }
});

 

來自:https://segmentfault.com/a/1190000007088741

 

 本文由用戶 jkbu5077 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!