react-native 水波/声波 效果

效果图

作用

可以拿去用来制作按钮,类似录音或者寻找功能,效果不错

直接上代码:

import React from 'react';
import {
    View,
    StyleSheet,
    Animated
} from 'react-native';

export default class AnimatedSpread extends React.Component{
    static defaultProps = {
        ripple:3,       //同时存在的圆数量
        initialDiameter:150,
        endDiameter:350,
        initialPosition:{top:310,left:180},
        rippleColor:'#5BC6AD',
        intervals:500,      //间隔时间
        spreadSpeed:2000,      //扩散速度
    }
    static propTypes = {
        initialPosition:React.PropTypes.object.isRequired
    }
    constructor(props){
        super(props);
        let rippleArr = [];
        for(let i=0;i<props.ripple;i++) rippleArr.push(0);
        this.state = {
            anim:rippleArr.map(()=> new Animated.Value(0))
        }
        this.cancelAnimated = false;
        this.animatedFun = null;
    }
    startAnimation(){
        this.state.anim.map((val,index)=>val.setValue(0));
        this.animatedFun = Animated.stagger(this.props.intervals,this.state.anim.map((val)=>{
            return Animated.timing(val,{toValue:1,duration:this.props.spreadSpeed})
        }));
        this.cancelAnimated = false;
        this.animatedFun.start(()=>{if(!this.cancelAnimated) {this.startAnimation()}})
    }
    stopAnimation(){
        this.cancelAnimated = true;
        this.animatedFun.stop();
        this.state.anim.map((val,index)=>val.setValue(0));
    }
    render(){
        const {initialPosition,initialDiameter,endDiameter,rippleColor} = this.props;
        let r = endDiameter-initialDiameter;    // 直径变化量,top与left的变化是直径的一半
        let rippleComponent = this.state.anim.map((val,index)=>{
            return (
                <Animated.View key={"animatedView_"+index} style={[styles.spreadCircle,{backgroundColor:rippleColor},{
                    opacity:val.interpolate({
                                inputRange:[0,1],
                                outputRange:[1,0]
                            }),
                    height:val.interpolate({
                                inputRange:[0,1],
                                outputRange:[initialDiameter,endDiameter]
                            }),
                    width:val.interpolate({
                                inputRange:[0,1],
                                outputRange:[initialDiameter,endDiameter]
                            }),
                    top:val.interpolate({
                                inputRange:[0,1],
                                outputRange:[initialPosition.top - initialDiameter/2,initialPosition.top - initialDiameter/2 - r/2]
                            }),
                    left:val.interpolate({
                                inputRange:[0,1],
                                outputRange:[initialPosition.left - initialDiameter/2,initialPosition.left - initialDiameter/2 - r/2]
                            }),
                    }]}></Animated.View>
            )
        })
        return (
            <View>
                {rippleComponent}
            </View>
        )
    }
}

const styles = StyleSheet.create({
    spreadCircle:{
        borderRadius:999,
        position:'absolute',
    },
})
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 今天是周二,所以下午就得去上画画课了。我和妈妈出发直到达目的地。几乎没有说过一句话,也没有做任何别的事情,...
    王悦晨阅读 204评论 0 1
  • 自助餐就是以自助,自主,便捷,美味,随意取胜,客人可以根据自己的喜好和口味来选取自己喜欢的菜肴!但因与我们平时就餐...
    踏雪无痕Sunny阅读 1,853评论 0 0
  • 13年7月21日,我在微博写下,若我无欲无求,你拿什么打败我?17年2月16日,偶然看到,心里猛的触动了一下。我不...
    ChaosX阅读 215评论 0 0
  • MonoBehaviour.OnApplicationQuit() 当应用程序退出 MonoBehaviour.U...
    不倒翁_2878阅读 639评论 0 0