首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >React native如何使文本和图像具有动画效果

React native如何使文本和图像具有动画效果
EN

Stack Overflow用户
提问于 2020-09-05 03:45:43
回答 1查看 456关注 0票数 0

大家好,我只想动画文字和图像。我写的这段代码是动画,在beginnig.When我更新状态,动画消失了,只是改变了图像和文本。我怎么才能修复它呢。

代码语言:javascript
复制
//the package I installed from npm
import * as Animatable from 'react-native-animatable';

//the images array changes by state
var arr=[{image:require('./assets/img1.png')},{image:require('./assets/img2.png')},{image:require('./assets/img3.png')}];

//The initial state
state = { 
  uri: require('./assets/img1.png'),
  message:"-",
  img_rank:0
};
代码语言:javascript
复制
//In onpress method of TocuhableOpacity
<TouchableOpacity
     onPress={() => {

const randomIndex = Math.floor(Math.random() * 3);

//state of message
this.setState({ message: "Act1" });

//state of image rank from the array
this.setState({ img_rank: randomIndex })
    ...

显示文本和图像

代码语言:javascript
复制
//it's showing the image with animation at the beginning
<Animatable.Image source={arr[this.state.img_rank].image}  animation="zoomInUp">      
</Animatable.Image>

//the text but it's not animating

<Animatable.Text   animation="zoomInUp">
   <Text>{this.state.message} </Text>
</Animatable.Text>

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2020-09-05 19:19:21

首先,将动画组件的ref放入类中的一个变量中,如animatedImageRef.

代码语言:javascript
复制
<Animatable.Image 
   ref={r => this.animatedImageRef = r}
   source={arr[this.state.img_rank].image}  
   animation={this.state.img_rank ? 'zoomInUp' : undefined}>      
</Animatable.Image>

然后,在您的onPress函数中,使用此引用再次为您的组件设置动画。

代码语言:javascript
复制
this.animatedImageRef.startAnimation(500, 0, () => { })
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63747210

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档