gpt4 book ai didi

react-native - 如何在 react-native 中更改动画中的文本?

转载 作者:行者123 更新时间:2023-12-03 16:41:15 24 4
gpt4 key购买 nike

我使用 Animated.Text 更改动画文本,但它无法正常工作

我还要求在动画中淡出旧文本并淡入新文本。

import React, { Component, PropTypes } from 'react';
import {
StyleSheet,
View,
Text,
Image,
Dimensions,
Animated
} from 'react-native';
import styles from './styles';

const win = Dimensions.get('window');

export default class Logo extends Component {
constructor(props) {
super(props);
this.tempText = new Animated.Value("Hello");
}

componentWillMount () {
Animated.timing(this.tempText, {
duration: 5000,
toValue: "New Text",
}).start();
};

render() {
return (
<View style={{flex:1}}>
<Animated.Text style={{color: "#9b9b9b"}}>
{this.tempText}
</Animated.Text>
</View>
);
}
}

实际输出获取 - 5 秒后更改文本但它不起作用。请帮助我。

最佳答案

无需使用 Animated 即可完成您要实现的目标实际上,Animated不适用于此特定用途。

替换文本可以用一个简单的变量来完成,文本更改可以由 setTimeout 触发。 .

动画用于更改数值,而不是文本值。这样想 - 如果更改应该在 5 秒间隔内发生,那么中间值是多少?

改为这样做:

export default class Logo extends Component {
constructor(props) {
super(props);
this.state = {text: "Hello"};
}

componentDidMount () {
setTimeout(() => this.setState({text: "New Text"}), 5000);
};

render() {
return (
<View style={{flex:1}}>
<Animated.Text style={{color: "#9b9b9b"}}>
{this.state.text}
</Animated.Text>
</View>
);
}
}

关于react-native - 如何在 react-native 中更改动画中的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45915619/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com