zoukankan      html  css  js  c++  java
  • React Native填坑之旅--动画

    动画是提高用户体验不可缺少的一个元素。恰如其分的动画可以让用户更明确的感知当前的操作是什么。

    无疑在使用React Native开发应用的时候也需要动画。这就需要知道RN都给我们提供了那些动画,和每个动画可以处理的功能有哪些。

    填坑材料Animated

    动画API提供了一些现成的组件:Animated.ViewAnimated.TextAnimated.Image默认支持动画。动画API会调用iOS或者Android的本地代码来完成这些组件的位移、大小等动画。这样各种动画在视觉上可以非常的流畅。

    一个绕着屏幕转的动画

    绕着屏幕转的动画:

    <--< 
    |  | 
    V--^ 
    

    基本代码

    export default class AnimatedSquare extends React.Component {
        constructor(props) {
            super(props);
            // 1
            this.state = {
                pan: new Animated.ValueXY
            }
        }
    
        getStyle() {
            return [
                styles.square,
                {
                    transform: this.state.pan.getTranslateTransform()
                }
            ];
        }
    
        render() {
            return (
                <View style={styles.container}>
                    <Animated.View style={this.getStyle()} />
                </View>
            );
        }
    }
    

    解释一下:

    1. this.state里用了Animated.ValueXY的实例。这样Animated动画就知道需要处理的是X和Y两个方向的值。
    2. getStyle()方法会返回一个数组,因为动画需要squaretransform两个样式值。getTranslateTransform()方法会获得一个数组:[{translateX: xValue}, {translateY: yValue}]xValueyValue就是根据我们开始的时候设置的Animated.ValueXY解析出来的。
    3. 最后设置Animated.View。也就是动画最终作用的元素(或者叫做视图)。

    依赖和样式

    依赖项:

    import React from 'react';
    import {
        Dimensions,
        StyleSheet,
        View,
        Animated
    } from 'react-native';
    
    let {
        width,
        height
    } = Dimensions.get('window');
    
    const SQUARE_DIMENSIONS = 30;
    

    样式:

    const styles = StyleSheet.create({
        container: {
            flex: 1
        },
        square: {
             SQUARE_DIMENSIONS,
            height: SQUARE_DIMENSIONS,
            backgroundColor: 'blue'
        }
    });
    

    动起来

    我们准备让这个目标物体,一个方框,从左上角:x = 0, y = 0到左下角: x = 0, y = (screenHeight - squreHeight)

    const SPRING_CONFIG = {tension: 2, friction: 3};
    
        componentDidMount() {
            Animated.spring(this.state.pan, {
                ...SPRING_CONFIG,
                toValue: {x: 0, y: height - SQUARE_DIMENSIONS}                        // return to start
            }).start();
        }
    

    componentDidMount方法是RN组件的生命周期方法,在组件完成html渲染的时候调用。在组件渲染完成后开始动画。

    我们指定了SPRING_CONFIG为弹簧动画的弹力和摩擦力,值都不大。所以这个方框会在屏幕的每个角稍微弹几下。

    依次的动动动。。。

    我们可以让几个动画按照顺序依次执行。这些动画会一个挨一个的执行。sequence方法是动画API组织动画的多种方式之一。也可以使用parallel来组织,这样几个动画会并行执行。

        componentDidMount() {
            Animated.sequence([
                Animated.spring(this.state.pan, {
                    ...SPRING_CONFIG,
                    toValue: {x: 0, y: height - SQUARE_DIMENSIONS} //animate to bottom left
                }),
                Animated.spring(this.state.pan, {
                  ...SPRING_CONFIG,
                  toValue: {x: width - SQUARE_DIMENSIONS, y: height - SQUARE_DIMENSIONS} // animated to bottom right
                }),
                Animated.spring(this.state.pan, {
                    ...SPRING_CONFIG,
                    toValue: {x: width - SQUARE_DIMENSIONS, y: 0} //animate to top right
                }),
                Animated.spring(this.state.pan, {
                  ...SPRING_CONFIG,
                  toValue: {x: 0, y: 0} // return to start
                })
            ]).start(cb);
        }
    
    

    我们定义了四个弹簧动画。这个四个一组的动画执行的顺序就是前文指定的顺序。

    重复动画

    调用动画的start方法的时候可以传入一个回调方法作为参数。这个回调方法会在这一组动画执行完成之后调用。在本例中,每次动画执行完之后会再次调用开始动画的方法。

        componentDidMount() {
            this.startAndRepeat();
        }
    
        startAndRepeat() {
            this.triggerAnimation(this.startAndRepeat);
        }
    
        triggerAnimation(cb) {
            Animated.sequence([
                Animated.spring(this.state.pan, {
                    ...SPRING_CONFIG,
                    toValue: {x: 0, y: height - SQUARE_DIMENSIONS} //animate to bottom left
                }),
                Animated.spring(this.state.pan, {
                  ...SPRING_CONFIG,
                  toValue: {x: width - SQUARE_DIMENSIONS, y: height - SQUARE_DIMENSIONS} // animated to bottom right
                }),
                Animated.spring(this.state.pan, {
                    ...SPRING_CONFIG,
                    toValue: {x: width - SQUARE_DIMENSIONS, y: 0} //animate to top right
                }),
                Animated.spring(this.state.pan, {
                  ...SPRING_CONFIG,
                  toValue: {x: 0, y: 0} // return to start
                })
            ]).start(cb);
        }
    
    

    调用triggerAnimation方法开始动画,并传入再次开始动画的方法startAndRepeat

    完整代码

    import React from 'react';
    import {
        Dimensions,
        StyleSheet,
        View,
        Animated
    } from 'react-native';
    
    let {
        width,
        height
    } = Dimensions.get('window');
    
    const SQUARE_DIMENSIONS = 30;
    const SPRING_CONFIG = {tension: 2, friction: 3};
    
    export default class AnimatedSquare extends React.Component {
        constructor(props) {
            super(props);
            this.state = {
                pan: new Animated.ValueXY
            }
    
            // bind
            this.startAndRepeat = this.startAndRepeat.bind(this);
            this.getStyle = this.getStyle.bind(this);
            this.startAndRepeat = this.startAndRepeat.bind(this);
            this.triggerAnimation = this.triggerAnimation.bind(this);
        }
    
        componentDidMount() {
            this.startAndRepeat();
        }
    
        startAndRepeat() {
            this.triggerAnimation(this.startAndRepeat);
        }
    
        getStyle() {
            return [
                styles.square,
                {
                    transform: this.state.pan.getTranslateTransform()
                }
            ];
        }
    
        triggerAnimation(cb) {
            Animated.sequence([
                Animated.spring(this.state.pan, {
                    ...SPRING_CONFIG,
                    toValue: {x: 0, y: height - SQUARE_DIMENSIONS} //animate to bottom left
                }),
                Animated.spring(this.state.pan, {
                  ...SPRING_CONFIG,
                  toValue: {x: width - SQUARE_DIMENSIONS, y: height - SQUARE_DIMENSIONS} // animated to bottom right
                }),
                Animated.spring(this.state.pan, {
                    ...SPRING_CONFIG,
                    toValue: {x: width - SQUARE_DIMENSIONS, y: 0} //animate to top right
                }),
                Animated.spring(this.state.pan, {
                  ...SPRING_CONFIG,
                  toValue: {x: 0, y: 0} // return to start
                })
            ]).start(cb);
        }
    
        render() {
            return (
                <View style={styles.container}>
                    <Animated.View style={this.getStyle()} />
                </View>
            );
        }
    }
    
    const styles = StyleSheet.create({
        container: {
            flex: 1
        },
        square: {
             SQUARE_DIMENSIONS,
            height: SQUARE_DIMENSIONS,
            backgroundColor: 'blue'
        }
    });
    
    

    更多相关代码请移步:https://github.com/future-challenger/petshop/tree/master/client/petshop

  • 相关阅读:
    Python之异常篇 [待更新]
    python脚本工具 - 4 获取系统当前时间
    python脚本工具 - 3 目录遍历
    数字签名和数字证书到底是个神马玩意?
    CSRF攻击[转]
    Python之数据结构篇
    Python之模块篇
    Python之函数篇
    python脚本工具-2 去除扩展名后提取目录下所有文件名并保存
    python脚本工具-1 制作爬虫下载网页图片
  • 原文地址:https://www.cnblogs.com/sunshine-anycall/p/5914230.html
Copyright © 2011-2022 走看看