1 消息上漂提示
- 实现方法
- 创建一个Layer图层并加载至场景中;
- 从消息队列中选择队头消息,加载至Layer并同时移出队列;
- 利用MoveTo方法将Layer向上移动一定长度(此时Layer中所有子节点消息都随之向上移动),之后将Layer所有子节点位置向上移动同等长度并将Layer位置重置成初始位置,之后递归这一动作,直到消息队列为空;
- 当向消息队列中添加消息时需要判断消息处理的递归动作是否已经停止,如果已停止,需再次调用递归函数以再次启动处理流程。
- 实现代码
local UIMessageTip = class("UIMessageTip",_class.UIBase)
function UIMessageTip:ctor()
_class.UIBase.ctor(self)
local cs = cc.Director:getInstance():getWinSize()
self:setContentSize(cs)
self.isMoving = false
self.m_msgArr = {}
self.m_movingLayer = cc.Layer:create()
self.m_movingLayer:setPosition(cc.p(0,0))
self:addChild(self.m_movingLayer)
end
function UIMessageTip:addMsg( msg )
table.insert(self.m_msgArr, msg)
if not self.isMoving then
self:showNextMsg()
end
end
UIMessageTip.MsgShowTime = 1.5
UIMessageTip.MsgMoveTime = 0.4
UIMessageTip.LayerMoveY = 30
function UIMessageTip:showNextMsg()
if #self.m_msgArr <= 0 then
self.isMoving = false
return
end
self.isMoving = true
local curMsg = table.remove(self.m_msgArr,1)
local msgText = cc.Label:createWithTTF(curMsg.text or "", "fonts/arial.ttf", 32)
msgText:setAnchorPoint(cc.p(0.5,0.5))
local cs = cc.Director:getInstance():getWinSize()
msgText:setPosition(cc.p(cs.width/2,cs.height/2))
self.m_movingLayer:addChild(msgText)
local delayTime = cc.DelayTime:create(self.MsgShowTime)
local callback = cc.CallFunc:create(function (target)
target:removeFromParent()
end)
msgText:runAction(cc.Sequence:create(delayTime,callback))
local moveto = cc.MoveTo:create(self.MsgMoveTime,cc.p(0,self.LayerMoveY))
local updateMsgPos = cc.CallFunc:create(function (target)
for i,v in ipairs(target:getChildren()) do
local curPosY = v:getPositionY()
curPosY = curPosY + self.LayerMoveY
v:setPositionY(curPosY)
end
target:setPositionY(0)
end)
local showNextFunc = cc.CallFunc:create(function ()
self:showNextMsg()
end)
self.m_movingLayer:runAction(cc.Sequence:create(moveto,updateMsgPos,showNextFunc))
end
function UIMessageTip:onEnter()
local clipcs = self:getContentSize()
local scrollBtn = ccui.Button:create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png")
scrollBtn:setPosition(cc.p(clipcs.width/2,50))
scrollBtn:addClickEventListener(function(sender)
print("scrollBtn:addClickEventListener")
self:addMsg({text = "i love tt"})
end)
scrollBtn:setTitleText("测试按钮")
self:addChild(scrollBtn)
end
function UIMessageTip:onExit()
end
function UIMessageTipMain()
cclog("UIRotateMenu2Main")
local scene = cc.Scene:create()
local rotateMenu = UIMessageTip:create()
scene:addChild(rotateMenu)
scene:addChild(CreateBackMenuItem())
return scene
end