React Native之LayoutAnimation自動布局動畫的使用DEMO

使用LayoutAnimation 可以使得布局發(fā)生變化時,不會那么生硬,帶點感性動畫

1.先看效果

QQ20170621-155557.gif

2.直接顯示代碼
【注1】涉及到ref的使用
栗子1: ref='touchBt' 使用的時候需要這樣調(diào)用 this.refs.touchBt.props.屬性值
栗子2: ref={(ref) => this.touchBt = ref} 使用的時候直接 this.touchbt.props.屬性值
這兩者是一樣的
【注2】setNativeProps 的基礎(chǔ)使用,當(dāng)然也可以直接用setState的方法替代,兩者setNativeProps不會重新渲染走render方法,只改變當(dāng)前對象的屬性
setState 重新渲染render 方法

/**
*  設(shè)置組建屬性setNativeProps
*
*
*/

'use strict'
import React, {Component} from 'react'
import {
   Navigator,
   View,
   Text,
   StyleSheet,
   TouchableOpacity,
   PixelRatio,
   Dimensions,
   Button,
   ScrollView,
   LayoutAnimation
} from 'react-native'

const {width, height} = Dimensions.get('window');


var CustomLayoutAnimation = {
   /**
    *  onfig 指定4個參數(shù)
    *
    *  duration:動畫持續(xù)的時間
    *  create:創(chuàng)建一個新視圖所使用的動畫
    *  update:當(dāng)視圖被更新的時候所使用的動畫
    *  delete:刪除一個視圖使用的動畫
    */
   /**
    *  Types:動畫類型有
    *  spring: 彈跳 linear: 線性 easeInEaseOut: 緩入緩出 easeIn:緩入 easeOut: 緩出 keyBoard:鍵入
    * */
   /**
    *  Properties 屬性有
    *  opcity:透明度  scaleXY 縮放
    * */

   duration: 1300,
   create: {
       type: LayoutAnimation.Types.easeInEaseOut,
       property: LayoutAnimation.Properties.opacity,
   },
   update: {
       type: LayoutAnimation.Types.spring,
      property: LayoutAnimation.Properties.scaleXY,
   },
   delete: {
       type: LayoutAnimation.Types.linear,
       property: LayoutAnimation.Properties.opacity,
   }
}

export default class SetnativeProps extends Component {

   constructor(props) {
       super(props);
       this.state = {}
   }
   componentDidMount() {
   }
   handelAction = (flag)=> {
       LayoutAnimation.configureNext(CustomLayoutAnimation);
       this.refView.setNativeProps({
           style: {backgroundColor: 'tomato', height: flag === 'open' ? 350 : 0}
       });
   }

   render() {

       return (
           <View style={styles.container}>
               <ScrollView
               >
                   <Button
                       title='open'
                       color='green'
                       onPress={()=> {
                           this.handelAction('open');
                       }}
                       ref={(ref) => this.touchBt = ref}
                   />
                   <View
                       ref={(c) => this.refView = c}
                       style={{backgroundColor: '#e5e55e', width: width, height: 0}}/>
                   <Button
                       title='close'
                       color='green'
                       onPress={()=> {
                           this.handelAction('close');
                       }}
                       ref={(ref) => this.touchBt1 = ref}
                   />
               </ScrollView>
           </View>
       );
   }
}

const styles = StyleSheet.create({
   container: {
       paddingTop: 100,
       flex: 1,
       backgroundColor: '#F5FCFF',
   },
   bgView: {
       top: 100,
       width: width,
       height: 100,
       backgroundColor: '#eeeeee',
       justifyContent: 'center',
       alignItems: 'center',
   }
});

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 原教程內(nèi)容詳見精益 React 學(xué)習(xí)指南,這只是我在學(xué)習(xí)過程中的一些閱讀筆記,個人覺得該教程講解深入淺出,比目前大...
    leonaxiong閱讀 2,940評論 1 18
  • 前言 學(xué)習(xí)本系列內(nèi)容需要具備一定 HTML 開發(fā)基礎(chǔ),沒有基礎(chǔ)的朋友可以先轉(zhuǎn)至 HTML快速入門(一) 學(xué)習(xí) 本人...
    珍此良辰閱讀 2,403評論 3 9
  • 自己最近的項目是基于react的,于是讀了一遍react的文檔,做了一些記錄(除了REFERENCE部分還沒開始讀...
    潘逸飛閱讀 3,737評論 1 10
  • It's a common pattern in React to wrap a component in an ...
    jplyue閱讀 3,397評論 0 2
  • 他們知道,就算你心里不爽,就算你因為他們的事浪費了很多時間,你也不能把他們怎么樣。最多你就是生個悶氣,回家和家人抱...
    淡淡君閱讀 1,975評論 28 17

友情鏈接更多精彩內(nèi)容