学习笔记--方便下次查看
大多数情况下,在 React Native 中创建动画是推荐使用 Animated API 的,其提供了三个主要的方法用于创建动画:

1.API

  1. Animated.timing() -- 最常用的动画类型,使一个值按照一个过渡曲线而随时间变化。
  2. Animated.decay() -- 衰变效果,以一个初始的速度和一个衰减系数逐渐减慢变为0。
  3. Animated.spring() -- 弹簧效果,基础的单次弹跳物理模型实现的 spring 动画

2.动画组件

Animated 封装了四个可以动画化的组件:
Animated.View、Animated.Text、Animated.Image、Animated.ScrollView
动画必须依赖这几个组件--使用当然和平常开发一样

4.Animated.timing()的使用

最常用的动画--我们看如何使用吧;
这边是一个渐变的效果;

渐变效果.gif

import React from "react";
import { Animated } from "react-native";export default class FadeInView extends React.Component {state = {fadeInOpacity: new Animated.Value(0) // 透明度初始值设为0};componentDidMount() {Animated.timing(// 随时间变化而执行动画this.state.fadeInOpacity, // 动画中的变量值{toValue: 1, // 透明度最终变为1,即完全不透明duration: 6000 // 让动画持续一段时间}).start();}render() {const { fadeInOpacity } = this.state;return (<Animated.View // 使用专门的可动画化的View组件style={{width: 100,height: 100,backgroundColor: "red",opacity: fadeInOpacity // 将透明度指定为动画变量值}}/>);}
}

这个demo 简单说一下代码的意思--方便理解

  • 在state中初始化一个动画效果值fadeInOpacity为0
  • 在生命周期中-使用动画的函数timing- 让fadeInOpacity在6秒钟从0变成1
  • fadeInOpacity的值使用在render里面控制opacity的值
    从而达到了一个渐变的效果

Animated.timing中的toValue配置项不一定是配置成1,也可以配置成其他数字比如控制一个view的宽度从0到100的过程,也可以试一下;

3.插值函数(interpolate)

interpolate():将输入值范围转换为输出值范围。
譬如:把0-1映射到0-10。
接下来,我们结合一个个的例子介绍它们的用法-当初自己学习挺懵逼的-先看demo

旋转.gif

import { Animated, Easing, StyleSheet, View } from "react-native";export default class Rotate extends React.Component<any, any> {state = {spinValue: new Animated.Value(0) //初始化动画的状态值};componentDidMount() {Animated.timing(this.state.spinValue, { //动画函数toValue: 1,                           //渐变的值duration: 3000,                        //时间}).start();}render() {const spin = this.state.spinValue.interpolate({inputRange: [0, 1],   //入参---是从0变化到1outputRange: ["0deg", "360deg"]   //出参---是从0deg变化到360deg});return (<View style={styles.container}><Animated.Imagestyle={{width: 300,height: 300,transform: [{ rotate: spin }]}}resizeMode={"contain"}source={{uri:"https://s3.amazonaws.com/media-p.slid.es/uploads/alexanderfarennikov/images/1198519/reactjs.png"}}/></View>);}
}const styles = StyleSheet.create({container: {flex: 1,alignItems: "center",justifyContent: "center"}
});

这是一个旋转的动画效果--代码和上文的渐变效果其实基本一致--唯一不同的是使用了interpolate 需要注意的是对inputRangeoutputRange的理解:动画属性值有一个从0到1的过程,我们其他属性也需要一个这样的过程,比如动画从0-360的旋转过程。那就需要一个映射的过程了。动画属性值从0到1 那我们旋转就从0-360。从而达到动画的效果了;

4.1interpolate的其他使用方式

inputRange是从0-1增长的过程。当然我们可以设置成多段的比如[0,0.5,1] ,那我们outputRange的也可以映射自己想要的过程。比如过程编导0.5的的时候,我想让他逆时针旋转,我们outputRange就可以写成这样[0,'360deg',0] 下面看下各种demo

多端设置组合.gif

import React from "react";
import { Animated, Easing, StyleSheet, View, Text } from "react-native";
export default class FadeInView extends React.Component<any, any> {constructor(props) {super(props);this.state = {animatedValue: new Animated.Value(0)};}componentDidMount() {this.spin();}spin() {this.state.animatedValue.setValue(0);Animated.timing(this.state.animatedValue, {toValue: 1,duration: 2000,easing: Easing.linear// delay:1000}).start(() => this.spin());}render() {let { animatedValue } = this.state;const marginLeft = animatedValue.interpolate({inputRange: [0, 1],outputRange: [0, 100]});const opacity = animatedValue.interpolate({inputRange: [0, 0.5, 1],outputRange: [0, 1, 0]});const movingMargin = animatedValue.interpolate({inputRange: [0, 0.5, 1],outputRange: [0, 300, 0]});const textSize = animatedValue.interpolate({inputRange: [0, 0.5, 1],outputRange: [18, 32, 18]});const rotateX = animatedValue.interpolate({inputRange: [0, 0.5, 1],outputRange: ["0deg", "180deg", "0deg"]});return (<View style={styles.container}><Animated.Viewstyle={{marginLeft,height: 30,width: 40,backgroundColor: "red"}}/><Animated.Viewstyle={{opacity,marginTop: 10,height: 30,width: 40,backgroundColor: "blue"}}/><Animated.Viewstyle={{marginLeft: movingMargin,marginTop: 10,height: 30,width: 40,backgroundColor: "orange"}}/><Animated.Textstyle={{fontSize: textSize,marginTop: 10,color: "green"}}>Animated Text!</Animated.Text><Animated.Viewstyle={{transform: [{ rotateX }],marginTop: 50,height: 30,width: 40,backgroundColor: "black"}}><Text style={{ color: "white" }}>Hello from TransformX</Text></Animated.View></View>);}
}
const styles = StyleSheet.create({container: {flex: 1}
});

我们可以用动画的值用在opacity、margins、text sizes 和 rotation 等样式属性中。就出现了上面的运动旋转等动画了

5.start()循环播放

如何实现动画的持续播放呢--那就需要用到我们start()的回调中调用自己,当然他是在动画结束后才会调用的-这个过程让我想到了 递归

  spin() {this.state.animatedValue.setValue(0);Animated.timing(this.state.animatedValue, {.....}).start(() => this.spin());}

tips:有一个小细节就this.state.animatedValue.setValue(0);这一步。动画结束后需要将初始值变成最初的值因为他有一个0-1的渐变过程。所以需要重置一下变量;

6.Animated.spring()

这个就是弹框效果了--直接看代码吧。使用都一样的

弹簧效果.gif

import React from "react";
import { Animated, StyleSheet, Text, View } from "react-native";
export default class FadeInView extends React.Component<any, any> {constructor(props) {super(props);this.state = {springValue: new Animated.Value(0)};}componentDidMount() {this.spring();}spring() {this.state.springValue.setValue(0);Animated.spring(this.state.springValue, {toValue: 1,               //放大的值:越大放大越大friction: 1,              //摩擦力:越大就越不能晃动的厉害tension:1                     //张力:越大就越有弹性-生动点}).start();}render() {return (<View style={styles.container}><Textstyle={{ marginBottom: 100 }}onPress={() => {this.spring();}}>点击可以出发动画</Text><Animated.Imagestyle={{width: 227,height: 200,transform: [{ scale: this.state.springValue }]}}source={{uri:"https://s3.amazonaws.com/media-p.slid.es/uploads/alexanderfarennikov/images/1198519/reactjs.png"}}/></View>);}
}const styles = StyleSheet.create({container: {flex: 1,alignItems: "center",justifyContent: "center"}
});

里面有一些属性需要注意一下--
toValue: 放大的值:越大放大越大
friction: 摩擦力:越大就越不能晃动的厉害
tension: 张力:越大就越有弹性-生动点

这边我好想吐槽一句--记笔记真的烦人--

7.Animated.parallel()

Animated.parallel() 会同时开始一个动画数组里的全部动画。

多个动画同时执行.gif

import React from "react";
import {Animated,Easing,StyleSheet,Text,TouchableHighlight,View
} from "react-native";
export default class FadeInView extends React.Component<any, any> {constructor(props) {super(props);this.state = {animatedValue1: new Animated.Value(0),animatedValue2: new Animated.Value(0),animatedValue3: new Animated.Value(0)};}componentDidMount() {this.animate();}animate() {this.state.animatedValue1.setValue(0);this.state.animatedValue2.setValue(0);this.state.animatedValue3.setValue(0);const createAnimation = function(value, duration, easing, delay = 0) {return Animated.timing(value, {toValue: 1,duration,easing,delay});};Animated.parallel([createAnimation(this.state.animatedValue1, 2000, Easing.ease),createAnimation(this.state.animatedValue2, 1000, Easing.ease, 1000),createAnimation(this.state.animatedValue3, 1000, Easing.ease, 2000)]).start();}spring() {this.state.springValue.setValue(0.3);Animated.spring(this.state.springValue, {toValue: 1,friction: 1// tension:1}).start();}render() {const scaleText = this.state.animatedValue1.interpolate({inputRange: [0, 1],outputRange: [0.5, 2]});const spinText = this.state.animatedValue2.interpolate({inputRange: [0, 1],outputRange: ["0deg", "720deg"]});const introButton = this.state.animatedValue3.interpolate({inputRange: [0, 1],outputRange: [-100, 400]});return (<View style={[styles.container]}><Animated.View style={{ transform: [{ scale: scaleText }] }}><Text>Welcome</Text></Animated.View><Animated.Viewstyle={{ marginTop: 20, transform: [{ rotate: spinText }] }}><Text style={{ fontSize: 20 }}>to the App!</Text></Animated.View><Animated.View style={{ top: introButton, position: "absolute" }}><TouchableHighlightonPress={this.animate.bind(this)}style={styles.button}><Text style={{ color: "white", fontSize: 20 }}>Click Here To Start</Text></TouchableHighlight></Animated.View></View>);}
}const styles = StyleSheet.create({container: {flex: 1,alignItems: "center",justifyContent: "center"},button: {width: 320,height: 90,backgroundColor: "blue",alignItems: "center",justifyContent: "center"}
});

8.Animated.parallel()

和 Animated.parallel() 一样, Animated.sequence() 接受一个动画数组。但不同的是,Animated.sequence() 是按顺序执行一个动画数组里的动画,等待一个完成后再执行下一个。

一个个输出播放.gif

import React from "react";
import { Animated, StyleSheet, View } from "react-native";
const arr = [];
for (let i = 0; i < 500; i++) {arr.push(i);
}
export default class FadeInView extends React.Component<any, any> {constructor(props) {super(props);this.state = {animatedValue: []};arr.forEach(value => {this.state.animatedValue[value] = new Animated.Value(0);});}componentDidMount() {this.animate();}animate() {const animations = arr.map(item => {return Animated.timing(this.state.animatedValue[item], {toValue: 1,duration: 100});});Animated.sequence(animations).start();}render() {const animations = arr.map((a, i) => {return (<Animated.Viewkey={i}style={{opacity: this.state.animatedValue[a],height: 20,width: 20,backgroundColor: "red",marginLeft: 3,marginTop: 3}}/>);});return <View style={styles.container}>{animations}</View>;}
}const styles = StyleSheet.create({container: {flex: 1,flexDirection: "row",flexWrap: "wrap"}
});

9. Animated.Stagger()

和 Animated.parallel() 和 Animated.sequence() 一样, Animated.Stagger 接受一个动画数组。但不同的是,Animated.Stagger 里面的动画有可能会同时执行(重叠),不过会以指定的延迟来开始。与上述两个动画主要的不同点是 Animated.Stagger 的第一个参数,delay 会被应用到每一个动画

可以同时播放的.gif

import React, {Component} from "react";import {Animated, StyleSheet, View} from "react-native";const arr = [];
for (let i = 0; i < 200; i++) {arr.push(i);
}
export default class animations extends Component<any, any> {constructor(props) {super(props);this.state = {animatedValue: []};arr.forEach(value => {this.state.animatedValue[value] = new Animated.Value(0);});}componentDidMount() {this.animate();}animate() {this.state.animatedValue.map(c=>{c.setValue(0);})const animations = arr.map(item => {return Animated.timing(this.state.animatedValue[item], {toValue: 1,duration: 1000});});Animated.stagger(10, animations).start(()=>{this.animate()});}render() {const animations = arr.map((a, i) => {return (<Animated.Viewkey={i}style={{opacity: this.state.animatedValue[a].interpolate({inputRange: [0, 0.5, 1],outputRange: [0, 1, 0.3]}),height: 20,width: 20,backgroundColor: "red",marginLeft: 3,marginTop: 3}}/>);});return <View style={styles.container}>{animations}</View>;}
}const styles = StyleSheet.create({container: {flex: 1,flexDirection: "row",flexWrap: "wrap"}
});

作者:小人头11
链接:https://www.jianshu.com/p/08d24a5e7c06
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

React Native 动画(Animated)笔记相关推荐

  1. [React Native] 动画 · Animated

    [React Native] 动画 · Animated 如果要在一个 React Native 应用中添加动画效果,首先考虑的就是官方提供的 Animated .通过定义输入和输出的动画状态值,调用 ...

  2. React Native动画Animated详解

    在移动开发中,动画是提高用户体验不可缺少的一个元素.在React Native中,动画API提供了一些现成的组件:Animated.View,Animated.Text和Animated.Image默 ...

  3. 19.React Native动画Animated效果三种动画类型二;

    目录 1.介绍 2.Animated.decay() 2.1方法 2.1.1value参数值 2.1.2config参数有以下这些属性: 2.2示例-执行缩放 2.2.1初始化缩放值 2.2.2绑定缩 ...

  4. React Native 动画 ---Animated

    关于Animated动画,使用的时候组件不能直接写<Text></Text> 要写成 <Animated.Text></Animated.Text> 先 ...

  5. React Native 动画(Animated)

    一.前言 对于一个移动应用APP,其中的动画交互能够给用户带来很好的体验,所以动画在移动应用开发过程中是非常重要的; 二.React Native中实现动画的方式 -不断修改state -Animat ...

  6. React Native 警告 Animated: `useNativeDriver` is not supported 的解决方案

    React Native 警告 Animated: `useNativeDriver` is not supported 的解决方案 当出现该报错的时候,是缺少了useNativeDrive参数造成了 ...

  7. react native 动画组件(Animated)浅析

    简述 react native封装了两个易于使用的动画组件.用于全局的布局动画LayoutAnimation,和用于创建更精细的交互控制的动画Animated.本章主要对Animated组件进行简单的 ...

  8. React Native动画入门全解析

    动画 React Native 提供了两个互补的动画系统: 用于创建精细的交互控制动画Animated 用于全局的布局动画LayoutAnimation Animated Animated通过 **. ...

  9. React Native开发学习笔记——WebStorm运行项目

    如何在webStorm上进行真机调试?下边以运行Android为例.ios有不同的地方. 调试分两步: debug配置. 运行. debug配置 点击Edit Configurations. 2.点击 ...

最新文章

  1. 源码级深挖AQS队列同步器
  2. vue 删除页面缓存_vue项目强制清除页面缓存的例子
  3. c语言二叉排序树的创建与查找,C语言实现二叉查找树的插入和删除操作问题求教...
  4. 各国家分析之 古埃及非洲经济
  5. HUE 提交Schedule 时区问题
  6. matlab 计算指北角,用MATLAB如何计算出矩形区域内的最大值?
  7. 从 CentOS 5.5 中精简出属于自己的专属Linux(二)
  8. 前端工作面试问题(上)---转
  9. Java调用动态库 缺点,Java调用动态库所需要关心的有关问题
  10. 数据结构第三篇——线性表的链式存储之单链表
  11. sql语句有没有怎么优化的空间,这条语句在我这里执行是死机
  12. os系统配置putty服务器,Mac 电脑安装putty
  13. 微信小程序的本地存储
  14. pxe启动找不到服务器,linux – PXE启动 – 在TFTP服务器上找不到内核
  15. itools苹果录屏大师_屏幕录制软件有哪些?找对合适录屏软件
  16. flink 部署模式和运行时架构(会话模式、单作业模式、应用模式,JobManager、TaskManager,YARN 模式部署以及运行时架构)
  17. FME转换CAD填充块文件为SHP,并正确显示颜色符号。
  18. VBA 贴片电阻名称转换
  19. control c linux命令,linux中Control+C是什么指令?使用什么命令可以给一个进程发出一个这样的指令?...
  20. 灭霸级——如何选择最适合你的Linux发行版

热门文章

  1. 60分钟吃掉嘎嘣脆的DeepCross模型
  2. 顺丰云服务器,基于华为云云原生解决方案,顺丰“快递+”这一项业务效率提升了48倍...
  3. Android内核层驱动程序UAF漏洞提权实例
  4. Command python setup.py egg_info failed with error code 1 in /private/var/folders/14/4hz051qx0wqd3
  5. 元素的隐藏和显示(v-show指令)
  6. vue中 给v-for渲染的元素动态添加移除类名
  7. 行走在数据库上的行癫(三)
  8. Installation failed due to: ‘‘cmd package install-create -r -t --user current --full --dont-kill -t
  9. 国产大飞机C919首飞成功 瑞星安全保驾护航
  10. 【U3D实战笔记】2DProject:RushMan