scroview有2个虚方法:
virtual void scrollViewDidScroll(ScrollView* view):这是当scrollView在被拖动时会响应该函数;
virtual void scrollViewDidZoom(ScrollView* view):这是当scrollView在进行缩放时会响应该函数;
self._ScrollView = self:getWidget("ScrollView_1") -- self._ScrollView:setInertiaScrollEnabled(false) self._slider = self:getWidget("Slider_1") local function percentChangedEvent(sender,eventType) print(eventType) if eventType == ccui.SliderEventType.percentChanged then local slider = sender local percent = slider:getPercent() self._ScrollView:jumpToPercentVertical(percent) elseif eventType == ccui.SliderEventType.slideBallUp then -- self._displayValueLabel:setString("Slide Ball Up") elseif eventType == ccui.SliderEventType.slideBallDown then -- self._displayValueLabel:setString("Slide Ball Down") elseif eventType == ccui.SliderEventType.slideBallCancel then -- self._displayValueLabel:setString("Slide Ball Cancel") end end --[[ typedef enum { --typedef: 0--8 SCROLLVIEW_EVENT_SCROLL_TO_TOP,SCROLLVIEW_EVENT_SCROLL_TO_BOTTOM,SCROLLVIEW_EVENT_SCROLL_TO_LEFT,SCROLLVIEW_EVENT_SCROLL_TO_RIGHT,SCROLLVIEW_EVENT_SCROLLING,SCROLLVIEW_EVENT_BOUNCE_TOP,SCROLLVIEW_EVENT_BOUNCE_BOTTOM,SCROLLVIEW_EVENT_BOUNCE_LEFT,SCROLLVIEW_EVENT_BOUNCE_RIGHT }ScrollviewEventType; ]] local function scrollViewDidScroll(view) local pos = view:getInnerContainerPosition() local posy = math.abs(pos.y) local InnerSize = view:getInnerContainerSize() local ScrollViewSize = view:getContentSize() local scroTotalHeight = InnerSize.height - ScrollViewSize.height local percent = (posy/scroTotalHeight)*100 self._slider:setPercent(100-percent) end --[[ local function ScrollCallback(sender,eventType) if eventType == SCROLLVIEW_EVENT_SCROLLING then -- print("---scrolling-----") local pos = self._ScrollView:getInnerContainerPosition() -- print("---pos-----",pos.x,pos.y) local posy = math.abs(pos.y) local InnerSize = self._ScrollView:getInnerContainerSize() local ScrollViewSize = self._ScrollView:getContentSize() local scroTotalHeight = InnerSize.height - ScrollViewSize.height -- print("---InnerSize--ScrollViewSize-",InnerSize.width,InnerSize.height,ScrollViewSize.width,ScrollViewSize.height) local percent = (posy/scroTotalHeight)*100 self._slider:setPercent(100-percent) elseif eventType == SCROLLVIEW_EVENT_SCROLL_TO_TOP then -- print("---scroll to top-----") elseif eventType == SCROLLVIEW_EVENT_SCROLL_TO_BOTTOM then -- print("---scroll to bottom-----") end end --]] self._slider:addEventListener(percentChangedEvent) self._ScrollView:addEventListener(scrollViewDidScroll)
TableView:
local slider2 = self:getWidget("Slider2") local tableView = cc.TableView:create(cc.size(600,60)) tableView:setDirection(cc.SCROLLVIEW_DIRECTION_HORIZONTAL) tableView:setPosition(cc.p(350,320)) tableView:setBounceable(false) tableView:setDelegate() self:addChild(tableView) local function scrollViewDidScroll(view) local posx = math.abs(tableView:getContentOffset().x) local innerWidth = 60*25 - 600 local percent = posx/innerWidth * 100 slider2:setPercent(percent) end local function scrollViewDidZoom(view) print("scrollViewDidZoom") end local function tableCellTouched(table,cell) print("cell touched at index: " .. cell:getIdx()) end local function cellSizeForTable(table,idx) return 60,60 end local function tableCellAtIndex(table,idx) local strValue = string.format("%d",idx) local cell = table:dequeueCell() local label = nil if nil == cell then cell = cc.TableViewCell:new() local sprite = cc.Sprite:create("testimages/Icon.png") sprite:setAnchorPoint(cc.p(0,0)) sprite:setPosition(cc.p(0,0)) cell:addChild(sprite) label = cc.Label:createWithSystemFont(strValue,"Helvetica",20.0) label:setPosition(cc.p(0,0)) label:setAnchorPoint(cc.p(0,0)) label:setTag(123) cell:addChild(label) else label = cell:getChildByTag(123) if nil ~= label then label:setString(strValue) end end return cell end local function numberOfCellsInTableView(table) return 25 end --registerScriptHandler functions must be before the reloadData funtion tableView:registerScriptHandler(numberOfCellsInTableView,cc.NUMBER_OF_CELLS_IN_TABLEVIEW) tableView:registerScriptHandler(scrollViewDidScroll,cc.SCROLLVIEW_SCRIPT_SCROLL) tableView:registerScriptHandler(scrollViewDidZoom,cc.SCROLLVIEW_SCRIPT_ZOOM) tableView:registerScriptHandler(tableCellTouched,cc.TABLECELL_TOUCHED) tableView:registerScriptHandler(cellSizeForTable,cc.TABLECELL_SIZE_FOR_INDEX) tableView:registerScriptHandler(tableCellAtIndex,cc.TABLECELL_SIZE_AT_INDEX) tableView:reloadData() local function percentChangedEvent2(sender,eventType) if eventType == ccui.SliderEventType.percentChanged then local slider = sender local percent = slider:getPercent()/100 local innerWidth = 60*25 - 600 local length = innerWidth*percent tableView:setContentOffset(cc.p(-length,0),false) elseif eventType == ccui.SliderEventType.slideBallUp then elseif eventType == ccui.SliderEventType.slideBallDown then elseif eventType == ccui.SliderEventType.slideBallCancel then end end slider2:addEventListener(percentChangedEvent2)原文链接:https://www.f2er.com/cocos2dx/338685.html