如何使用React Native创建发光动画

时间:2019-06-10 18:19:52

标签: javascript css reactjs react-native css-animations

我想为按钮和图像创建发光的动画。我想知道是否可以使用本机动画来实现这种发光效果。我有这种CSS样式,但我认为它不能与react native一起使用。关于如何在react-native或react.js中为按钮或图像执行此操作的任何想法?

.glow {
  font-size: 80px;
  color: #fff;
  text-align: center;
  -webkit-animation: glow 1s ease-in-out infinite alternate;
  -moz-animation: glow 1s ease-in-out infinite alternate;
  animation: glow 1s ease-in-out infinite alternate;
}

@-webkit-keyframes glow {
  from {
    text-shadow: 0 0 10px #fff, 0 0 20px #fff, 0 0 30px #e60073, 0 0 40px #e60073, 0 0 50px #e60073, 0 0 60px #e60073, 0 0 70px #e60073;
  }
  to {
    text-shadow: 0 0 20px #fff, 0 0 30px #ff4da6, 0 0 40px #ff4da6, 0 0 50px #ff4da6, 0 0 60px #ff4da6, 0 0 70px #ff4da6, 0 0 80px #ff4da6;
  }
}

1 个答案:

答案 0 :(得分:0)

我认为在react native中无法使用关键帧,因此我实现了自己的解决方案:

this.state = {
      isBlinking: false
    };
this.blinkingInterval = false;

componentWillUnmount() {
    clearInterval(this.blinkingInterval);
    this.blinkingInterval = false;
  }

 componentDidMount() {
   if (!this.blinkingInterval) {
      this.blinkingInterval = setInterval(() => {
       this.setState({
          isBlinking: !this.state.isBlinking
         });
       }, 800);
    }
  }

在渲染中:

 <Text style={this.state.isBlinking && styles.textGlowing}>Hello World</Text>

在css文件中:

textGlowing: {
    textShadowColor: 'rgba(0, 0, 0, 0.75)',
    textShadowOffset: {width: -1, height: 1},
    textShadowRadius: 15
  }