// // PurchaseBannerView.hpp // MyGameCpp // // Created by zhang yuntao on 2017/10/10. // // #ifndef PurchaseBannerView_hpp #define PurchaseBannerView_hpp #include "cocos2d.h" #include "extensions/cocos-ext.h" #include "PurchaseBannerCell.hpp" /* PageView类: 主要用途: 为了模仿iPhone的主屏幕page滑动页控件,使用场景:一些帮助页面使用。有的格子关卡页面等等。 使用说明: 1. 使用 PageView* create(cocos2d::Size& viewSize, sDotCfg & dotCfg, bool cachePages)来创建. 一般在ccb里设置好node的大小,然后得到大小传过来。cachePages:需不需要来缓存各个page,出于性能考虑,不缓存的话,通常只有三个页面存在 于内存中。来回迭代创建而已。 代码创建出来后一般设置 原点和锚点都为0, 再放在上面ccb设置的node上面。 然后设置delegate为自己类,且实现代理方法。 //下方小圆圈这个功能是可选择的,如果不需要,上面dotCfg随便创建一个对象传进去就成。 _initDotInd() //初始完毕后,使用下面函数刷新数据. reloadData(int pageCount, int defaultIndex = 0); 2. sDotCfg为iPhone主屏下方小圆圈配置。定义如下: struct sDotCfg { int sepWidth = 0; //每个小圆圈之间的间距 int yStart = 0; //默认是在viewSize的底部.也可以设置偏移. string highSp = ""; //当前页高亮与普通对应的图片. string normalSp = ""; }; 3. 实现三个代理方法: //创建cell页面。 virtual PageViewCell* pageCellAtIndex(PageView * pageView, int index) = 0; //点击到pagecell时的通知。 virtual void pageCellTouched (Touch * touch, PageViewCell * page) = 0; //当前页面发生改变时通知。manualSet来区分是不是调用setCurrentPageIndex()手动使当前页面刷新。 virtual void onPageChanged(int currentPageIndex, bool manualSet) = 0; 4. 自定义的 pageCell 必须承继于 PageViewCell,只是承继而已,别的不需要做什么。 */ class PurchaseBannerView; class PurchaseBannerViewDelegate { public: virtual PurchaseBannerCell* pageCellAtIndex(PurchaseBannerView* pageView, int index) = 0; virtual void pageCellTouched(Touch* touch, PurchaseBannerCell* page) = 0; virtual void onPageChanged(int currentPageIndex, bool manualSet) = 0; virtual void onPageTouchBegan(Touch* touch) = 0; }; class PurchaseBannerView : public Layer, public cocos2d::extension::ScrollViewDelegate { public: struct sDotCfg { int sepWidth = 0; int yStart = 0; string highSp = ""; string normalSp = ""; }; static PurchaseBannerView* create(Size& viewSize, sDotCfg& dotCfg, bool cachePages,bool isHor = true); void init(Size& viewSize, sDotCfg& dotCfg, bool cachePages, bool isHor); virtual void onEnter() override; virtual void onExit() override; void setDelegate(PurchaseBannerViewDelegate* delegate); void addBanner(PurchaseBannerCell* tmpNode); void reloadData(int pageCount, int defaultIndex = 0); void setCurrentPageIndex(int index); PurchaseBannerCell* getCurrentPage(); int getCurrentPageIdx(); void moveToPrePage(); void moveToNextPage(); void moveReferToPageIndex(int delta); void moveToFirstPage(); int getPageCount(); virtual void scrollViewDidScroll(cocos2d::extension::ScrollView* view) override; virtual bool onTouchBegan(Touch* touch, Event* unused_event) override; virtual void onTouchEnded(Touch* touch, Event* unused_event) override; private: PurchaseBannerViewDelegate* _pageViewDelegate = nullptr; int _currentPageIndex = 0; int _pageCount = 0; cocos2d::extension::ScrollView* _scrollView = nullptr; Size _viewSize = Size::ZERO; Vec2 _touchBeganPoint = Vec2::ZERO; int _touchedCount = 0; sDotCfg _dotCfg; bool _cachePages = false; bool _scrollViewMoved = false; bool _isHori = true; //dot bool _initedDot = false; vector _dotSpArr = {}; void _resizeView(); void _generatePage(); void _initDotInd(); void _initDotAdded(); void _updateIndSp(); void _removeExpirePage(); void _beginNewMoveToAnimation(float dt); void _notifyPageChanged(bool manualSet); void _autoSetBounceable(); bool isTouchedInNode(Node* node, Touch* touch, bool isAnotherSize = false, Vec2 anotherSize = Vec2::ZERO); }; #endif /* CPageView_hpp */