cc.ScrollView.ClassName = "ScrollView" function cc.ScrollView:saveToXmlNode(xmlNode) cc.Layout.saveToXmlNode(self , xmlNode); xmlNode.BounceEnabled = self:isBounceEnabled(); xmlNode.Direction = self:getDirection(); xmlNode.InnerContainerSize = self:getInnerContainerSize(); end function cc.ScrollView:loadFromXmlNode(xmlNode) cc.Layout.loadFromXmlNode(self , xmlNode); self:setBounceEnabled(xmlNode.BounceEnabled); self:setDirection(xmlNode.Direction); self:setInnerContainerSize(xmlNode.InnerContainerSize); end function cc.ScrollView:extend(node) cc.Layout:extend(node); node:setName("ScrollView"); local vBar = node:getVBar(); vBar:setBackGroundImage("jindutiao_01.png" , cc.TextureResType.plistType); vBar:setProgressImage("res/ui/zy_tongyong/jindutiao_1.png"); local hBar = node:getHBar(); hBar:setBackGroundImage("jindutiao_01.png" , cc.TextureResType.plistType); hBar:setProgressImage("res/ui/zy_tongyong/jindutiao_1.png"); -- 0.1Ӣէ node:setChildFocusCancelOffset(cc.Device:getDPI() * 0.1); end function cc.ScrollView:hideAllBar() local vBar = self:getVBar(); vBar:setVisible(false) local hBar = self:getHBar(); hBar:setVisible(false) end function cc.ScrollView:jumpToTopOnSizeChanged() local function onEvent(ui, eventType) if eventType == 3 then self:jumpToTop(); end end self:getInnerContainer():setWidgetEventListener(onEvent) end function cc.ScrollView:jumpToBottomOnSizeChanged() local function onEvent(ui, eventType) if eventType == 3 then self:jumpToBottom(); end end self:getInnerContainer():setWidgetEventListener(onEvent) end -- ܱփؓޚ֣քλ׃љؖ҈ìࠉӔԃ4ַԃjumpToPercentHorizontalìscrollToֈگ˽ì׵ܘքˇ{x = [0-100] , y = [0-100]}; function cc.ScrollView:getCenterPercent(childNode) local world = childNode:getWorldTranslation(); local size = childNode:getContentSize(); local anchor = childNode:getAnchorPoint(); world.x = world.x + (size.width) / 2; world.y = world.y + (size.height) / 2; local nodePos = self:getInnerContainer():convertToNodeSpace(world); local percent = self:getPercent(nodePos); return cc.p(percent.x * 100 , percent.y * 100); end -- Ƀ֢ٶޚ֣ߓא function cc.ScrollView:scrollXToCenter(childNode , scrollTime) local x = self:getCenterPercent(childNode).x; --print("xxxx" , x , childNode:getPosition().x); self:scrollToPercentHorizontal(x , scrollTime , true); end -- Ƀ֢ٶޚ֣ߓא function cc.ScrollView:scrollYToCenter(childNode , scrollTime) self:scrollToPercentVertical(self:getCenterPercent(childNode).y , scrollTime , true); end -- 获得子节点的位置百分比,可以用来调用jumpToPercentHorizontal,scrollTo等函数,返回的是{x = [0-100] , y = [0-100]}; function cc.ScrollView:getChildPercent(childNode) local world = childNode:getPosition(); local nodeSize = childNode:getContentSize(); local anchor = childNode:getAnchorPoint(); world.x = world.x - (nodeSize.width) * anchor.x; world.y = world.y - (nodeSize.height) * anchor.y; local innerSize = self:getInnerContainerSize(); local size = self:getSize(); -- scrollView最下面是0位置而百分比上面是0 world.y = innerSize.height - world.y - nodeSize.height local perscent = cc.p(0,0) local remainSize = cc.size(innerSize.width - size.width, innerSize.height - size.height) if remainSize.width == 0 then perscent.x = 0 else perscent.x = world.x / remainSize.width end if remainSize.height == 0 then perscent.y = 0 else perscent.y = world.y / remainSize.height end if perscent.x > 1 then perscent.x = 1 end if perscent.y > 1 then perscent.y = 1 end return cc.p(perscent.x * 100 , perscent.y * 100); end -- 滚动到节点所在的位置 function cc.ScrollView:scrollXToChild(childNode , scrollTime) local x = self:getChildPercent(childNode).x; --print("xxxx" , x , childNode:getPosition().x); self:scrollToPercentHorizontal(x , scrollTime , true); end -- 滚动到节点所在的位置 function cc.ScrollView:scrollYToChild(childNode , scrollTime) self:scrollToPercentVertical(self:getChildPercent(childNode).y , scrollTime , true); end function cc.ScrollView:createNode() local layer = cc.ScrollView:create(); return layer; end -- ʨ׃Ĭɏֵ function cc.ScrollView:setDefaults() cc.Layout.setDefaults(self); self:setBackGroundColor(cc.c3b(255,150,100)); self:setBounceEnabled(true); self:loadDefaults(); -- ĬɏѨҪЬӦtouch˂ݾ self:setTouchEnabled(true) end function cc.ScrollView:loadDefaults() self:addProtectedChild(self:createTouchSizeDrawer()); end -- ԴݨԴݨڶ֯ȸԲ function cc.ScrollView:createTouchSizeDrawer() local glNode = gl.glNodeCreate() glNode:setAnchorPoint(cc.p(0, 0)) local function primitivesDraw(transform, transformUpdated) local isStencilEnabled = gl.isEnabled(gl.STENCIL_TEST) if isStencilEnabled then gl.disable(gl.STENCIL_TEST); end local isScissorEnabled = gl.isEnabled(gl.SCISSOR_TEST) if isScissorEnabled then gl.disable(gl.SCISSOR_TEST); end -- ѳ֨Рܺbˀާߘֳ cc.DrawPrimitives.setCamera(self:getActiveCamera()); cc.DrawPrimitives.setWorldTransform(transform); -- ۭߘю gl.lineWidth(1) cc.DrawPrimitives.drawColor4B(255,255,255,255) local rect = cc.rect(0,self:getSize().height,self:getInnerContainerSize().width,-self:getInnerContainerSize().height); cc.DrawPrimitives.drawRect(cc.p(rect.x , rect.y), cc.p(rect.x + rect.width , rect.y + rect.height)) -- ״̬ܖش gl.lineWidth(1) cc.DrawPrimitives.drawColor4B(255,255,255,255) cc.DrawPrimitives.setPointSize(1) if isStencilEnabled then gl.enable(gl.STENCIL_TEST); end if isScissorEnabled then gl.enable(gl.SCISSOR_TEST); end end glNode:registerScriptDrawHandler(primitivesDraw) return glNode; end