You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

357 lines
9.8 KiB

  1. local SlideView = class("SlideView", cc.UIView)
  2. function SlideView:ctor(data)
  3. SlideView.super.ctor(self)
  4. local ui = loadUI("res/ui/ui_common/SlideView.ui")
  5. self.ui = ui
  6. self:addChild(ui)
  7. --外部数据
  8. self.data = data or {}
  9. --初始化数据(在前)
  10. self:initData()
  11. --初始化界面(在后)
  12. self:initView()
  13. end
  14. function SlideView:onEnter()
  15. SlideView.super.onEnter(self)
  16. --开启自滑动
  17. self:timeToSlide()
  18. end
  19. function SlideView:initData()
  20. --当前item
  21. self.curItem = nil
  22. --排序值
  23. self.maxIdx = 0
  24. --items列表
  25. self.items = {}
  26. --默认开启循环滑动
  27. self.loopSlide = self.data.loopSlide == nil and true or self.data.loopSlide
  28. --默认开启自滑动
  29. self.autoSlide = self.data.autoSlide == nil and true or self.data.autoSlide
  30. --默认隐藏左右箭头
  31. self.leftOrRight = self.data.leftOrRight == nil and false or self.data.leftOrRight
  32. --点击按钮的回调
  33. self.callBack = self.data.callBack
  34. end
  35. function SlideView:initView()
  36. --隐藏item
  37. self.ui.Items.Layout_CheckBox_Item:setVisible(false)
  38. --默认不显示背景
  39. self.ui.Items.Image_ContentBg:setVisible(false)
  40. --注册触摸事件
  41. self:registerTouch()
  42. --左右按钮
  43. self:setLeftOrRight(self.leftOrRight)
  44. self.ui.Items.Button_Left:registerClick(handler(self, self.onClickLeft))
  45. self.ui.Items.Button_Right:registerClick(handler(self, self.onClickRight))
  46. end
  47. function SlideView:addItem(imagePath)
  48. --添加item
  49. local item = cc.ImageView:createNode()
  50. item:setAnchorPoint(cc.p(0, 0.5))
  51. item:loadTexture(imagePath)
  52. self.ui.Items.Layout_Content:addChild(item, self.maxIdx)
  53. self.ui.Items.Layout_Content:requestDoLayout()
  54. self.ui.Items.Layout_Content:doLayout()
  55. --设置数据
  56. item.imageIdx = self.maxIdx
  57. self.maxIdx = self.maxIdx + 1
  58. table.insert(self.items, item)
  59. --更新区域大小
  60. if #self.items > 0 then
  61. self.ui.Items.Layout_Info:setSize(self.items[1]:getSize())
  62. --checkBox位置
  63. self:setCheckBoxPercentagePos(50, 10)
  64. --左右按钮位置
  65. self:setLeftRightPercentagePos(20, 50)
  66. end
  67. self:updateItems()
  68. end
  69. function SlideView:updateItems()
  70. local xDiff = 0
  71. --更新显示
  72. self.checkBoxs = {}
  73. self.ui.Items.Layout_CheckBox:removeAllChildren()
  74. for k,v in ipairs(self.items) do
  75. --当前显示
  76. self.curItem = self.curItem or v
  77. --保证显示
  78. v:setVisible(true)
  79. --CheckBox
  80. local checkBox = self.ui.Items.Layout_CheckBox_Item:getCopied()
  81. checkBox.Items = getUIItems(checkBox)
  82. self.ui.Items.Layout_CheckBox:addChild(checkBox)
  83. table.insert(self.checkBoxs, checkBox)
  84. checkBox.itemData = v
  85. end
  86. self.ui.Items.Layout_CheckBox:requestDoLayout()
  87. self.ui.Items.Layout_CheckBox:doLayout()
  88. self:runToCurItem(true)
  89. end
  90. function SlideView:updateCheckBox()
  91. if self.curItem then
  92. for _,v in ipairs(self.checkBoxs) do
  93. v.Items.CheckBox:setSelectedState(self.curItem.imageIdx == v.itemData.imageIdx)
  94. end
  95. end
  96. end
  97. function SlideView:updateItemsPos()
  98. if self.curItem then
  99. self.curItem:setPosition(cc.p(0, 0))
  100. self.ui.Items.Layout_Content:setPosition(cc.p(0, 0))
  101. local pos = self.curItem:getPosition()
  102. local ave = math.floor(#self.items / 2)
  103. if ave > 0 then
  104. local infoSize = self.ui.Items.Layout_Info:getSize()
  105. for _,v in ipairs(self.items) do
  106. local diff = v.imageIdx - self.curItem.imageIdx
  107. if diff > ave then
  108. diff = diff - #self.items
  109. end
  110. if diff < -ave then
  111. diff = diff + #self.items
  112. end
  113. v:setPosition(cc.p(pos.x + diff * infoSize.width, infoSize.height / 2))
  114. end
  115. end
  116. end
  117. end
  118. function SlideView:registerTouch()
  119. --触摸开始
  120. local onTouchBegan = function(touch)
  121. self:stopAllActions()
  122. if self.ui.Items.Layout_Content:getNumberOfRunningActions() > 0 then
  123. return
  124. end
  125. self.sPos = touch:getLocation()
  126. self.ui.Items.Layout_Content:stopAllActions()
  127. self.beganPos = self.ui.Items.Layout_Content:getPosition()
  128. end
  129. --触摸移动
  130. local onTouchMove = function(touch)
  131. self:stopAllActions()
  132. if self.ui.Items.Layout_Content:getNumberOfRunningActions() > 0 or not self.sPos then
  133. return
  134. end
  135. local mPos = touch:getLocation()
  136. local diff = mPos.x - self.sPos.x
  137. self.ui.Items.Layout_Content:stopAllActions()
  138. local infoSize = self.ui.Items.Layout_Info:getSize()
  139. local contentSize = self.ui.Items.Layout_Content:getSize()
  140. --每次滑动不能超过一张图
  141. if diff > infoSize.width then diff = infoSize.width end
  142. if diff < -infoSize.width then diff = -infoSize.width end
  143. --是否循环滑动
  144. if not self.loopSlide and self.curItem then
  145. if self.curItem.imageIdx == 0 and diff > 0 then
  146. diff = 0
  147. end
  148. if self.curItem.imageIdx == self.maxIdx - 1 and diff < 0 then
  149. diff = 0
  150. end
  151. end
  152. self.ui.Items.Layout_Content:setPositionX(self.beganPos.x + diff)
  153. end
  154. --触摸结束(或者取消)
  155. local onTouchEnd = function(touch)
  156. self:stopAllActions()
  157. if self.ui.Items.Layout_Content:getNumberOfRunningActions() > 0 or not self.sPos then
  158. return
  159. end
  160. --判断滑动的距离
  161. local ePos = touch:getLocation()
  162. local diff = ePos.x - self.sPos.x
  163. local infoSize = self.ui.Items.Layout_Info:getSize()
  164. --滑动超过10%
  165. if self.curItem and math.abs(diff) > infoSize.width * 0.1 then
  166. local checkIdx = nil
  167. if diff > 0 then
  168. if self.loopSlide or self.curItem.imageIdx > 0 then
  169. checkIdx = (self.curItem.imageIdx - 1 + self.maxIdx) % self.maxIdx
  170. end
  171. else
  172. if self.loopSlide or self.curItem.imageIdx < self.maxIdx - 1 then
  173. checkIdx = (self.curItem.imageIdx + 1 + self.maxIdx) % self.maxIdx
  174. end
  175. end
  176. if checkIdx then
  177. self.curItem = self.items[checkIdx + 1]
  178. end
  179. else
  180. local checkIdx = self.curItem.imageIdx + 1
  181. if checkIdx then
  182. if self.callBack then
  183. self.callBack(checkIdx)
  184. end
  185. end
  186. end
  187. self:runToCurItem()
  188. self:timeToSlide()
  189. end
  190. --注册事件
  191. self.ui.Items.Layout_Info:registerTouchEvent(onTouchBegan, onTouchMove, onTouchEnd, onTouchEnd)
  192. end
  193. function SlideView:setCheckBoxPercentagePos(xp, yp)
  194. if xp and yp then
  195. local layoutSize = self.ui.Items.Layout_Info:getSize()
  196. self.ui.Items.Layout_CheckBox:setPosition(cc.p(layoutSize.width * xp / 100, layoutSize.height * yp / 100))
  197. end
  198. end
  199. function SlideView:setCheckBoxImage(selectImage, unselectImage, xDiff)
  200. --选中图片
  201. self.ui.Items.CheckBox:loadTextureFrontCross(selectImage)
  202. --背景图图片
  203. self.ui.Items.CheckBox:loadTextureBackGround(unselectImage)
  204. local checkBoxSize = self.ui.Items.CheckBox:getSize()
  205. self.ui.Items.Layout_CheckBox_Item:setSize(cc.size(xDiff or (checkBoxSize.width + 10), checkBoxSize.height))
  206. self:updateItems()
  207. end
  208. function SlideView:setAutoSlide(bAuto)
  209. if bAuto then
  210. self.autoSlide = true
  211. if self:getNumberOfRunningActions() == 0 then
  212. self:timeToSlide()
  213. end
  214. else
  215. self.autoSlide = false
  216. self:stopAllActions()
  217. end
  218. end
  219. function SlideView:setLoopSlide(bLoop)
  220. if bLoop then
  221. self.loopSlide = true
  222. else
  223. self.loopSlide = false
  224. end
  225. end
  226. function SlideView:setLeftRightPercentagePos(xp, yp)
  227. if xp and yp then
  228. local layoutSize = self.ui.Items.Layout_Info:getSize()
  229. self.ui.Items.Button_Left:setPosition(cc.p(layoutSize.width * xp / 100, layoutSize.height * yp / 100))
  230. self.ui.Items.Button_Right:setPosition(cc.p(layoutSize.width * (100 - xp) / 100, layoutSize.height * yp / 100))
  231. end
  232. end
  233. function SlideView:setLeftRightImage(leftImage, rightImage)
  234. --左
  235. self.ui.Items.Button_Left:loadTextureNormal(leftImage)
  236. --右
  237. self.ui.Items.Button_Right:loadTextureNormal(rightImage)
  238. end
  239. function SlideView:updateLeftOrRight()
  240. if self.loopSlide then
  241. self.ui.Items.Button_Left:setVisible(true)
  242. self.ui.Items.Button_Right:setVisible(true)
  243. else
  244. self.ui.Items.Button_Left:setVisible(self.curItem and self.curItem.imageIdx > 0)
  245. self.ui.Items.Button_Right:setVisible(self.curItem and self.curItem.imageIdx < self.maxIdx - 1)
  246. end
  247. end
  248. function SlideView:onClickLeft()
  249. playBtnEffect()
  250. self:stopAllActions()
  251. if self.ui.Items.Layout_Content:getNumberOfRunningActions() > 0 then
  252. return
  253. end
  254. local checkIdx = nil
  255. if self.loopSlide or self.curItem.imageIdx > 0 then
  256. checkIdx = (self.curItem.imageIdx - 1 + self.maxIdx) % self.maxIdx
  257. end
  258. if checkIdx then
  259. self.curItem = self.items[checkIdx + 1]
  260. end
  261. self:runToCurItem()
  262. self:timeToSlide()
  263. end
  264. function SlideView:onClickRight()
  265. playBtnEffect()
  266. self:stopAllActions()
  267. if self.ui.Items.Layout_Content:getNumberOfRunningActions() > 0 then
  268. return
  269. end
  270. local checkIdx = nil
  271. if self.loopSlide or self.curItem.imageIdx < self.maxIdx - 1 then
  272. checkIdx = (self.curItem.imageIdx + 1 + self.maxIdx) % self.maxIdx
  273. end
  274. if checkIdx then
  275. self.curItem = self.items[checkIdx + 1]
  276. end
  277. self:runToCurItem()
  278. self:timeToSlide()
  279. end
  280. function SlideView:setLeftOrRight(bLeftOrRight)
  281. if bLeftOrRight then
  282. self.leftOrRight = true
  283. self:updateLeftOrRight()
  284. else
  285. self.leftOrRight = false
  286. end
  287. self.ui.Items.Layout_LeftOrRight:setVisible(self.leftOrRight)
  288. end
  289. function SlideView:timeToSlide()
  290. if not self.autoSlide then
  291. return
  292. end
  293. self:runAction(
  294. cc.Sequence:create(
  295. cc.DelayTime:create(2.0),
  296. cc.CallFunc:create(function()
  297. if self.maxIdx == 0 then
  298. return
  299. end
  300. local checkIdx = nil
  301. if self.loopSlide or self.curItem.imageIdx < self.maxIdx - 1 then
  302. checkIdx = (self.curItem.imageIdx + 1 + self.maxIdx) % self.maxIdx
  303. end
  304. if checkIdx then
  305. self.curItem = self.items[checkIdx + 1]
  306. self:runToCurItem()
  307. self:timeToSlide()
  308. end
  309. end)
  310. )
  311. )
  312. end
  313. function SlideView:runToCurItem(immediately)
  314. if self.curItem then
  315. local contentPos = self.ui.Items.Layout_Content:getPosition()
  316. self.ui.Items.Layout_Content:stopAllActions()
  317. local rePos = self.ui.Items.Layout_Info:convertToNodeSpace(self.curItem:getWorldPosition())
  318. self.ui.Items.Layout_Content:runAction(
  319. cc.Sequence:create(
  320. cc.MoveTo:create(immediately and 0 or 0.15, cc.p(contentPos.x - rePos.x, contentPos.y)),
  321. cc.CallFunc:create(function()
  322. self:updateItemsPos()
  323. end)
  324. )
  325. )
  326. self:updateCheckBox()
  327. self:updateLeftOrRight()
  328. end
  329. end
  330. return SlideView