react-native动画之LayoutAnimation--

作者: 编程小石头666 | 来源:发表于2018-05-25 11:00 被阅读20次
  • 最近业务需要,在学习react-native,就顺便做个react-native实战入门系列教程。我会在学习过程中,把自己认为有用的,常用的rn知识点总结出来分享给大家

今天给大家分享reactnative动画

  • LayoutAnimation动画
    先看效果图


    LayoutAnimationExample.gif

再来看实现代码

import React from 'react';
import {
    NativeModules,
    LayoutAnimation,
    Text,
    TouchableOpacity,
    StyleSheet,
    View,
} from 'react-native';

//如果要在Android上使用LayoutAnimation,那么目前还需要在UIManager中启用:
const {UIManager} = NativeModules;
UIManager.setLayoutAnimationEnabledExperimental &&
UIManager.setLayoutAnimationEnabledExperimental(true);

export default class App extends React.Component {
    state = {
        w: 100,
        h: 100,
    };

    _onPress = () => {
        // Animate the update
        LayoutAnimation.spring();
        this.setState({w: this.state.w + 15, h: this.state.h + 15})
    }

    render() {
        return (
            <View style={styles.container}>
                <View style={[styles.box, {width: this.state.w, height: this.state.h}]}/>
                <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: {
        width: 200,
        height: 200,
        backgroundColor: 'red',
    },
    button: {
        backgroundColor: 'black',
        paddingHorizontal: 20,
        paddingVertical: 15,
        marginTop: 15,
    },
    buttonText: {
        color: '#fff',
        fontWeight: 'bold',
    },
});

是不是实现起来很简单,以后给大家分享的实例都是力求简单,方便大家用到的时候,直接来把代码copy走就可以直接用了。当然大家也可以参考借鉴来实现自己的功能。

相关文章

网友评论

  • 42ab2d42527d:Hello~我是腾讯云+社区的运营小编,阅读下来觉得您的内容质量很不错,非常符合我们的《自媒体分享计划》入选要求,所以想咨询一下是否可以将您的文章同步至腾讯云+社区呢? 我们会提供为您基于腾讯云的云服务器、域名等云服务,内容可以在云+社区享受百万流量级的推广,优质文章还有机会在腾讯系内容矩阵进行推广,另外还有技术作者交流机会、腾讯云周边礼物等支持。《自媒体分享计划》的链接是:https://cloud.tencent.com/developer/support-plan 打开地址进行简单申请即可,成功入驻后,社区会与您的源平台自动进行内容同步,不需要您手动搬运哦~。 如果有什么疑问的话可以加我微信详聊哦~我的微信号是:13189608841。

本文标题:react-native动画之LayoutAnimation--

本文链接:https://www.haomeiwen.com/subject/xwyyjftx.html