ios - 在本机反应中使动画连续
问题描述
在这里,我正在为一个图标设置动画,但我需要它不断旋转。我能够让它旋转,但它停止了。我不确定如何更改它以使其不断旋转。它会旋转一点,但不会连续旋转。
import React, { useRef } from 'react';
import { Animated, Easing, StyleProp, TextStyle } from 'react-native';
import useMountEffect from '../../../common/hook/useMountEffect';
import Icon from '../../../common/icon/Icon/Icon';
interface Props {
name: string;
size?: number;
color?: string;
style?: StyleProp<TextStyle>;
variant?: 'solid' | 'brand' | 'light';
}
const SpinningIcon: React.FC<Props> = ({
name,
size,
color,
style,
variant,
}) => {
const spinValue = useRef(new Animated.Value(0));
useMountEffect(() => {
Animated.timing(spinValue.current, {
toValue: 1,
duration: 1000,
easing: Easing.linear,
useNativeDriver: true,
}).start();
});
const rotate = spinValue.current.interpolate({
inputRange: [0, 1],
outputRange: ['0deg', '360deg'],
});
return (
<Animated.View style={{ transform: [{ rotate }] }}>
<Icon
size={size}
color={color}
name={name}
style={style}
variant={variant}
/>
</Animated.View>
);
};
export default SpinningIcon;
解决方案
您可以使用Animated.loop()
import React, { useRef } from 'react';
import { Animated, Easing, StyleProp, TextStyle } from 'react-native';
import useMountEffect from '../../../common/hook/useMountEffect';
import Icon from '../../../common/icon/Icon/Icon';
interface Props {
name: string;
size?: number;
color?: string;
style?: StyleProp<TextStyle>;
variant?: 'solid' | 'brand' | 'light';
}
const SpinningIcon: React.FC<Props> = ({
name,
size,
color,
style,
variant,
}) => {
const spinValue = useRef(new Animated.Value(0));
useMountEffect(() => {
Animated.loop(
Animated.timing(spinValue.current, {
toValue: 1,
duration: 1000,
easing: Easing.linear,
useNativeDriver: true,
}),
).start();
});
const rotate = spinValue.current.interpolate({
inputRange: [0, 1],
outputRange: ['0deg', '360deg'],
});
return (
<Animated.View style={{ transform: [{ rotate }] }}>
<Icon
size={size}
color={color}
name={name}
style={style}
variant={variant}
/>
</Animated.View>
);
};
export default SpinningIcon;
推荐阅读
- javascript - 导出到 Excel 时如何以行格式显示项目数组?
- javascript - 在 Heroku 上本地运行的 React 应用程序崩溃:H10 “应用程序崩溃”
- python - Pandas,如何找到满足特定条件的行并将前一行保存在新的数据框中
- dynamic - 报告正文的 SSRS 动态宽度
- python - 切换用户后后台运行AppleScript
- symfony - 多对多错误:传递给 Doctrine\Common\Collections\ArrayCollection::__construct() 的参数 1 必须是数组类型,给定对象
- javascript - Fullpagejs 使我的导航移动停止工作
- angular - 在 Angular 6 上升级打字稿
- c# - 有没有办法在一段时间后(不是立即)在 for 循环中执行操作?
- c - 样式检查中的神秘额外文本