function npc.main()
-- npc.jj_data = longmaishuju
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 = G
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-05-29 20:42:30 |
👁️ 阅读: 1
要实现克隆组件从压缩高度慢慢展开和删除时慢慢压缩的动画效果,可以使用缩放动画配合透明度变化。以下是修改后的代码:
```lua
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 = 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
-- 压缩动画:从原始大小缩放到0
GUI:runAction(npc.currentClone, GUI:ActionSequence(
GUI:ActionScaleTo(0.3, 0.01), -- 压缩到几乎看不见
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:setScaleY(newPanel, 0.01)
GUI:setOpacity(newPanel, 0) -- 初始透明
-- 插入到当前按钮后面
local itemIndex = GUI:ListView_getItemIndex(npc.ui.ListView_1, child)
GUI:ListView_insertCustomItem(npc.ui.ListView_1, newPanel, itemIndex + 1)
-- 记录当前克隆组件
npc.currentClone = newPanel
-- 展开动画:从压缩状态慢慢展开
GUI:runAction(newPanel, GUI:ActionSequence(
GUI:ActionScaleTo(0.3, 1), -- 展开到原始大小
GUI:ActionFadeIn(0.3) -- 淡入显示
))
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.3, 0.01), -- 压缩动画
GUI:ActionFadeOut(0.2), -- 淡出动画
GUI:CallFunc(function()
GUI:removeFromParent(npc.currentClone)
npc.currentClone = nil
-- 关闭父窗口
if npc._parent and not GUI:Win_IsNull(npc._parent) then
GUI:removeFromParent(npc._parent)
end
end)
))
else
-- 直接关闭父窗口
if npc._parent and not GUI:Win_IsNull(npc._parent) then
GUI:removeFromParent(npc._parent)
end
end
end
```
**关键点说明:**
1. **展开动画**:
- 初始设置`setScaleY(newPanel, 0.01)`将组件高度压缩到几乎为0
- 使用`ActionSequence`组合`ActionScaleTo(0.3, 1)`和`ActionFadeIn(0.3)`实现同时展开和淡入
2. **压缩动画**:
- 使用`ActionScaleTo(0.3, 0.01)`将组件压缩回几乎看不见的状态
- 配合`ActionFadeOut(0.2)`实现淡出效果
- 在动画完成后通过`CallFunc`回调移除组件
3. **注意事项**:
- 使用`setOpacity(newPanel, 0)`设置初始透明度为0
- 动画时间参数(0.3秒)可以根据需要调整
- 确保在动画完成后才移除组件,避免动画中断
这样就能实现点击按钮时组件从压缩状态慢慢展开,再次点击或关闭时慢慢压缩消失的动画效果了。
← 返回问答库