Cocos2dx implements Lili's rolling layer.
1 -- 2 -- sliding inertia. 3 -- 4 Control. slideInertia = Control. slideInertia or class ("Control. slideInertia ", function () 5 return cc. node: create (); 6 end); 7 8 function Control. slideInertia: init () 9 self. schId = 0; 10 end 11 12 function Control. slideInertia: destroy () 13 self. schId = Utils: unSchedule (self. schId); 14 end 15 16 function Control. slideInertia: setMark (x, y) 17 self. time = OS. time (); 18 self. mark = cc. p (x, y); 19 self. schId = Utils: unSchedule (self. schId); 20 end 21 22 function Control. slideInertia: run (x, y, frameCallFunc, endCallFunc) 23 frameCallFunc = functions or function () cclog ("default frameCallFunc") end; 24 endCallFunc = endCallFunc or function () cclog ("default endCallFunc") end; 25 26 local curTime = OS. time (); 27 local diffTime = math. max (curTime-self. time, 1); 28 local stepX = (x-self. mark. x)/diffTime; 29 local stepY = (y-self. mark. y)/diffTime; 30 local function call () 31 frameCallFunc (stepX, stepY); 32 stepX = stepX * 0.8; 33 stepY = stepY * 0.8; 34 if math. abs (stepX) <1 and math. abs (stepY) <1 then 35 endCallFunc (); 36 self. schId = Utils: unSchedule (self. schId); 37 end 38 end 39 if math. abs (stepX)> 0 or math. abs (stepY)> 0 then 40 self. schId = Utils: unSchedule (self. schId); 41 self. schId = Utils: runSchedule (call, 0.01); 42 end 43 end 44 45 46 -- 47 -- scroll layer. 48 -- 49 Control. scrollView = Control. scrollView or class ("Control. scrollView ", function () 50 return cc. node: create (); 51 end); 52 53 -- [[54 params = {55 ["count"] = 0, 56 ["cellWidth"] = 0, 57 ["cellHeight"] = 0, 58 ["triggerLen"] = 0, 59 ["scaleXY"] = 0, 60 ["maxOffsetY"] = 0, 61 ["newCallFunc"] = nil, => newCallFunc (I) 62 ["selCallFunc"] = nil, => selCallFunc (cell) 63 ["enterCallFunc"] = nil, => enterCallFunc (cell) 64 ["leaveCallFunc"] = nil, => leaveCallFunc (cell) 65} 66] 67 68 function Control. scrollView: init (params) 69 local count = params ["count"]; 70 local cellWidth = params ["cellWidth"]; 71 local cellHeight = params ["cellHeight"]; 72 local triggerLen = params ["triggerLen"]; 73 local scaleXY = params ["scaleXY"]; 74 local maxOffsetY = params ["maxOffsetY"] or 0; 75 local newCallFunc = params ["newCallFunc"]; 76 local selCallFunc = params ["selCallFunc"] or function (cell) cclog ("sel cell: % s", cell ); end; 77 local enterCallFunc = params ["enterCallFunc"] or function (cell) cclog ("enter cell: % s", cell); end; 78 local leaveCallFunc = params ["leaveCallFunc"] or function (cell) cclog ("leave cell: % s", cell); end; 79 local selCell = nil; 80 local curCell = nil; 81 local triggerScale = triggerLen/cellWidth; 82 local downPosX; 83 local downPosY; 84 local cellScale = 1; 85 local elements = {}; 86 local nodePosX = triggerLen * 0.5 + cellWidth * 0.5; 87 local isTouch = false; 88 local isLoadEnd = false; 89 local skipFrame = math. floor (count/50) + 1; 90 local skipCount = 0; 91 local viewSize = cc. director: getInstance (): getVisibleSize (); 92 93 local function in2Value (value, minValue, maxValue) 94 return value >=minvalue and value <= maxValue; 95 end 96 97 -- move all nodes. 98 local function moveCells (offsetX) 99 nodePosX = nodePosX + offsetX; 100 for I = 1, count do101 local cell = elements [I]; 102 if math. abs (nodePosX + I * cellWidth) <viewSize. width + cellWidth/2 then103 cell: setVisible (true); 104 -- move. 105 (function () 106 local curPosX = nodePosX + (I-1) * cellWidth; 107 local movetoX = curPosX; 108 local movetoY = 0; 109 local maxX = triggerLen/2; 110 local minX = maxX-cellWidth; 111 if curPosX <= maxX and curPosX> = minX then112 if not cell. isEnter then113 leaveCallFunc (selCell); 114 selCell. isEnter = false; 115 selCell = cell; 116 selCell. isEnter = true; 117 enterCallFunc (cell); 118 end119 movetoX = curPosX + (curPosX-maxX) * (triggerScale-1); 120 local centerOffsetY = (maxX-cellWidth/2 ); 121 local offsetWidth = (maxX-minX)/2; 122 movetoY = (1-math. abs (curPosX-centerOffsetY)/offsetWidth) * maxOffsetY; 123 else124 movetoX = curPosX + (curPosX <minX and-cellWidth * (triggerScale-1) or 0); 125 end126 cell: setPositionX (movetoX); 127 cell: setPositionY (movetoY); 128 end) (); 129 -- zoom. 130 (function () 131 local distance = math. abs (cell: getPositionX (); 132 local scale = 0; 133 local maxDistance = triggerLen * 0.5; 134 if distance <= maxDistance then135 scale = (1-distance/maxDistance) * (scaleXY-cellScale); 136 end1_cell: setScale (cellScale + scale); 138 end) (); 139 else140 cell: setVisible (false ); 141 end142 end143 end144 145 -- Adjust the location. 146 local function adjustPosition () 147 local function call () 148 local cell = curCell and curCell or selCell; 149 local pointX = cell: getPositionX (); 150 local step =-pointX * 0.1; 151 if math. abs (step) <0.1 then152 curCell = nil; 153 selCallFunc (cell); 154 self. schSlideId = Utils: unSchedule (self. schSlideId); 155 end156 moveCells (step); 157 end158 self. schSlideId = Utils: unSchedule (self. schSlideId); 159 self. schSlideId = Utils: runSchedule (call, 0.01); 160 end161 162 local function isTouchRect (touchPoint) 163 local worldPoint = self: convertToWorldSpace (cc. p (0, 0); 164 return in2Value (touchPoint. y, worldPoint. y, worldPoint. y + cellHeight); 165 end166 167 local function onTouchUp (touch) 168 local function frameCallFunc (stepX, stepY) 169 moveCells (stepX); 170 end171 local function endCallFunc () 172 adjustPosition (); 173 end174 local touchPoint = touch: getLocation (); 175 self. slide: run (touchPoint. x, touchPoint. y, frameCallFunc, endCallFunc); 176 end177 178 local function onTouchBegan (touch, event) 179 skipCount = 0; 180 local touchPoint = touch: getLocation (); 181 local result = isTouchRect (touchPoint); 182 curCell = nil; 183 downPosX = touchPoint. x; 184 downPosY = touchPoint. y; 185 if result then186 self. slide: setMark (downPosX, downPosY); 187 self. schSlideId = Utils: unSchedule (self. schSlideId); 188 end189 return result; 190 end191 192 local function onTouchMoved (touch, event) 193 if skipCount % skipFrame = 0 then194 local touchPoint = touch: getLocation (); 195 196 -- Adjust the drag sensitivity. 197 if math. abs (downPosX-touchPoint. x)> 5 then198 isTouch = false; 199 end200 201 self. slide: setMark (downPosX, downPosY); 202 moveCells (touchPoint. x-downPosX); 203 downPosX = touchPoint. x; 204 downPosY = touchPoint. y; 205 end206 skipCount = skipCount + 1; 207 end208 209 local function onTouchEnded (touch, event) 210 onTouchUp (touch); 211 end212 213 local function onTouchCancelled (touch, event) 214 onTouchUp (touch); 215 end216 217 local function setCurCell (cell) 218 if isTouch = true then219 curCell = cell; 220 adjustPosition (); 221 end222 end223 224 local function loadEnd () 225 isLoadEnd = true; 226 227 self. schLoadId = Utils: unSchedule (self. schLoadId); 228 229 -- touch response layer. 230 local touchLayer = cc. layer: create (); 231 local listener = cc. eventListenerTouchOneByOne: create (); 232 listener: registerScriptHandler (onTouchBegan, cc. handler. EVENT_TOUCH_BEGAN); 233 listener: registerScriptHandler (onTouchMoved, cc. handler. event_touch_movable); 234 listener: registerScriptHandler (onTouchEnded, cc. handler. EVENT_TOUCH_ENDED); 235 listener: registerScriptHandler (onTouchCancelled, cc. handler. EVENT_TOUCH_CANCELLED); 236 touchLayer: getEventDispatcher (): addEventListenerWithSceneGraphPriority (listener, touchLayer); 237 self: addChild (touchLayer); 238 239 -- touch swallowed layer. 240 local listener = cc. eventListenerTouchOneByOne: create (); 241 listener: registerScriptHandler (242 function (touch, event) 243 return isTouchRect (touch: getLocation (); 244 end, cc. handler. EVENT_TOUCH_BEGAN); 245 listener: setSwallowTouches (true); 246 self: getEventDispatcher (): listener (listener, self); 247 248 adjustPosition (); 249 end250 251 self. schSlideId = 0; 252 self. schLoadId = 0; 253 self. slide = Utils: createCocos2dObject (Control. slideInertia); 254 self: addChild (self. slide); 255 self: setAnchorPoint (cc. p (0.5, 0.0); 256 257 -- asynchronous loading. 258 local num = 0; 259 self. schLoadId = Utils: runSchedule (function () 260 local cells = newCallFunc (num + 1); 261 self: addChild (cell); 262 cell: setAnchorPoint (cc. p (0.5, 0.0); 263 cell: setPosition (nodePosX + num * cellWidth, 0.0); 264 cell. isEnter = false; 265 cell: setTouchEnabled (true); 266 cell. onTouchBegan = function () if isLoadEnd then isTouch = true; end; 267 cell. onTouchEnded = function () setCurCell (cell); end; 268 Utils: registerButtonEvent (cell); 269 table. insert (elements, cell); 270 cellScale = cell: getScale (); 271 selCell = selCell and selCell or cell; 272 273 num = num + 1; 274 if num = count then275 loadEnd (); 276 end277 end, 0.016); 278 end279 280 function Control. scrollView: destroy () 281 self. schSlideId = Utils: unSchedule (self. schSlideId); 282 self. schLoadId = Utils: unSchedule (self. schLoadId); 283 end copy code this control is essentially a cc. node. cc is not used internally. scrollView. this is because the engine's built-in controls are rather difficult to expand. most importantly, I like wheel building. the widget contains a sliding object used to control inertia. this control creates subnodes through newCallFunc. callback when SelCallFunc node is maximized. callback when the enterCallFunc node starts to become larger. callback when the leaveCallFunc node is restored to its original state. when moving a subnode, You need to traverse and move the subnodes one by one. this consumes a lot of performance overhead. however, this is impossible. child Nodes out of the range will be ignored and hidden. the frame skip technique is used internally. the number of hops varies according to the number of nodes. when dragging 300 skeleton animations, a minimum of 30 or more frames are allowed, with an average of about 45 frames.