ReactNative幻灯片动画仅跳动一次

时间:2019-07-04 09:16:27

标签: reactjs react-native react-navigation react-animated react-animations

我正在尝试创建一个带有按钮的组件,按一下该按钮可以提供类似抽屉的功能。

我正在使用React的Animated动画,但是这种动画只发生一次。

我在代码中做错什么了吗?

还要检查附件图片

enter image description here

import React, { Component } from 'react'
import {
  View,
  Dimensions,
  Text,
  TouchableOpacity,
  StyleSheet,
  Animated,
  Easing
} from 'react-native'
import { connect } from 'react-redux'

const animationXStart = new Animated.Value(0)
const animationXEnd = new Animated.Value(-200)

class TestScreen extends Component {
  static navigationOptions = {
    header: null
  }

  constructor (props) {
    super(props)
    this.state = {
      drawerClose: true,
      visible: false,
      x: animationXStart,
      rotateY: '0deg'
    }
  }

  shouldComponentUpdate(nextProps, nextState) {
    console.log('should component update')
    if (this.state.x !== nextState.x && this.state.rotateY !== nextState.rotateY ) {
      return false
    }
  }

  toggleDrawer = () => {
    Animated.spring(this.state.x, {
      toValue: this.state.x === animationXStart ? animationXEnd : animationXStart,
      duration: 250,
      easing: Easing.linear(),
      useNativeDriver: true
    }).start()
    this.setState({
      visible: true,
      rotateY: this.state.rotateY === '0deg' ? '40deg' : '0deg',
      x: this.state.x === animationXStart ? animationXEnd : animationXStart,
      drawerClose: !this.state.drawerClose
    })
  }

  render () {
    return (
      <View style={styles.container}>
        <Animated.View style={[styles.child, {
          transform: [
            { perspective: 850 },
            { translateX: this.state.x },
            { rotateY: this.state.rotateY}
          ]
        }]}>
          <TouchableOpacity style={{flex: 1, height: '10%'}} onPress={this.toggleDrawer}>
            <Text style={{textAlign: 'right', color: 'black', fontSize: 20}}>
              Right ------
            </Text>
          </TouchableOpacity>
          <View style={{height: '90%'}} />
        </Animated.View>
      </View>
    )
  }
}

const styles = StyleSheet.create({
  container: {
    backgroundColor: '#14275e',
    flex: 1
  },
  child: {
    flex: 1,
    backgroundColor: 'green',
    marginTop: 30
  },
  childDrawer: {
    flex: 1,
    backgroundColor: 'green',
    scaleX: Dimensions.get('window').height * 0.60,
    marginTop: '20%',
    marginBottom: '20%',
    transform: [
      { perspective: 850 },
      { translateX: -Dimensions.get('window').width * 0.40 },
      { rotateY: '50deg'}
    ]
  }
})

const mapStateToProps = (state) => {
  return {
    materials: state.materials.payload,
    fetching: state.materials.fetching,
    localGovernmentArea: state.profile.localGovernmentArea
  }
}

const mapDispatchToProps = (dispatch) => {
  return {}
}

export default connect(mapStateToProps, mapDispatchToProps)(TestScreen)

0 个答案:

没有答案