cocosCreator动态调整pageView下面的标记indicator

pageView是我们在开发过程中经常使用到的一个组件,但是之前很少去动态修改过该属性的indicator,一般都是使用的默认的。今天产品要求实现一个动态效果,就是当页面左滑或者右滑时,下面的标记也会有一个左右滑动的效果(不知道怎么描述合适,大家进来看效果图自然明白)
参考链接,更详细https://lengmo714.top/18e312f1.html

效果如图:

请添加图片描述

实现思路:

1、监听滑动方向

  • 使用 page-turning 事件,结合 currentPagepreviousPage 的差值判断滑动方向。
    2、动态调整标记尺寸
  • 根据滑动方向,决定标记的拉伸动画从哪一侧开始,使用 UITransformcontentSize 属性进行拉伸。
    3、区分左右滑动
  • 左滑时标记从左侧向右拉伸,右滑时标记从右侧向左拉伸。

代码

import { tween, Size } from 'cc';

private previousPage: number = 0; // 记录上一次的页面索引
private currentPage: number = 0;  // 当前页面的索引

private updateIndicatorHighlight() {
    const indicator = this._view._PageC_banner.indicator;
    if (!indicator) return;

    const markers = indicator.node.children;
    const currentPage = this._view._PageC_banner.getCurrentPageIndex();

    for (let i = 0; i < markers.length; i++) {
        const sprite = markers[i].getComponent(Sprite);
        if (sprite) {
            let uiTransform = markers[i].getComponent(UITransform);

            if (i === currentPage) {
                // 根据滑动方向决定拉伸动画
                if (currentPage > this.previousPage) {
                    // 左滑:从左往右拉伸
                    Tool.setImgSprite(markers[i], "texture/illustrate/UI_Introduce_Point1", UIPnlillustrateLogic.bundleName);
                    uiTransform.setContentSize(this.markerWidth2, this.markerHeight); // 初始化为较小的尺寸
                    tween(uiTransform)
                        .to(0.2, { contentSize: new Size(this.markerWidth1, this.markerHeight) }, { easing: 'smooth' })
                        .start();
                } else {
                    // 右滑:从右往左拉伸
                    Tool.setImgSprite(markers[i], "texture/illustrate/UI_Introduce_Point1", UIPnlillustrateLogic.bundleName);
                    uiTransform.setContentSize(this.markerWidth2, this.markerHeight); // 初始化为较小的尺寸
                    tween(uiTransform)
                        .to(0.2, { contentSize: new Size(this.markerWidth1, this.markerHeight) }, { easing: 'smooth' })
                        .start();
                }
            } else {
                // 非当前页面,设置为未选中状态
                Tool.setImgSprite(markers[i], "texture/illustrate/UI_Introduce_Point2", UIPnlillustrateLogic.bundleName);
                tween(uiTransform)
                    .to(0.2, { contentSize: new Size(this.markerWidth2, this.markerHeight) }, { easing: 'smooth' })
                    .start();
            }
        }
    }

    // 更新 previousPage
    this.previousPage = currentPage;
}

代码说明

1、previousPage
通过记录上一个页面索引来判断滑动方向:

  • currentPage > previousPage:左滑。
  • currentPage < previousPage:右滑。
    2、动态调整尺寸
  • 使用 UITransformsetContentSize 方法动态调整标记尺寸
    3、平滑效果
  • 使用 tweensmooth 插值函数,让尺寸调整更加自然。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南锋1

您的打赏是我继续创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值