ReactNative-アニメーション

この章では、使用方法を説明します LayoutAnimation ReactNativeで。

アニメーションコンポーネント

設定します myStyle国家の財産として。このプロパティは、内部の要素のスタイルを設定するために使用されますPresentationalAnimationComponent

また、2つの関数を作成します- expandElement そして collapseElement。これらの関数は、状態から値を更新します。最初のものは使用しますspring プリセットアニメーション、2番目のアニメーションには linearプリセット。これらも小道具として渡します。ザ・Expand そしてその Collapse ボタンは expandElement() そして collapseElement() 関数。

この例では、ボックスの幅と高さを動的に変更します。以来Home コンポーネントは同じになり、変更するだけです Animations 成分。

App.js

import React, { Component } from 'react'
import { View, StyleSheet, Animated, TouchableOpacity } from 'react-native'

class Animations extends Component {
   componentWillMount = () => {
      this.animatedWidth = new Animated.Value(50)
      this.animatedHeight = new Animated.Value(100)
   }
   animatedBox = () => {
      Animated.timing(this.animatedWidth, {
         toValue: 200,
         duration: 1000
      }).start()
      Animated.timing(this.animatedHeight, {
         toValue: 500,
         duration: 500
      }).start()
   }
   render() {
      const animatedStyle = { width: this.animatedWidth, height: this.animatedHeight }
      return (
         <TouchableOpacity style = {styles.container} onPress = {this.animatedBox}>
            <Animated.View style = {[styles.box, animatedStyle]}/>
         </TouchableOpacity>
      )
   }
}
export default Animations

const styles = StyleSheet.create({
   container: {
      justifyContent: 'center',
      alignItems: 'center'
   },
   box: {
      backgroundColor: 'blue',
      width: 50,
      height: 100
   }
})