2016-07-26 52 views
3

我有一個包含一系列圖像的動畫:image01.pngimage02.pngimage03.png等。如何讓這些動畫在React Native上持續動畫?React Native上的動畫圖像序列

+0

您可以檢出動畫和佈局動畫反應原生提供。 –

回答

4

你可以與圖書館嘗試:

首先是更加更加高效,二是純JavaScript。另一種方式是實現它自己的方式,喜歡這裏:https://github.com/facebook/react-native/issues/9280

它應該看起來像這樣

export default class Animation extends Component { 
    constructor(props) { 
     super(props); 
     this.images = [ 
      require('./img_01.png'), 
      require('./img_02.png'), 
      require('./img_03.png'), 
     ]; 
     this.next = this.next.bind(this); 
     this.state = {index: 0}; 
    } 

    componentDidMount() { 
     this.next(); 
    } 

    next() { 
     setTimeout(() => { 
      this.setState({index: (this.state.index+1)%3}); 
      this.next(); 
     }, 300); 
    } 

    render() { 
     return (
      <Image 
       source={this.images[this.state.index]} 
       style={styles.image} 
      /> 
     ) 
    } 
}