ScrollViewUI.lua 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. -- region *.lua
  2. -- Date
  3. -- 此文件由[BabeLua]插件自动生成
  4. local ScrollViewUI = class("ScrollViewUI", function() return ccui.Widget:create() end)
  5. ScrollViewUI.SCROLLVIEW_DIR = {
  6. SCROLLVIEW_DIR_NONE = 0,
  7. SCROLLVIEW_DIR_VERTICAL = 1,
  8. SCROLLVIEW_DIR_HORIZONTAL = 2,
  9. SCROLLVIEW_DIR_BOTH = 3
  10. }
  11. function ScrollViewUI:ctor()
  12. self._scrollView = nil
  13. self._cellList = { }
  14. self._cellCount = 0
  15. self._spacing = 0
  16. self._innerWidth = 0
  17. self._innerHeight = 0
  18. self._fixHeight = 0
  19. self._bottomCallBack = nil
  20. end
  21. function ScrollViewUI:Init(scrollView)
  22. self._scrollView = scrollView
  23. if not self._scrollView then
  24. return false
  25. end
  26. self._direction = self._scrollView:getDirection()
  27. self._update = Schedule( function()
  28. if self._direction == ScrollViewUI.SCROLLVIEW_DIR.SCROLLVIEW_DIR_HORIZONTAL then
  29. self:_UpdateCellHorizontal()
  30. elseif self._direction == ScrollViewUI.SCROLLVIEW_DIR.SCROLLVIEW_DIR_VERTICAL then
  31. self:_UpdateCellVertical()
  32. end
  33. end , 0)
  34. -- 监听事件;
  35. self:registerScriptHandler( function(state)
  36. if state == "enter" then
  37. self:onEnter()
  38. elseif state == "exit" then
  39. self:onExit()
  40. end
  41. end )
  42. local parent = self._scrollView:getParent()
  43. if parent then
  44. parent:addChild(self)
  45. end
  46. local function bottom(sender, type)
  47. if sender:getInnerContainerPosition().y == 0 then
  48. self:_moveBottomCallBack()
  49. end
  50. end
  51. self._scrollView:addClickEventListener(bottom)
  52. return true
  53. end
  54. function ScrollViewUI:Create(...)
  55. local ui = ScrollViewUI.new()
  56. if ui and ui:Init(...) then
  57. return ui
  58. end
  59. end
  60. function ScrollViewUI:onEnter()
  61. end
  62. function ScrollViewUI:onExit()
  63. self:RemoveAllCell()
  64. if self._update then
  65. UnSchedule(self._update)
  66. self._update = nil
  67. end
  68. end
  69. function ScrollViewUI:InitPosition()
  70. if self._direction == ScrollViewUI.SCROLLVIEW_DIR.SCROLLVIEW_DIR_HORIZONTAL then
  71. self:_InitPositionHorizontal()
  72. self:_UpdateCellHorizontal()
  73. elseif self._direction == ScrollViewUI.SCROLLVIEW_DIR.SCROLLVIEW_DIR_VERTICAL then
  74. self:_InitPositionVertical()
  75. self:_UpdateCellVertical()
  76. end
  77. end
  78. function ScrollViewUI:_InitPositionVertical()
  79. local containerSize = self._scrollView:getInnerContainerSize()
  80. local totalHeight = self._fixHeight
  81. for i = self._cellCount, 1, -1 do
  82. local cell = self._cellList[i]
  83. local cellAnchorY = cell:GetAnchorPoint().y
  84. local cellHeight = cell:GetContentSize().height
  85. if cellAnchorY == 0 then
  86. cell.positionY = totalHeight
  87. elseif cellAnchorY == 0.5 then
  88. cell.positionY = totalHeight + cellHeight / 2
  89. elseif cellAnchorY == 1 then
  90. cell.positionY = totalHeight + cellHeight
  91. end
  92. totalHeight = totalHeight + cellHeight + self._spacing
  93. end
  94. local contentSizeHeight = self._scrollView:getContentSize().height
  95. if totalHeight < contentSizeHeight then
  96. local y = contentSizeHeight - totalHeight
  97. for i = 1, self._cellCount do
  98. local cell = self._cellList[i]
  99. cell.positionY = cell.positionY + y
  100. end
  101. end
  102. -- 记录原先的pos,防止插入cell后滚动位置跳动
  103. -- local pos = self._scrollView:getInnerContainerPosition()
  104. -- pos.y = pos.y - (totalHeight - containerSize.height)
  105. self._innerHeight = totalHeight
  106. self._innerWidth = containerSize.width
  107. self._scrollView:setInnerContainerSize(cc.size(containerSize.width, totalHeight))
  108. -- 恢复位置
  109. -- self._scrollView:setInnerContainerPosition(pos)
  110. end
  111. function ScrollViewUI:_InitPositionHorizontal()
  112. local containerSize = self._scrollView:getInnerContainerSize()
  113. local totalWidth = 0
  114. for i = 1, self._cellCount do
  115. local cell = self._cellList[i]
  116. local cellAnchorX = cell:GetAnchorPoint().x
  117. local cellWidth = cell:GetContentSize().width
  118. if cellAnchorX == 0 then
  119. cell.positionX = totalWidth
  120. elseif cellAnchorX == 0.5 then
  121. cell.positionX = totalWidth + cellWidth / 2
  122. elseif cellAnchorX == 1 then
  123. cell.positionX = totalWidth + cellWidth
  124. end
  125. totalWidth = totalWidth + cellWidth + self._spacing
  126. end
  127. self._innerHeight = containerSize.height
  128. self._innerWidth = totalWidth
  129. self._scrollView:setInnerContainerSize(cc.size(totalWidth, containerSize.height))
  130. end
  131. function ScrollViewUI:_UpdateCellVertical()
  132. local innerContainerPositionY = self._scrollView:getInnerContainerPosition().y
  133. local innercontainerHeight = self._scrollView:getInnerContainerSize().height
  134. local contentSize = self._scrollView:getContentSize()
  135. for key, cell in pairs(self._cellList) do
  136. local cellHeight = cell:GetContentSize().height
  137. -- 是否在显示范围
  138. if cell.positionY + innerContainerPositionY >= - cellHeight and
  139. cell.positionY + innerContainerPositionY <= contentSize.height then
  140. if not cell._entered then
  141. cell:Enter()
  142. if cell._widget then
  143. self._scrollView:addChild(cell._widget)
  144. end
  145. end
  146. if cell._widget and not tolua.isnull(cell._widget) then
  147. cell._widget:setPosition(cc.p(0, cell.positionY))
  148. else
  149. local layerFacadeMediator = global.Facade:retrieveMediator("LayerFacadeMediator")
  150. local mediator = layerFacadeMediator._normalItems[#layerFacadeMediator._normalItems]
  151. if mediator then
  152. if cell.__cname then
  153. local bool1 = false
  154. local bool2 = false
  155. if cell._widget then
  156. bool1 = true
  157. if not tolua.isnull(cell._widget) then
  158. bool1 = false
  159. end
  160. end
  161. end
  162. end
  163. end
  164. else
  165. if cell._entered then
  166. cell:Exit()
  167. end
  168. end
  169. end
  170. end
  171. function ScrollViewUI:_UpdateCellHorizontal()
  172. local innerContainerPositionX = self._scrollView:getInnerContainerPosition().x
  173. local innercontainerWidth = self._scrollView:getInnerContainerSize().width
  174. local contentSize = self._scrollView:getContentSize()
  175. for key, cell in pairs(self._cellList) do
  176. local cellWidth = cell:GetContentSize().width
  177. -- 是否在显示范围
  178. if cell.positionX + innerContainerPositionX >= - cellWidth and
  179. cell.positionX + innerContainerPositionX <= contentSize.width + cellWidth then
  180. if not cell._entered then
  181. cell:Enter()
  182. self._scrollView:addChild(cell._widget)
  183. end
  184. cell._widget:setPosition(cc.p(cell.positionX, 100))
  185. else
  186. if cell._entered then
  187. cell:Exit()
  188. end
  189. end
  190. end
  191. end
  192. function ScrollViewUI:_moveBottomCallBack()
  193. if self._bottomCallBack then
  194. self._bottomCallBack()
  195. end
  196. end
  197. function ScrollViewUI:AddBottomCallBack(callBack)
  198. self._bottomCallBack = callBack
  199. end
  200. function ScrollViewUI:GetInnerWidth()
  201. return self._innerWidth
  202. end
  203. function ScrollViewUI:GetInnerHeight()
  204. return self._innerHeight
  205. end
  206. function ScrollViewUI:FixHeight(value)
  207. self._fixHeight = value or self._fixHeight
  208. end
  209. function ScrollViewUI:PushCell(cell)
  210. self._cellCount = self._cellCount + 1
  211. self._cellList[#self._cellList + 1] = cell
  212. self:InitPosition()
  213. end
  214. function ScrollViewUI:RemoveAllCell()
  215. for _, cell in pairs(self._cellList) do
  216. if cell._entered then
  217. cell:Exit()
  218. end
  219. end
  220. self._cellList = { }
  221. self._cellCount = 0
  222. self:InitPosition()
  223. end
  224. function ScrollViewUI:RemoveCellById(index)
  225. local cell = self._cellList[index]
  226. if not cell then
  227. return
  228. end
  229. if cell._entered then
  230. cell:Exit()
  231. end
  232. for i = index, self._cellCount - 1 do
  233. self._cellList[i] = self._cellList[i + 1]
  234. end
  235. self._cellList[self._cellCount] = nil
  236. self._cellCount = self._cellCount - 1
  237. self:InitPosition()
  238. end
  239. function ScrollViewUI:RemoveCell(cell)
  240. for index, value in pairs(self._cellList) do
  241. if value == cell then
  242. self:RemoveCellById(index)
  243. return
  244. end
  245. end
  246. end
  247. function ScrollViewUI:JumpToBottom()
  248. self._scrollView:jumpToBottom()
  249. end
  250. function ScrollViewUI:JumpToTop()
  251. self._scrollView:jumpToTop()
  252. end
  253. function ScrollViewUI:setInnerContainerPosition(pos)
  254. self._scrollView:setInnerContainerPosition(pos)
  255. end
  256. function ScrollViewUI:ScrollToBottom(timeInSec, attenuated)
  257. self._scrollView:scrollToBottom(timeInSec, attenuated)
  258. end
  259. return ScrollViewUI
  260. -- endregion