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',
      },
    });
    
  • 相关阅读:
    做过的笔试题
    (转)32位机器中int的字长
    JS_void()
    JS_增加事件,移除事件,动态元素的增删事件研究
    JS_animate 站在别人的肩膀上
    JS_对象的方法
    JS_Class.extend
    JS_返回值
    JS_eventBind
    JS_应用对象的复制
  • 原文地址:https://www.cnblogs.com/smart-girl/p/10648425.html
Copyright © 2011-2022 走看看