ScrollView
来源:互联网 发布:淘宝店铺出租转让 编辑:程序博客网 时间:2024/06/06 02:16
#ifndef __ScrollView_H__#define __ScrollView_H__#include "cocos2d.h"#include "cocos-ext.h"USING_NS_CC;USING_NS_CC_EXT;class ScrollView : public Layer{public:ScrollView();~ScrollView();CREATE_FUNC(ScrollView);bool init();bool TouchBegan(Touch*, Event*);void TouchEnded(Touch*, Event*);void TouchMoved(Touch*, Event*);float _lastContainerPos;void detectContainerMoving(float);void adjustScrollView();ScrollView* _view;};#endif
<pre name="code" class="html">#include "ScrollView.h"ScrollView::ScrollView(){}ScrollView::~ScrollView(){}bool ScrollView::init(){Layer::init();auto winSize = Director::getInstance()->getWinSize();// create时候的参数size,指所见的尺寸,不是内容的尺寸ScrollView* view = ScrollView::create(winSize);_view = view;addChild(view);// 设置内容Node* node = view->getContainer();for (int i = 0; i < 3; ++i){char buf[1024];sprintf(buf, "Images/background%d.png", i + 1);Sprite* sprite = Sprite::create(buf);node->addChild(sprite);sprite->setPosition(winSize.width / 2 + i*winSize.width, winSize.height/2);sprite->setTag(i);}//node->setContentSize(Size(winSize.width * 3, winSize.height));view->setContentSize(Size(winSize.width * 3, winSize.height));// 额外设置view->setDirection(ScrollView::Direction::HORIZONTAL);//view->setBounceable(false);/* 调整Scroll1. 增加触摸处理*/auto ev = EventListenerTouchOneByOne::create();ev->onTouchBegan = CC_CALLBACK_2(ScrollView::TouchBegan, this);ev->onTouchMoved = CC_CALLBACK_2(ScrollView::TouchMoved, this);ev->onTouchEnded = CC_CALLBACK_2(ScrollView::TouchEnded, this);this->_eventDispatcher->addEventListenerWithSceneGraphPriority(ev, this);return true;}bool ScrollView::TouchBegan(Touch*, Event*){return true;}void ScrollView::TouchEnded(Touch* touch, Event*){Vec2 delta = touch->getLocation() - touch->getStartLocation();if (delta.getLengthSq()<25){// 点击Node* node = _view->getContainer();Vec2 posInNode = node->convertTouchToNodeSpace(touch);for (int i = 0; i < 3; ++i){Node* sprite = node->getChildByTag(i);if (sprite->getBoundingBox().containsPoint(posInNode)){CCLOG("click i=%d", i);break;}}}else{// 当手指离开屏幕后,启用定时器来检测container是否停止运动_lastContainerPos = 10000000;schedule(schedule_selector(ScrollView::detectContainerMoving), 0.05f);}}void ScrollView::detectContainerMoving(float){#if 0static int i = 0;i++;if (i % 2 == 0)return;#endif// 获取当前Container的位置,判断位置和_lastContainerPos是否相等Node* node = _view->getContainer();float cur = node->getPositionX();if (cur == _lastContainerPos){// 开始调整unschedule(schedule_selector(ScrollView::detectContainerMoving));adjustScrollView();}else{_lastContainerPos = cur;}}void ScrollView::adjustScrollView(){auto winSize = Director::getInstance()->getWinSize();//获取所有的Container中的精灵,判断精灵位置到窗口中心的距离,找到最近的距离float distNearest = 1000000;int idxNearest = 0;float moveDist;Node* node = _view->getContainer();for (int i = 0; i < 3; ++i){Node* sprite = node->getChildByTag(i);Vec2 pos = sprite->getPosition();Vec2 posInWorld = node->convertToWorldSpace(pos);float dist = abs(posInWorld.x - winSize.width / 2);if (dist < distNearest){moveDist = posInWorld.x - winSize.width / 2;idxNearest = i;distNearest = dist;}else{break; }}// 移动ContainerMoveBy* move = MoveBy::create(distNearest/200, Vec2(-moveDist, 0));node->runAction(move);}void ScrollView::TouchMoved(Touch*, Event*){}
0 0
- ScrollView
- ScrollView
- scrollView
- ScrollView
- ScrollView
- ScrollView
- ScrollView
- ScrollView
- ScrollView
- ScrollView
- scrollView
- ScrollView
- ScrollView
- scrollview
- ScrollView
- ScrollView
- scrollView
- ScrollView
- WebService学习总结(一)——WebService的相关概念
- Swift枚举
- 字母重排
- Swift基础三
- 高效的MacBook工作环境配置
- ScrollView
- Oracle Meger Into
- 自学QT之QMovie播放gif(播放、暂停和继续)
- Android Studio bug:Gradle sync failed: Cause: org/gradle/api/publication/maven/internal/DefaultMaven
- 使用Latex模板时编译提示缺少{widetext.sty}
- Android 四大组件 —— 活动(活动的隐式跳转)
- vijos P1597 2的幂次方 题解(递归)
- shell批量将文件从dos格式转为Unix
- Codeforces 144D Missile Silos(SPFA最短路)