ScrollNoticeView.lua 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. local ScrollNoticeView = class("ScrollNoticeView")
  2. local moveSpeed = 3
  3. local delay = 2
  4. local pos = Vector2.zero
  5. local timerId
  6. local updateHandle
  7. local contentLength
  8. function ScrollNoticeView:ctor()
  9. if not updateHandle then
  10. updateHandle = UpdateBeat:CreateListener(self.Update, self)
  11. end
  12. UpdateBeat:AddListener(updateHandle)
  13. end
  14. function ScrollNoticeView:Init(root)
  15. self.root = root
  16. self.noticeList = {}
  17. self.startPos = root.startPoint
  18. self.endPos = root.endPoint
  19. self.content = root.content
  20. pos = self.startPos.rectTransform.anchoredPosition
  21. end
  22. function ScrollNoticeView:InsertGMNotice(notice)
  23. if not notice.content or notice.content == "" or not notice.content or notice.count == 0 then
  24. return
  25. end
  26. self.root:SetActive(true)
  27. self:Reset()
  28. --notice.content = "商城购买商城购买商城购买商城购买商城购买商城购买商城购买商城购买商城购买!!!!!!!!!!!!"
  29. --notice.speed = moveSpeed
  30. --notice.count = 4
  31. --notice.interval = 2
  32. table.insert(self.noticeList, notice)
  33. self:StartNextNotice()
  34. end
  35. function ScrollNoticeView:StartNextNotice()
  36. if not self.data and #self.noticeList > 0 then
  37. local notice = table.remove(self.noticeList, #self.noticeList)
  38. self.pause = true
  39. self.data = notice
  40. self.content.text.text = self.data.content or ""
  41. self.content.contentSizeFitter:SetLayoutHorizontal()
  42. contentLength = self.content.rectTransform.rect.size.x
  43. self.count = self.data.count or 0
  44. self.speed = (not self.data.speed or self.data.speed == 0) and moveSpeed or self.data.speed
  45. timerId = ManagerContainer.LuaTimerMgr:AddTimer(delay, 1, self, self.PlayNotice, nil)
  46. end
  47. end
  48. function ScrollNoticeView:PlayNotice()
  49. if self.count == 0 then
  50. self:Reset()
  51. return
  52. end
  53. if self.data then
  54. self.pause = false
  55. pos = self.startPos.rectTransform.anchoredPosition
  56. self.content.rectTransform.anchoredPosition = pos
  57. self.count = self.count - 1
  58. end
  59. end
  60. function ScrollNoticeView:Update()
  61. if not self.pause and self.data then
  62. pos.x = pos.x - self.speed
  63. self.content.rectTransform.anchoredPosition = pos
  64. local delta = self.endPos.rectTransform.anchoredPosition.x - pos.x
  65. if delta >= contentLength then
  66. if self.count > 0 then
  67. self.pause = true
  68. timerId = ManagerContainer.LuaTimerMgr:AddTimer(self.data.interval*1000, 1, self, self.PlayNotice, nil)
  69. else
  70. --结束
  71. ManagerContainer.DataMgr.GMNoticeData:NilCurNotice()
  72. self:Reset()
  73. --self.root:SetActive(false)
  74. if #self.noticeList > 0 then
  75. self:StartNextNotice()
  76. else
  77. self.root.animator:Play("NoticeClose")
  78. end
  79. end
  80. end
  81. end
  82. end
  83. function ScrollNoticeView:Reset()
  84. if timerId then
  85. ManagerContainer.LuaTimerMgr:RemoveTimer(timerId)
  86. timerId = nil
  87. end
  88. self.data = nil
  89. self.count = 0
  90. self.pause = false
  91. self.content.rectTransform.anchoredPosition = pos
  92. end
  93. function ScrollNoticeView:Dispose()
  94. self:Reset()
  95. if updateHandle ~= nil then
  96. UpdateBeat:RemoveListener(updateHandle)
  97. updateHandle = nil
  98. end
  99. self.root = nil
  100. self.sartPos = nil
  101. self.endPos = nil
  102. self.content = nil
  103. end
  104. return ScrollNoticeView