IAPCardView.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // IAPCardView.cpp
  3. // demo
  4. //
  5. // Created by Red_mini on 2024/10/25.
  6. //
  7. #include "IAPCardView.hpp"
  8. IAPCardView* IAPCardView::create(cocos2d::Size &viewSize, sDotCfg &dotCfg){
  9. // 创建 PageView 用于分页滑动卡片
  10. IAPCardView* ret = new IAPCardView();
  11. if (ret && ret->_init(viewSize, dotCfg)) {
  12. ret->autorelease();
  13. return ret;
  14. } else {
  15. delete ret;
  16. return nullptr;
  17. }
  18. }
  19. void IAPCardView::addCard(Node* card){
  20. if(card == nullptr) return;
  21. auto layout = ui::Layout::create();
  22. layout->setContentSize(_pageView->getContentSize());
  23. // 将卡片添加到布局并居中
  24. card->setPosition(Vec2(layout->getContentSize().width / 2, layout->getContentSize().height / 2));
  25. layout->addChild(card);
  26. // 添加到 PageView
  27. _pageView->addPage(layout);
  28. // 添加指示点
  29. float spacing = _dotCfg.sepWidth;
  30. auto newDot = Sprite::create(_dotCfg.normalSp);
  31. newDot->setPosition(Vec2(_indicators.size() * spacing, _dotCfg.yStart));
  32. _indicators.push_back(newDot);
  33. // 指示点添加到父节点中
  34. _indicatorNode->addChild(newDot);
  35. // 如果该卡片是添加的第一个
  36. if(_indicators.size() == 1){
  37. auto indicator = dynamic_cast<Sprite*>(_indicators[0]);
  38. if(indicator){
  39. indicator->setTexture(_dotCfg.hightSp); // 切换为选中图片
  40. }
  41. }
  42. // 调整指示点位置
  43. float indicatorNodeWidth = (_indicators.size() - 1) * spacing;
  44. _indicatorNode->setPosition(Vec2(_pageView->getContentSize().width / 2 - indicatorNodeWidth / 2, _dotCfg.yStart)); // 调整y轴位置
  45. }
  46. void IAPCardView::removeCard(int index){
  47. // 确保索引合法
  48. if (index < 0 || index >= _pageView->getPages().size()) {
  49. CCLOG("Invalid index: %d", index);
  50. return;
  51. }
  52. // 从 PageView 中移除指定页面
  53. _pageView->removePageAtIndex(index);
  54. // 从指示点数组中移除指定指示点,并从父节点中删除
  55. if (index < _indicators.size()) {
  56. auto indicator = _indicators[index];
  57. _indicatorNode->removeChild(indicator);
  58. _indicators.erase(_indicators.begin() + index);
  59. }
  60. // 更新剩余指示点的位置
  61. float spacing = _dotCfg.sepWidth;
  62. for (int i = 0; i < _indicators.size(); ++i) {
  63. _indicators[i]->setPosition(Vec2(i * spacing, _dotCfg.yStart));
  64. }
  65. }
  66. bool IAPCardView::_init(Size &viewSize, sDotCfg &dotCfg){
  67. _viewSize = viewSize;
  68. _dotCfg = dotCfg;
  69. // 创建 PageView 用于分页滑动卡片
  70. _pageView = ui::PageView::create();
  71. _pageView->setContentSize(viewSize);
  72. // 创建总指示器Node
  73. _indicatorNode = Node::create();
  74. // 设置 PageView 的分页切换效果(滑动自动切换)
  75. _pageView->setUsingCustomScrollThreshold(true); // 启用自定义滑动阈值
  76. _pageView->setCustomScrollThreshold(50); // 设置触发翻页的滑动距离阈值
  77. _pageView->setAutoScrollStopEpsilon(0.05f); // 提高自动滚动停止的精度
  78. this->addChild(_pageView);
  79. this->addChild(_indicatorNode);
  80. // 添加回调函数监听pageView变化
  81. _pageView->addEventListener([&](Ref* sender, ui::PageView::EventType type) {
  82. auto pageView = dynamic_cast<ui::PageView*>(sender);
  83. int currentPageIndex = pageView->getCurrentPageIndex();
  84. // 更新指示点的图片,当前页的指示点变为选中状态,其他保持未选中
  85. for (int i = 0; i < _indicators.size(); i++) {
  86. auto indicator = dynamic_cast<Sprite*>(_indicators[i]);
  87. if (i == currentPageIndex) {
  88. indicator->setTexture(_dotCfg.hightSp); // 切换为选中图片
  89. } else {
  90. indicator->setTexture(_dotCfg.normalSp); // 切换为未选中图片
  91. }
  92. }
  93. });
  94. return true;
  95. }