React Native动画效果

aa.gif

 

用Native 做右边的箭头旋转效果,应该是件很简答的事情,但是用React Native就需要花费时间考虑下。由于这个控件很常用,所以应该做个封装,任何时候调用都可以。
React Native做简单动画使用框架内Animated就可以了,更高级的动画,可以去Google上查。下面就是封装这个控件的关键点:
1.封装的控件代码

changeRorateValue (){
    this.changeValue+=180;
    Animated.timing(                  // Animate over time
        this.state.rotateValue,            // The animated value to drive
        {
          toValue: this.changeValue,                   // Animate to opacity: 1 (opaque)
          duration: 400,              // Make it take a while
        }
      ).start(()=>
    
       this.state.rotateValue.setValue(this.changeValue)
      ); 
}

render() {
    return (
        <TouchableOpacity onPress={
            ()=>{
                this.changeRorateValue()
            }} >
            <View style={{
                flexDirection: 'row',
                justifyContent: 'center',
                alignItems: 'center',
                backgroundColor: (this.props.setBackgroundColor) ? this.props.setBackgroundColor : this.props.defaultColor,
                width: (this.props.setWidth !== 0) ? this.props.setWidth : 60,
                height: (this.props.setHeight !== 0) ? this.props.setHeight : 20,
            }}
            >
            <Animated.Image style={[{width:16,height:9, transform: [{
                                        rotate: this.state.rotateValue
                                            .interpolate({ inputRange: [0, 360], outputRange: ['0deg', '360deg'] })
                                    }]}]}
                  source={require('../images/Down_Arrow.png')}
                 
                />
            </View>
        </TouchableOpacity>
    );
}

我们要定义一个外部可调用方法 changeRorateValue (),还需要 <TouchableOpacity onPress={
()=>{
this.changeRorateValue()
}} >留一个回调给外部控件使用
2.外部控件的调用

<TouchableOpacity style={{ flexDirection: 'row', justifyContent: 'flex-start', alignItems: 'center' }} onPress={() => {
this.test()
}}>

                    <Text style={{ flex: 1 }}>获奖项目</Text>
                    <Text>{this.state.isTurnDown ?"展开":"收起"}</Text>
                    <RotateArrowTwo ref='RotateArrow' setBackgroundColor='#999999' setWidth={16} setHeight={11} buttonName='ggsmifaf'
                        block={() => {
                            this.updateRotate()
                        }}
                    ></RotateArrowTwo>
                </TouchableOpacity>

updateRotate() {

    let result = !this.state.isTurnDown ;
    this.setState({
        isTurnDown:result
    })
    this.refs.RotateArrow.changeRorateValue();

}
这里注意使用的ref,这样的话能保证封装的这个控件的独立性。
如果需要源码的朋友,可以在评论区留言,如有不对之处,还望指正。

 



作者:Jesse1949
链接:https://www.jianshu.com/p/284515be27ca
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值