xcode 使用 cocos2d-x 3.6 实现滚动视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30903933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Implement a scrollview with cocos2d-x 3.6
提问by ctapp1
wondering if anyone knows how to implement a scroll view in cocos2d-x 3.6 (C++). All the tutorials I have found are for earlier cocos2d-x versions.
想知道是否有人知道如何在 cocos2d-x 3.6 (C++) 中实现滚动视图。我找到的所有教程都是针对早期的 cocos2d-x 版本的。
Thanks
谢谢
I added my code below, I can get the grey scrollview box to show but it cant be scrolled and the buttons don't appear on it:
我在下面添加了我的代码,我可以显示灰色滚动视图框,但它无法滚动并且按钮没有出现在它上面:
header files: "CocosGUI.h" and "cocos-ext.h"
头文件:“CocosGUI.h”和“cocos-ext.h”
//add scroll view
Size scollFrameSize = Size(visibleSize.width, visibleSize.height/4);
auto scrollView = cocos2d::ui::ScrollView::create();
scrollView->setContentSize(scollFrameSize);
scrollView->setBackGroundColorType(cocos2d::ui::Layout::BackGroundColorType::SOLID);
scrollView->setBackGroundColor(Color3B(200, 200, 200));
scrollView->setPosition(Point(0, visibleSize.height/1.5));
scrollView->setDirection(cocos2d::ui::ScrollView::Direction::HORIZONTAL);
scrollView->setBounceEnabled(true);
scrollView->setTouchEnabled(true);
auto containerSize = Size(scollFrameSize.width*2, scollFrameSize.height*2);
scrollView->setInnerContainerSize(containerSize);
this->addChild(scrollView);
auto button1 = cocos2d::ui::Button::create();
button1->setColor(Color3B(250, 200, 50));
button1->setTouchEnabled(true);
button1->setContentSize(Size(100, 100));
button1->setPosition(Point(containerSize.width / 4, containerSize.height / 2));
scrollView->addChild(button1);
auto button2 = cocos2d::ui::Button::create();
button2->setColor(Color3B(250, 200, 50));
button1->setContentSize(Size(100, 100));
button2->setTouchEnabled(true);
button2->setPosition(Point(containerSize.width / 8, containerSize.height / 2));
scrollView->addChild(button2);
采纳答案by ctapp1
I figured it out, It was scrolling but I added my buttons wrong. For anyone who is interested add a button like this
我想通了,它正在滚动,但我添加了错误的按钮。对于任何有兴趣的人添加这样的按钮
auto button1 = ui::Button::create();
button1->setTouchEnabled(true);
button1->ignoreContentAdaptWithSize(false);
button1->setContentSize(Size(100, 100));
button1->loadTextures("pic1.png", "pic2.png");
button1->setPosition(Point(containerSize.width / 8, containerSize.height / 2));
scrollView->addChild(button1);
回答by yangguang1029
Size scroll_size = Director::getInstance()->getWinSize();
Size container_size = Size(scroll_size.width * 2, scroll_size.height);
Layer* container = Layer::create();
container->setContentSize(container_size);
ScrollView* scroll = ScrollView::create(scroll_size, container);
mScroll->setDelegate(this);
mScroll->setDirection(ScrollView::Direction::HORIZONTAL);
1.size of container should be larger than size of scrollview
1.容器的大小应大于滚动视图的大小
2.add child to container, not scrollview
2.将子项添加到容器,而不是滚动视图
3.implement ScrollViewDelegate
3.实现ScrollViewDelegate