zoukankan      html  css  js  c++  java
  • react-native中的动画

    先看效果

    这个一个渐渐显示的动画,代码如下

    import React from 'react';
    import { Animated, Text, View } from 'react-native';
    
    class FadeInView extends React.Component {
      //定义状态
      state = {
        fadeAnim: new Animated.Value(0),  // Initial value for opacity: 0
      }
    
      componentDidMount() {
        Animated.timing(                  // Animate over time
          this.state.fadeAnim,            // The animated value to drive
          {
            toValue: 1,                   // Animate to opacity: 1 (opaque)
            duration: 10000,              // Make it take a while
          }
        ).start();                        // Starts the animation
      }
    
      render() {
        let { fadeAnim } = this.state;
    
        return (
          <Animated.View                 // Special animatable View
            style={{
              ...this.props.style,
              opacity: fadeAnim,         // Bind opacity to animated value
            }}
          >
            {this.props.children}
          </Animated.View>
        );
      }
    }
    
    // You can then use your `FadeInView` in place of a `View` in your components:
    export default class App extends React.Component {
      render() {
        return (
          <View style={{flex: 1, alignItems: 'center', justifyContent: 'center'}}>
            <FadeInView style={{ 250, height: 50, backgroundColor: 'powderblue'}}>
              <Text style={{fontSize: 28, textAlign: 'center', margin: 10}}>Fading in</Text>
            </FadeInView>
          </View>
        )
      }
    }
    

    第二个demo
    点击图形变大
    先看效果

    再看代码如下

    import React from 'react';
    import {
      NativeModules,
      LayoutAnimation,
      Text,
      TouchableOpacity,
      StyleSheet,
      View,
    } from 'react-native';
    
    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, { 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: {
         200,
        height: 200,
        backgroundColor: 'red',
      },
      button: {
        backgroundColor: 'black',
        paddingHorizontal: 20,
        paddingVertical: 15,
        marginTop: 15,
      },
      buttonText: {
        color: '#fff',
        fontWeight: 'bold',
      },
    });
    
  • 相关阅读:
    封装缓动动画函数
    封装动画函数-匀速运动
    实现产品图片的放大镜效果:
    仿淘宝侧边栏滚动案例:
    页面被卷去的头部兼容性解决方案
    简单发送短信倒计时案例
    Echarts 版本的那些坑
    json变量作键名
    媒体查询那些事儿
    mac 强制关闭指定端口
  • 原文地址:https://www.cnblogs.com/smart-girl/p/10648425.html
Copyright © 2011-2022 走看看