本篇介绍ScrollVIEw实现上拉刷新的功能,也可以实现下拉刷新。
数据来自Data.Json文件
首先显示3条数据,在ScrollVIEw滚动到底部回d时再显示新数据。
local MainScene = class("MainScene",cc.load("mvc").VIEwBase)function MainScene:ctor() self:enableNodeEvents() self.localData = {} self.index = 0endfunction MainScene:onEnter() self:createUI()endfunction MainScene:createUI() local path = "res/Data.Json" if cc.fileUtils:getInstance():isfileExist(path) then local fs = cc.fileUtils:getInstance():getStringFromfile(path) local t = Json.decode(fs) for i = 1,#t.data do table.insert(self.localData,t.data[i]) end self.index = self.index + #self.localData end self:setScroll()endfunction MainScene:setScroll() self.hT = {} local h = 40 + (#self.localData-1) * 20 self.sv = ccui.ScrollVIEw:create() self.sv:setContentSize(600,560) self.sv:setBounceEnabled(true) self.sv:setDirection(ccui.ScrollVIEwDir.vertical) self.sv:setClipPingEnabled(true) self.sv:setAnchorPoint(0,0) self.sv:setposition(180,50) self:addChild(self.sv) local total = 0 if self.index > 3 then total = 3 else total = self.index end self.index = self.index - total for i=1,total do local label = self.getWhiteLabel(self.localData[i],20,{wIDth = 600,height = 0},cc.TEXT_AlignmENT_left,cc.VERTICAL_TEXT_AlignmENT_CENTER) if i%2 == 0 then label:setTextcolor(cc.c4f(0,186,255,255)) end h = h + label:getContentSize().height table.insert(self.hT,h-40-(#self.localData-1) * 20) self.sv:addChild(label) end self.sv:setInnerContainerSize(cc.size(self.sv:getContentSize().wIDth,h)) local chs = self.sv:getChildren() for i=1,#chs do local label = chs[i] label:setAnchorPoint(0,0) local y = h - 20 - self.hT[i] - (i-1) * 20 label:setposition(0,y) end self.sv:addEventListener(function(sender,event) if event == ccui.ScrollvIEwEventType.bounceBottom then if self.index > 0 then self:showMessage("Loading........") self:beginLoad(total) else self:showMessage("no more message") end end end)endfunction MainScene:showMessage(str) local label = self.getRedLabel(str,28) local size = cc.Director:getInstance():getWinSize() self:addChild(label) label:setposition(size.wIDth/2,size.height/2) local moveto = cc.Moveto:create(3,cc.p(size.wIDth/2,size.height*1.2)) label:runAction(cc.Sequence:create(moveto,cc.RemoveSelf:create()))endfunction MainScene:beginLoad(total) local h = self.sv:getInnerContainerSize().height for i = 1,self.index do local label = self.getWhiteLabel(self.localData[total+i],cc.VERTICAL_TEXT_AlignmENT_CENTER) if (total+i)%2 == 0 then label:setTextcolor(cc.c4f(0,h-40-(#self.localData-1) * 20) self.sv:addChild(label) end self.sv:setInnerContainerSize(cc.size(self.sv:getContentSize().wIDth,h)) self:reloadData()endfunction MainScene:reloadData() local chs = self.sv:getChildren() local h = self.sv:getInnerContainerSize().height for i=1,y) end self:finishLoad()endfunction MainScene:finishLoad() self.sv:scrolltopercentVertical(80,0.5,true) self.index = 0endfunction MainScene.getWhiteLabel(text,FontSize,dimensions,hAlignment,vAlignment,mb) return MainScene.getTextLabel(text,FontSize,cc.c4f(255,255),dimensions,hAlignment,vAlignment,mb)endfunction MainScene.getBlueLabel(text,Fontsize,Fontsize,cc.c4f(0,mb)endfunction MainScene.getRedLabel(text,0,84,mb)endfunction MainScene.getTextLabel(text,color,mb) local ttfConfig = {} ttfConfig.FontfilePath = "res/Fonts/DFYUANW7-GB2312.ttf" ttfConfig.FontSize = Fontsize ttfConfig.glyphs = cc.GLYPHColLECTION_DYNAMIC ttfConfig.customGlyphs = true ttfConfig.distanceFIEldEnabled = false local textLabel = cc.Label:createWithTTF(ttfConfig,text) textLabel:setlineBreakWithoutSpace(true) if color then textLabel:setTextcolor(color) end if dimensions then textLabel:setDimensions(dimensions.wIDth,dimensions.height) end if hAlignment then textLabel:setAlignment(hAlignment) end if vAlignment then textLabel:setVerticalAlignment(vAlignment) end -- 描边 if not mb then textLabel:enableOutline(cc.c4b(0,2) end return textLabel endreturn MainScene
运行效果:
以上是内存溢出为你收集整理的ScrollView模拟上拉刷新全部内容,希望文章能够帮你解决ScrollView模拟上拉刷新所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)