react native 触摸的响应方法以及过程(二)

本文介绍如何在React Native中实现动画效果,包括定义动画响应组件、设置动画响应函数及触发方式。通过触摸遥控器按键触发动画,实现透明度变化等效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

动画的响应方法及过程

1、首先导入Animated

import {
    Animated
} from 'react-native';

2、定义动画响应组件

render: function () {
    let keyBoardImg = this.keyBoardImg();
    return (
        <View style={styles.wrap}>
            <View style={[styles.touchContainer, {backgroundColor: '#e2eaea'}]}
                {...this._panResponder.panHandlers}>
                <Animated.Image
                    style={[styles.middleContainer_BG, {opacity: this.bounceValue}]}
                    source={{uri: keyBoardImg}}/>
            </View>
        </View>
    );
}

3、定义动画响应函数

actAnimationStart: function () {
    this.console("actAnimationStart");
    // 可选的基本动画类型: spring, decay, timing
    this.bounceValue.setValue(0);
    Animated.timing(
        /**
         * 将`bounceValue`值动画化的意思是,在以Animated为前缀名的组件中,比如此例中的Animated.Image,
         * 将`bounceValue`值所赋予的样式就是会跟随`bounceValue`值的变化而改变样式的,
         * `bounceValue`值的改变在Animated.timing()函数中改变,此例中`bounceValue`值将从0变化至1,变化的时间为200ms
         * 也就是说,以Animated为前缀名的组件Image(Animated.Image),将在200ms的时间内,透明度从0变化至1
         * */
        this.bounceValue,// 将`bounceValue`值动画化
        {
            toValue: 1,// 将其值以动画的形式改到一个较小值
            duration: 180,
            easing: Easing.ease
        }
    ).start(() => {
            this.actAnimationEnd()
        });
},
actAnimationEnd: function () {
    this.console("actAnimationEnd");
    // 可选的基本动画类型: spring, decay, timing
    this.bounceValue.setValue(1);
    Animated.timing(
        this.bounceValue,// 将`bounceValue`值动画化
        {
            toValue: 0,// 将其值以动画的形式改到一个较小值
            duration: 300,
            easing: Easing.ease
        }
    ).start();
}
4、在合适的地方触发动画响应函数
keyPressIn: function (keyCode) {
    let _keyString = '';
    switch (keyCode) {
        case 2://UP
            this.setState({keyBoard: 'up'});
            this.actAnimationStart();
            _keyString = 'UP';
            break;
        case 3://DOWN
            this.setState({keyBoard: 'down'});
            this.actAnimationStart();
            _keyString = 'DOWN';
            break;
        case 4://LEFT
            this.setState({keyBoard: 'left'});
            this.actAnimationStart();
            _keyString = 'LEFT';
            break;
        case 5://RIGHT
            this.setState({keyBoard: 'right'});
            this.actAnimationStart();
            _keyString = 'RIGHT';
            break;
        case 6://CENTER
            this.setState({keyBoard: 'center'});
            this.actAnimationStart();
            _keyString = 'CENTER';
            break;
        default:
            break;
    }
}

该示例代码为触摸遥控器解决方案的一部分代码,完整示例代码将会在react native触摸遥控器解决方案提供。

react native 触摸的响应方法以及过程(三)—— 手势的上下左右判断的方法


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值