Lottie 反应原生
反应原生( Lottie) 组件iOS系统 财务报告和审定财务报表 机器人(中文(简体) )
洛蒂是图书馆的生态系统 用于解析 效果后的 Adobe 动画导出为 JSON 身体运动 使他们变成本地人!
设计师们第一次可以创造 和船舶 美丽的动画,没有工程师 艰苦地用手重新创作.
安装
V6中突破变化!
我们在第六版做了一些重大更新 可能会影响你目前的设置 为了了解这些变化的所有细节,请查看 更改日志。 。 。 .
保持知情,确保无缝地向最新版本过渡. 谢谢!
iOS和Android 软件
- 安装
lottie-react-native(最新数据):
yarn add lottie-react-native
转到您的 ios 文件夹并运行 :
pod install
使用量
Lottie可用声明方式:
import React from 'react';
import LottieView from 'lottie-react-native';
export default function Animation() {
return (
<LottieView source={require('../path/to/animation.json')} autoPlay loop />
);
}
此外,还有必要的API,有时比较简单.
import React, { useEffect, useRef } from 'react';
import LottieView from 'lottie-react-native';
export default function AnimationWithImperativeApi() {
const animationRef = useRef<LottieView>(null);
useEffect(() => {
animationRef.current?.play();
// Or set a specific startFrame and endFrame with:
animationRef.current?.play(30, 120);
}, []);
return (
<LottieView
ref={animationRef}
source={require('../path/to/animation.json')}
/>
);
}
Lottie的动画取景可以由React Independent动画或再生API来控制.
import React, { useEffect, useRef, Animated } from 'react';
import { Animated, Easing } from 'react-native';
import LottieView from 'lottie-react-native';
const AnimatedLottieView = Animated.createAnimatedComponent(LottieView);
export default function ControllingAnimationProgress() {
const animationProgress = useRef(new Animated.Value(0));
useEffect(() => {
Animated.timing(animationProgress.current, {
toValue: 1,
duration: 5000,
easing: Easing.linear,
useNativeDriver: false,
}).start();
}, []);
return (
<AnimatedLottieView
source={require('../path/to/animation.json')}
progress={animationProgress.current}
/>
);
}
改变地层颜色 :
注:这一特性可能无法在Android上正常工作。 我们很快会试着解决的.
import React from 'react';
import LottieView from 'lottie-react-native';
export default function ChangingColorOfLayers() {
return (
<LottieView
source={require('../path/to/animation.json')}
colorFilters={[
{
keypath: 'button',
color: '#F00000',
},
{
keypath: 'Sending Loader',
color: '#F00000',
},
]}
autoPlay
loop
/>
);
}
如果你想使用的话 .lottie 文件
你需要修改你的 metro.config.js 因此,通过添加 lottie 扩展至 assetExts 数组 :
const { getDefaultConfig, mergeConfig } = require('@react-native/metro-config');
const defaultConfig = getDefaultConfig(__dirname);
/**
* Metro configuration
* https://facebook.github.io/metro/docs/configuration
*
* @type {import('metro-config').MetroConfig}
*/
const config = {
resolver: {
assetExts: [...defaultConfig.resolver.assetExts, 'lottie'],
},
};
module.exports = mergeConfig(getDefaultConfig(__dirname), config);
为 Dot Lottie 文件设置游戏
在以下路径中创建文件 __mocks__/lottieMock.js 并添加以下代码:
module.exports = 'lottie-test-file-stub';
然后在你的 jest.config.js 文件 :
module.exports = {
...
moduleNameMapper: {
...,
'\\.(lottie)$': '<rootDir>/jest/__mocks__/lottieMock.js',
},
...
}
API 密码
你可以找到我们所有可用的道具和方法 API 文档。这些是最常见的:
| Prop | 说明 | 默认 |
|---|---|---|
source | 强制性 -动画的来源 可以被字符串作为本地资产引用,也可以与带有一个对象的远程引用 uri 也可以是动画中真正的JS对象 require('../path/to/animation.json')。 。 。 . | 无 |
style | 视图的样式属性, 如标准所期望的 View。 。 。 . | 你需要手动设置它。 参见此 拉动请求。 。 。 . |
loop | 显示动画是否循环的布尔旗 . | true |
autoPlay | 显示动画是否在挂载时自动启动的布尔旗 。 这只影响到必需的API. | false |
colorFilters | 用 KeyPath 来解析多层对象的阵列和一个新的颜色过滤值( 作为十六进制字符串) . | [] |
解决问题
并不是所有的"后效应"特性都由洛蒂支持. 如果您注意到有些图层或动画缺少检查 此列表 以确保它们得到支持.
再来
查看更多文档、 FAQ、 帮助、 示例和更多 空气bnb.io/地铁




