-- elg:cc.ProgressHUD:show(10)
local ProgressHUD = {}
cc.ProgressHUD = ProgressHUD
-- 显示等待UI,interval:超时时间-秒 timeoutFunc:超时回调
function ProgressHUD:show( interval, timeoutFunc )
if not(self.hudInstance) then
self:init()
end
self:_show(interval, timeoutFunc)
end
-- 隐藏等待UI
function ProgressHUD:hidden( )
if not(self.hudInstance) then return end
self:_hidden(false)
end
-- 设置UI是否可见
function ProgressHUD:setState( bVisible )
if self.hudInstance then
self:setVisible(bVisible)
end
end
-- 初始化等待UI界面
function ProgressHUD:init( )
self.interval = 0
self.isUsing = false
self.isShow = false
self.hudIcon = nil
self.hudBg = nil
self.timeoutFunc = nil
self.hudInstance = nil
local hudInstance = cc.Layer:create()
self.hudInstance = hudInstance
self.hudBg = cc.Sprite:create("icon/hud_bg.png")
hudInstance:addChild(self.hudBg)
self.hudBg:setPosition(display.center)
self.hudIcon = cc.Sprite:create("icon/hud.png")
self.hudBg:addChild(self.hudIcon)
local bgSize = self.hudBg:getContentSize()
self.hudIcon:setPosition(bgSize.width / 2, bgSize.height / 2)
self.hudIcon:runAction(cc.RepeatForever:create(cc.Sequence:create(cc.RotateBy:create(0.0, 360 / 8), cc.DelayTime:create(0.25), nil)))
local touchListen = cc.EventListenerTouchOneByOne:create()
touchListen:setSwallowTouches(true)
touchListen:registerScriptHandler(handler(self, self.onTouchBegan),cc.Handler.EVENT_TOUCH_BEGAN )
cc.Director:getInstance():getEventDispatcher():addEventListenerWithSceneGraphPriority(touchListen, self.hudInstance)
self.hudInstance:registerScriptHandler(function(state)
if state == "cleanup" then
self.hudInstance = nil
end
end)
self.hudInstance:setVisible(false)
cc.Director:getInstance():getRunningScene():addChild(self.hudInstance, 2048)
end
function ProgressHUD:_show( interval, timeoutFunc )
self.interval = interval
self.timeoutFunc = timeoutFunc
if self.isUsing then
if self.interval > 0 then
self.hudInstance:stopActionByTag(8)
local delay = cc.DelayTime:create(self.interval)
local sequence = cc.Sequence:create(delay, cc.CallFunc:create(handler(self, self._time)))
local action = cc.RepeatForever:create(sequence)
action:setTag(8)
self.hudInstance:runAction(action)
else
self.hudInstance:stopActionByTag(8)
self.interval = 0
end
else
if self.interval > 0 then
local delay = cc.DelayTime:create(self.interval)
local sequence = cc.Sequence:create(delay, cc.CallFunc:create(handler(self, self._time)))
local action = cc.RepeatForever:create(sequence)
action:setTag(8)
self.hudInstance:runAction(action)
end
self.hudInstance:setVisible(false)
self.isUsing = true
end
-- 此处为了良好的用户体验,故意延迟一秒钟后出现UI,
-- 例如网络请求在一秒之内回包,那么hidden之后就不会出现UI,如果超过一秒钟,则1秒之后才会出现等待UI
-- 如果网络状况良好,则用户就看不到等待UI了,让人感觉没有等待
if not(self.isShow) then
local delay = cc.DelayTime:create(1.0)
local sequence = cc.Sequence:create(delay, cc.CallFunc:create(handler(self, self._autoShowUI)))
local action = cc.RepeatForever:create(sequence)
action:setTag(16)
self.hudInstance:runAction(action)
end
end