React Native之LayoutAnimation自动布局动画的使用DEMO

使用LayoutAnimation 可以使得布局发生变化时,不会那么生硬,带点感性动画

1.先看效果

QQ20170621-155557.gif

2.直接显示代码
【注1】涉及到ref的使用
栗子1: ref='touchBt' 使用的时候需要这样调用 this.refs.touchBt.props.属性值
栗子2: ref={(ref) => this.touchBt = ref} 使用的时候直接 this.touchbt.props.属性值
这两者是一样的
【注2】setNativeProps 的基础使用,当然也可以直接用setState的方法替代,两者setNativeProps不会重新渲染走render方法,只改变当前对象的属性
setState 重新渲染render 方法

/**
*  设置组建属性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个参数
    *
    *  duration:动画持续的时间
    *  create:创建一个新视图所使用的动画
    *  update:当视图被更新的时候所使用的动画
    *  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',
   }
});

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 原教程内容详见精益 React 学习指南,这只是我在学习过程中的一些阅读笔记,个人觉得该教程讲解深入浅出,比目前大...
    leonaxiong阅读 2,940评论 1 18
  • 前言 学习本系列内容需要具备一定 HTML 开发基础,没有基础的朋友可以先转至 HTML快速入门(一) 学习 本人...
    珍此良辰阅读 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,391评论 0 2
  • 他们知道,就算你心里不爽,就算你因为他们的事浪费了很多时间,你也不能把他们怎么样。最多你就是生个闷气,回家和家人抱...
    淡淡君阅读 1,974评论 28 17

友情链接更多精彩内容