cocos2d-X里提供的CCScrollView里并没有提供滚动条和滚动条背景,我对它进行了一些修改,所用的cocos2d-x 版本为3.0
使用方法
CCBarScrollView* scrollView = CCBarScrollView::create();
scrollView->initWithViewSize((Size(280.0f,150.0f)));
scrollView->setContentSize(Size(280,150*10.0f));
scrollView->setDirection(cocos2d::extension::ScrollView::Direction::VERTICAL);
scrollView->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
scrollView->setBounceable(true);
this->addChild(scrollView,0);
auto bgImage = Scale9Sprite::create("bg_pd_item.png");
auto barImage = Scale9Sprite::create("bg2.png");
scrollView->setBarImage(bgImage, barImage);
// scrollView->setAutoHidden(true);
for (int i = 0; i < 50; i++) {
auto sprite1 = Sprite::create("CloseNormal.png");
sprite1->setPosition(20, 20+i*40);
scrollView->addChild(sprite1);
}
控件代码: 点击打开链接
class CCBarScrollView:public ScrollView
{
public:
static CCBarScrollView* create(Size size, Node* container = NULL);
static CCBarScrollView* create();
using Node::addChild;
void setBarImage(Scale9Sprite* barBg,Scale9Sprite* barImage);
virtual void addChild(Node * child, int zOrder, int tag) override;
void setContentOffset(Point offset, bool animated = false);
void setAutoHidden(bool autoHidden); //设置是否自动隐藏
virtual bool onTouchBegan(Touch *touch, Event *event);
virtual void onTouchEnded(Touch *touch, Event *event);
protected:
void deaccelerateScrolling(float dt);
void updateBarPos();
Scale9Sprite* ba