要优化代码让`ui.Panel_1`节点下的所有控件都做相同的淡入淡出动画,我们需要遍历所有子控件并分别设置动画。以下是优化后的代码:
```lua
MainLeftTop = {}
function MainLeftTop.main()
local bool = SL:GetMetaValue("IS_PC_PLAY_MODE")
if bool then
SL:Print("PC端")
MainLeftTop.client = {831, -5}
MainLeftTop.status = false
else
SL:Print("手机端")
MainLeftTop.client = {831, -35}
MainLeftTop.status = true
end
MainLeftTop.createUI()
end
local function toggleExpand()
-- 切换状态
MainLeftTop.status = not MainLeftTop.status
MainLeftTop.updateUI()
end
function MainLeftTop.createUI()
local parent = GUI:Attach_LeftTop()
GUI:removeAllChildren(parent)
GUI:LoadExport(parent, "main/main_left_top")
local ui = GUI:ui_delegate(parent)
MainLeftTop.ui = ui
-- 绑定点击事件
GUI:addOnClickEvent(ui.Button_2, toggleExpand)
GUI:addOnClickEvent(ui.Button_1, toggleExpand)
-- 设置初始位置
GUI:setPosition(ui.Node_1, MainLeftTop.client[1], MainLeftTop.client[2])
-- 初始化UI状态
MainLeftTop.updateUI()
end
-- 辅助函数:获取Panel_1下的所有子控件
local function getChildrenOfPanel(panel)
local children = {}
local childNames = GUI:getChildrenNames(panel) -- 底层函数,获取所有子节点名称
for _, name in ipairs(childNames) do
local child = GUI:getChildByName(panel, name)
if child and not GUI:Win_IsNull(child) then
table.insert(children, child)
end
end
return children
end
-- 辅助函数:为控件组设置淡入动画
local function fadeInGroup(widgets, time)
for _, widget in ipairs(widgets) do
if not GUI:Win_IsNull(widget) then
GUI:setOpacity(widget, 0)
GUI:setVisible(widget, true)
GUI:Timeline_FadeIn(widget, time)
end
end
end
-- 辅助函数:为控件组设置淡出动画
local function fadeOutGroup(widgets, time, callback)
local completed = 0
local total = #widgets
for _, widget in ipairs(widgets) do
if not GUI:Win_IsNull(widget) then
GUI:Timeline_FadeOut(widget, time, function()
completed = completed + 1
GUI:setVisible(widget, false)
GUI:setOpacity(widget, 255) -- 重置透明度
-- 所有动画完成后执行回调
if completed == total and callback then
callback()
end
end)
else
completed = completed + 1
end
end
end
function MainLeftTop.updateUI()
local ui = MainLeftTop.ui
if not ui then return end
SL:Print("状态:", MainLeftTop.status, "坐标:", MainLeftTop.client[1], MainLeftTop.client[2])
-- 获取Panel_1下的所有子控件
local panelChildren = getChildrenOfPanel(ui.Panel_1)
if MainLeftTop.status then
-- 展开状态
GUI:setVisible(ui.Panel_1, true)
GUI:setVisible(ui.Button_1, false)
GUI:setVisible(ui.Button_2, true)
-- 为Panel_1及其所有子控件设置淡入动画
GUI:setOpacity(ui.Panel_1, 0)
GUI:Timeline_FadeIn(ui.Panel_1, 0.5)
-- 为子控件设置淡入动画
fadeInGroup(panelChildren, 0.5)
else
-- 收起状态
GUI:setVisible(ui.Button_1, true)
GUI:setVisible(ui.Button_2, false)
-- 为Panel_1及其所有子控件设置淡出动画
GUI:Timeline_FadeOut(ui.Panel_1, 0.5, function()
GUI:setVisible(ui.Panel_1, false)
GUI:setOpacity(ui.Panel_1, 255) -- 重置透明度
end)
-- 为子控件设置淡出动画
fadeOutGroup(panelChildren, 0.5, function()
-- 所有子控件动画完成后的回调
SL:Print("所有子控件淡出动画完成")
end)
end
end
```
**优化说明:**
1. **添加了辅助函数**:
- `getChildrenOfPanel()`:获取Panel_1下的所有子控件
- `fadeInGroup()`:为控件组设置淡入动画
- `fadeOutGroup()`:为控件组设置淡出动画
2. **动画同步处理**:
- 淡出动画使用计数器确保所有子控件动画完成后才执行回调
- 每个控件独立设置透明度和可见性
3. **健壮性增强**:
- 使用`GUI:Win_IsNull()`检查控件是否有效
- 处理可能为空的控件
4. **动画效果**:
- Panel_1和所有子控件同时进行淡入淡出
- 保持0.5秒的动画时间一致
- 动画完成后重置透明度为255
这样修改后,当Panel_1展开或收起时,它下面的所有子控件都会同步进行淡入淡出动画,视觉效果更加统一协调。