android - 在本机反应中为图像设置动画
问题描述
我对原生反应完全陌生,我正在尝试为图像制作动画,任何建议都会有所帮助!
import React, { Component } from 'react';
import { Animated, Easing } from 'react-native';
import FontAwesome5Pro from 'react-native-vector-icons/FontAwesome5Pro';
class AnimateIcon extends Component {
spinValue = new Animated.Value(0);
componentDidMount() {
this.spin();
}
spin = () => {
this.spinValue.setValue(0);
Animated.timing(this.spinValue, {
toValue: 1,
duration: 1000,
easing: Easing.linear,
useNativeDriver: true,
}).start(() => this.spin());
};
render() {
const { style, children } = this.props;
const rotate = this.spinValue.interpolate({
inputRange: [0, 1],
outputRange: ['0deg', '360deg'],
});
return (
<Animated.View style={{ transform: [{ rotate }] }}>
<FontAwesome5Pro style={style}>{children}</FontAwesome5Pro>
</Animated.View>
);
}
}
export default AnimateIcon;
我收到以下错误:
“类型 'Readonly<{}> 和 Readonly<{ children?: ReactNode; }> 上不存在属性 'style'
解决方案
因为您使用的是 TypeScript,所以您需要定义您props
的props.style
. 此外,您需要记住保留您Animated.Value
的 a state
,因为 React 使用更改state
来确定何时重新渲染。
import React, { Component } from 'react'
import { Animated, Easing, ViewStyle } from 'react-native'
import FontAwesome5Pro from 'react-native-vector-icons/FontAwesome5Pro'
interface AnimateIconProps {
style?: ViewStyle
}
class AnimateIcon extends Component<AnimateIconProps, { spinValue?: Animated.Value }> {
constructor (props: AnimateIconProps) {
super(props)
this.state = {
spinValue: new Animated.Value(0),
}
}
componentDidMount () {
this.spin()
}
spin = () => {
this.state.spinValue.setValue(0)
Animated.timing(this.state.spinValue, {
toValue: 1,
duration: 1000,
easing: Easing.linear,
useNativeDriver: true,
}).start(() => this.spin())
}
render () {
const { style, children } = this.props
const rotate = this.state.spinValue.interpolate({
inputRange: [0, 1],
outputRange: ['0deg', '360deg'],
})
return (
<Animated.View style={{ transform: [{ rotate }] }}>
<FontAwesome5Pro style={style}>{children}</FontAwesome5Pro>
</Animated.View>
)
}
}
export default AnimateIcon
推荐阅读
- python - 如何在 CVXPY 中指定非线性约束?
- laravel - Laravel - :“in_array()期望参数2是数组,对象给定”在离开请求中
- java - 为什么我不能从 PriorityQueue 中删除 peek() 获取的元素?
- javascript - Wheelnav.js,默认 NavItemText 未选择
- c# - Azure 功能无法访问连接的 WCF 服务
- c++ - 如何编写 gmock 测试,使其不调用夹具类的构造函数
- python - 如何使用python cgi?
- asp.net-core - 写入响应正文时为空标题和响应正文
- nuxt.js - 如何在 NuxtJS 项目中使用 EditorJS?
- asp.net - 目标 Web 配置文件中的 XDT 转换