123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- //
- // IAPCardView.cpp
- // demo
- //
- // Created by Red_mini on 2024/10/25.
- //
- #include "IAPCardView.hpp"
- IAPCardView* IAPCardView::create(cocos2d::Size &viewSize, sDotCfg &dotCfg){
- // 创建 PageView 用于分页滑动卡片
- IAPCardView* ret = new IAPCardView();
- if (ret && ret->_init(viewSize, dotCfg)) {
- ret->autorelease();
- return ret;
- } else {
- delete ret;
- return nullptr;
- }
-
- }
- void IAPCardView::addCard(Node* card){
- if(card == nullptr) return;
-
- auto layout = ui::Layout::create();
- layout->setContentSize(_pageView->getContentSize());
- // 将卡片添加到布局并居中
- card->setPosition(Vec2(layout->getContentSize().width / 2, layout->getContentSize().height / 2));
- layout->addChild(card);
- // 添加到 PageView
- _pageView->addPage(layout);
- // 添加指示点
- float spacing = _dotCfg.sepWidth;
- auto newDot = Sprite::create(_dotCfg.normalSp);
- newDot->setPosition(Vec2(_indicators.size() * spacing, _dotCfg.yStart));
- _indicators.push_back(newDot);
- // 指示点添加到父节点中
- _indicatorNode->addChild(newDot);
-
- // 如果该卡片是添加的第一个
- if(_indicators.size() == 1){
- auto indicator = dynamic_cast<Sprite*>(_indicators[0]);
- if(indicator){
- indicator->setTexture(_dotCfg.hightSp); // 切换为选中图片
- }
- }
- // 调整指示点位置
- float indicatorNodeWidth = (_indicators.size() - 1) * spacing;
- _indicatorNode->setPosition(Vec2(_pageView->getContentSize().width / 2 - indicatorNodeWidth / 2, _dotCfg.yStart)); // 调整y轴位置
- }
- void IAPCardView::removeCard(int index){
- // 确保索引合法
- if (index < 0 || index >= _pageView->getPages().size()) {
- CCLOG("Invalid index: %d", index);
- return;
- }
-
- // 从 PageView 中移除指定页面
- _pageView->removePageAtIndex(index);
-
- // 从指示点数组中移除指定指示点,并从父节点中删除
- if (index < _indicators.size()) {
- auto indicator = _indicators[index];
- _indicatorNode->removeChild(indicator);
- _indicators.erase(_indicators.begin() + index);
- }
- // 更新剩余指示点的位置
- float spacing = _dotCfg.sepWidth;
- for (int i = 0; i < _indicators.size(); ++i) {
- _indicators[i]->setPosition(Vec2(i * spacing, _dotCfg.yStart));
- }
-
- }
- bool IAPCardView::_init(Size &viewSize, sDotCfg &dotCfg){
- _viewSize = viewSize;
- _dotCfg = dotCfg;
-
- // 创建 PageView 用于分页滑动卡片
- _pageView = ui::PageView::create();
- _pageView->setContentSize(viewSize);
- // 创建总指示器Node
- _indicatorNode = Node::create();
-
- // 设置 PageView 的分页切换效果(滑动自动切换)
- _pageView->setUsingCustomScrollThreshold(true); // 启用自定义滑动阈值
- _pageView->setCustomScrollThreshold(50); // 设置触发翻页的滑动距离阈值
- _pageView->setAutoScrollStopEpsilon(0.05f); // 提高自动滚动停止的精度
-
- this->addChild(_pageView);
- this->addChild(_indicatorNode);
-
-
- // 添加回调函数监听pageView变化
- _pageView->addEventListener([&](Ref* sender, ui::PageView::EventType type) {
- auto pageView = dynamic_cast<ui::PageView*>(sender);
- int currentPageIndex = pageView->getCurrentPageIndex();
-
- // 更新指示点的图片,当前页的指示点变为选中状态,其他保持未选中
- for (int i = 0; i < _indicators.size(); i++) {
- auto indicator = dynamic_cast<Sprite*>(_indicators[i]);
- if (i == currentPageIndex) {
- indicator->setTexture(_dotCfg.hightSp); // 切换为选中图片
- } else {
- indicator->setTexture(_dotCfg.normalSp); // 切换为未选中图片
- }
- }
- });
-
- return true;
- }
|