local npc = {}
npc.__cname = "XingShiHuFu"
-- npc.cfg = LM_JX_1
-- local job = SL:GetMetaValue("JOB")
-- npc.job_cfg = LM_JX_2[job]
function npc.main()
-- npc.jj_data = longmaishuju
local parent = GUI:Win_Create(npc.__cname, 0, 0, 0, 0,
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-05-29 20:39:23 |
👁️ 阅读: 1
要实现克隆组件从压缩高度慢慢展开,以及删除时慢慢压缩的动画效果,可以使用缩放动画配合淡入淡出效果。
以下是修改后的代码:
```lua
local npc = {}
npc.__cname = "XingShiHuFu"
function npc.main()
local parent = GUI:Win_Create(npc.__cname, 0, 0, 0, 0, false, false, true, true, true, 0)
GUI:LoadExport(parent, "UI/XingShiHuFuUI")
npc._parent = parent
npc.ui = {}
npc.ui = GUI:ui_delegate(parent)
-- 关闭按钮
GUI:addOnClickEvent(npc.ui.close, function()
npc.Close()
end)
-- 存储当前显示的克隆组件
npc.currentClone = nil
-- 获取ListView中的主按钮
local mainButtons = {}
for k, child in ipairs(GUI:ListView_getItems(npc.ui.ListView_1)) do
local name = GUI:getName(child)
SL:print("按钮名称:", name, "索引:", k)
-- 只处理Button_1到Button_4
if name == "Button_1" or name == "Button_2" or name == "Button_3" or name == "Button_4" then
mainButtons[k] = child
-- 为每个主按钮添加点击事件
GUI:addOnClickEvent(child, function()
-- 移除之前显示的克隆组件
if npc.currentClone and not GUI:Win_IsNull(npc.currentClone) then
-- 压缩动画
GUI:runAction(npc.currentClone,
GUI:ActionSequence(
GUI:ActionScaleTo(0.2, 1, 0.1), -- 压缩到几乎看不见
GUI:CallFunc(function()
GUI:removeFromParent(npc.currentClone)
npc.currentClone = nil
end)
)
)
end
-- 克隆新的Panel
local newPanel = GUI:Clone(npc.ui.Panel_1)
GUI:setVisible(newPanel, true)
-- 设置初始缩放为压缩状态(高度几乎为0)
GUI:setScale(newPanel, 1, 0.1)
-- 插入到当前按钮后面
local itemIndex = GUI:ListView_getItemIndex(npc.ui.ListView_1, child)
GUI:ListView_insertCustomItem(npc.ui.ListView_1, newPanel, itemIndex + 1)
-- 展开动画
GUI:runAction(newPanel,
GUI:ActionSequence(
GUI:DelayTime(0.1), -- 延迟0.1秒开始动画
GUI:ActionScaleTo(0.3, 1, 1) -- 0.3秒内从压缩状态展开到正常
)
)
-- 记录当前克隆组件
npc.currentClone = newPanel
SL:print("点击了按钮:", name, "插入位置:", itemIndex + 1)
end)
end
end
-- 入场动画
Action_slow_appear(npc._parent, 200, 3, 1)
end
-- 关闭函数
function npc.Close()
if npc.currentClone and not GUI:Win_IsNull(npc.currentClone) then
-- 关闭时压缩动画
GUI:runAction(npc.currentClone,
GUI:ActionSequence(
GUI:ActionScaleTo(0.2, 1, 0.1),
GUI:CallFunc(function()
GUI:removeFromParent(npc.currentClone)
npc.currentClone = nil
GUI:Win_Close(npc._parent)
end)
)
)
else
GUI:Win_Close(npc._parent)
end
end
return npc
```
**实现原理说明:**
1. **展开动画**:
- 克隆组件时,先设置初始缩放为`setScale(1, 0.1)`,让高度压缩到几乎看不见
- 使用`ActionScaleTo(0.3, 1, 1)`在0.3秒内将Y轴缩放从0.1恢复到1
2. **压缩动画**:
- 删除时使用`ActionScaleTo(0.2, 1, 0.1)`在0.2秒内将Y轴缩放从1压缩到0.1
- 动画完成后调用`removeFromParent`移除组件
3. **动画序列**:
- 使用`ActionSequence`将多个动作组合成序列
- `DelayTime`用于延迟动画开始时间
- `CallFunc`用于在动画完成后执行回调函数
**注意点:**
- 动画时间参数(0.2、0.3)可以根据需要调整,数值越大动画越慢
- 确保在动画完成后才移除组件,避免动画被中断
- 使用`GUI:Win_IsNull`检查组件是否有效,避免空指针错误
这样就能实现点击按钮时,克隆组件从压缩状态慢慢展开,再次点击其他按钮时,之前的组件慢慢压缩消失的动画效果了!
← 返回问答库