quick-cocos2d-x实现scrollview
来源:互联网 发布:俄勒冈州立大学 知乎 编辑:程序博客网 时间:2024/05/16 02:28
小注:本文使用的quick版本较低,所以很多控件需要自己写。在最新的quick版本里面已经集成了UIScrollView
这里实现的是一个简陋的scrollview,只支持单个的滑动。源码是根据网上一篇博客修改的,时间久远已经忘记出处,原作者可以私信我,我会加上原出处。谢谢
-- 说明:-- 主体为scrollview,但是不实现任何代理,-- 通过触摸层self.layerContainer来检测滑动-- 然后手动调整scrollView2DidScroll事件local UIScrollView = class("UIScrollView", function()return display.newLayer()end)function UIScrollView:ctor(param) -- self.fileName = param.fileName self.fileName = {"ui032_8_8.png", "ui032_8_9.png", "ui032_8_10.png", "ui001_2_01.png"} self:setConstance() self:createScrollView() self:addItems() self:addCircles()end-- 设置缩放系数function UIScrollView:getScale(content) -- 先判断是否有大得 if content.width > display.width or content.height > self.scrollHeight then if content.width > display.width and content.height > self.scrollHeight then -- 两边都大 local scaleW = display.width/content.width local scaleH = self.scrollHeight/content.height if scaleW < scaleH then return scaleW else return scaleH end elseif content.width > display.width then -- 只有宽大 return display.width/content.width else return self.scrollHeight/content.height end else -- 两边都小 local scaleW = display.width/content.width local scaleH = display.height/self.scrollHeight if scaleW < scaleH then return scaleW else return scaleH end endend-- 设置常量function UIScrollView:setConstance()local AdHeight = 400 if display.height < 800 then AdHeight = display.height/2 end self.scrollHeight = AdHeight - 16 self.scrollWidth = display.width self.cellNum = #self.fileName self.prevX = 0 self.endX = 0 -- 不让其自动滑动 self.bolTouchEnd = trueend-- 创建容器function UIScrollView:createContainer() self.layerContainer = display.newColorLayer(ccc4(255, 255, 0, 0)) self.layerContainer:setTouchEnabled(true) self.layerContainer:setPosition(ccp(0, 0)) self.layerContainer:setTouchEnabled(true) self.layerContainer:addTouchEventListener(function(event, x, y) return self:onCellCallback(event, x, y) end) self.widgetContainer = display.newColorLayer(ccc4(0, 255, 255, 0)) :align(display.LEFT_BOTTOM, 0, 0) :addTo(self.layerContainer) self.widgetContainer:setContentSize(CCSizeMake(self.scrollWidth, self.scrollHeight)) local w = self.cellNum*self.scrollWidth self.layerContainer:setContentSize(CCSizeMake(w, self.scrollHeight)) local preOffx = w local w1 = self.scrollWidth-w if w1 < 0 then w1 = 0 end self.layerContainer:setPositionX(w1)end-- 传感器function UIScrollView:onCellCallback(event, x, y)if event == "began" then self.prevX = x self.bolTouchEnd = false return true elseif event == "ended" then self.endX = x self.bolTouchEnd = true endend-- 添加栏目function UIScrollView:addItems()for i=1, self.cellNum do local scrollRight = self.scrollWidth*(i-1) local textureName = self.fileName[i] local cell = display.newSprite(textureName):addTo(self.widgetContainer) :align(display.CENTER, scrollRight+self.scrollWidth/2, self.scrollHeight/2) local size = cell:getContentSize() local scaleSize = self:getScale(size) cell:setScale(scaleSize) endend-- 设置滑动function UIScrollView:createScrollView() -- 设置容器相关内容 self:createContainer() -- 设置scrollView的相关操作self.scrollView = CCScrollView:create() self.scrollView:setContentSize(CCSizeMake(self.scrollWidth, self.scrollHeight)) -- 设置内容大小 self.scrollView:setViewSize(CCSizeMake(self.scrollWidth, self.scrollHeight)) -- 设置可见大小 self.scrollView:setPosition(ccp(0, 0)) -- 设置位置 self.scrollView:setContainer(self.layerContainer) -- 设置容器 self.scrollView:setDirection(kCCScrollViewDirectionHorizontal) -- 设置滑动方向 self.scrollView:setClippingToBounds(true) -- 设置裁剪 self.scrollView:setBounceable(false) -- 设置弹性效果 self.scrollView:setDelegate(this) -- 设置代理 self:addChild(self.scrollView) -- 实现代理 self.scrollView:registerScriptHandler(handler(self, self.scrollView2DidScroll), CCScrollView.kScrollViewScroll)end-- 添加小圆圈function UIScrollView:addCircles()local distance = 20local circlePosX = display.cx - (self.cellNum-1)/2*distance -- 初始化其它的for i=1, self.cellNum dolocal circle = display.newSprite("res/ui024_6_1.png"):addTo(self) :align(display.CENTER, circlePosX+(i-1)*distance, 60) circle:setScale(0.5) circle:setTag(100+i)end self:getChildByTag(101):setTexture(CCTextureCache:sharedTextureCache():addImage("ui024_6_2.png"))end-- 更新小圆圈function UIScrollView:updateCircles(x) for i=1,self.cellNum do self:getChildByTag(100+i):setTexture(CCTextureCache:sharedTextureCache():addImage("ui024_6_1.png")) end local index = x/(-self.scrollWidth) + 1 self:getChildByTag(100+index):setTexture(CCTextureCache:sharedTextureCache():addImage("ui024_6_2.png"))end-- 代理函数 function UIScrollView:scrollView2DidScroll() if self.bolTouchEnd == true then self.bolTouchEnd = false local offx = self.layerContainer:getPositionX() local minx = self.scrollWidth-self.cellNum*self.scrollWidth if offx < 0 and offx > minx then local item = -(math.abs(offx)%self.scrollWidth) local standerWidth = 0 if self.endX > self.prevX then -- 向右滑动 standerWidth = -self.scrollWidth*7.0/8 else -- 向左滑动 standerWidth = -self.scrollWidth/8 end if item <= standerWidth then -- 设置滑动 item = offx-item-self.scrollWidth else -- 没有滑动 item = offx-item end self:updateCircles(item) --scrollview滑动到指定的位置 self.scrollView:setContentOffset(ccp(item, 0), true) end endendreturn UIScrollView
4 0
- quick-cocos2d-x实现scrollview
- quick-cocos2d-x实现留言板
- quick-cocos2d-x 虚拟手柄实现实例
- cocos2d-x ScrollView实现帮助界面
- quick cocos2d-x
- 初窥 quick-cocos2d-x
- quick-cocos2d-x 笔记
- quick-cocos2d-x scheduler
- quick-cocos2d-x 加密
- 初窥 quick-cocos2d-x
- 初窥 quick-cocos2d-x
- Quick-cocos2d-x 组件
- quick-cocos2d-x踩坑记
- lua QUICK-COCOS2D-X
- quick-cocos2d-x教程10:实现血条效果。
- 基于Quick-cocos2d-x 2.2.3 的动态更新实现
- Cocos2d-X 中quick----lua的跳转场景的实现
- quick-cocos2d-x lua中碰撞检测的实现
- 软件安全测试之系统安全测试
- Android Wear开发可能用到的Android通知栏监听(仅适用4.3及以上)
- 小知识
- ArrayList和LinkedList的操作时间的差异
- Java中throw和throws的区别
- quick-cocos2d-x实现scrollview
- 欢迎使用CSDN-markdown编辑器
- 我的第一个汇编程序,使用分步运行-t是学习理解程序的好方法
- 图论_并查集
- Compare Version Numbers
- SIP 数据包分析与应用实例
- It is not safe to rely on the system's timezone settings
- android中服务的使用 与 Activity和Service的通讯
- python socket通信demo