reactjs - React Native 动画图像旋转播放和暂停
问题描述
如何使图像停止旋转并从该位置开始旋转成为可能。谢谢你。
import React, {Component} from 'react';
import {StyleSheet, View, Animated, Image, Easing} from 'react-native';
export default class App extends Component<{}> {
constructor() {
super()
this.RotateValueHolder = new Animated.Value(0);
}
componentDidMount() {
this.StartImageRotateFunction();
}
StartImageRotateFunction() {
this.RotateValueHolder.setValue(0)
Animated.timing(
this.RotateValueHolder,
{
toValue: 1,
duration: 3000,
easing: Easing.linear
}
).start(() => this.StartImageRotateFunction())
}
render() {
const RotateData = this.RotateValueHolder.interpolate({
inputRange: [0, 1],
outputRange: ['0deg', '360deg']
})
return (
<View style={styles.container}>
<Animated.Image
style={{
width: 250,
height: 230,
transform: [{rotate: RotateData}]
}}
source={{uri: 'https://reactnativecode.com/wp-content/uploads/2017/10/Butterfly.png'}}/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
}
});
解决方案
最简单的方法是调用RotateData.stopAnimation()
小吃示例:https ://snack.expo.io/B1mPsdt_Q
相关代码:
componentDidMount() {
Animated.timing(
this.rotate,
{ toValue: 1, duration: 3000 }
).start()
this.timeout = setTimeout(() => {
this.rotate.stopAnimation(e => {
console.log('animation stopped', e)
})
}, 2000)
this.timeout = setTimeout(() => {
Animated.timing(
this.rotate,
{ toValue: 1, duration: 2000}
).start()
}, 4000)
}
如您所见,我已将旋转设置为运行 10 秒,并在 2 秒后超时停止。如果您打开零食,您会看到该物品根据需要停止旋转。4 秒后,动画将在调用时从当前位置恢复,无需任何添加配置。
同样重要的是要注意动画在当前值上停止,并stopAnimation()
接受回调作为参数,并且该回调将在动画停止时传递动画的值。
推荐阅读
- javascript - 停止变量干扰/变量范围
- ngxs - 将 NGSX 与路由器插件模块一起使用
- ceylon - 在不涉及 Null 的情况下满足 'Iterable' 接口
- wordpress - 视觉作曲家和用户角色问题
- forms - 我面临 Laravel 集体/html 类表单未找到错误
- javascript - 动态创建输入并检索其值以通过 Ajax 请求发送
- ruby - 优雅关闭 amqp(和/或)http 守护进程,(以及关于全局变量)
- nidaqmx - libnipalu.so 未能初始化所有 NI 相关命令的错误
- ios - geocodeaddressString 提供了错误的坐标
- c++ - protobuf,如何在我不详细了解的 protobuf 消息中遍历所有设置的字段?(C++)