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-30 14:30:24 |
👁️ 阅读: 2
根据你的代码分析,要实现点击Button_1后,Button_2、3、4按钮依次向下滑动的效果,需要对现有的点击事件逻辑进行调整。以下是修改后的代码:
```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 = {}
npc.ui = GUI:ui_delegate(parent)
ssrSetWidgetPosition(parent, npc.ui.Image_1, 2, 0, 0, true, true)
-- 关闭按钮
GUI:addOnClickEvent(npc.ui.close, function()
npc.Close()
end)
-- 存储当前显示的克隆组件
npc.currentClone = nil
-- 获取ListView中的主按钮
local mainButtons = {}
local buttonPositions = {} -- 存储按钮原始位置
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
buttonPositions[name] = GUI:getPosition(child) -- 记录原始位置
-- 为每个主按钮添加点击事件
GUI:addOnClickEvent(child, function()
SL:print("点击了按钮:", name)
-- 根据点击的按钮索引,决定哪些按钮需要滑动
local clickedIndex = tonumber(string.sub(name, 8)) -- 获取按钮数字
-- 移除之前显示的克隆组件
if npc.currentClone and not GUI:Win_IsNull(npc.currentClone) then
GUI:removeFromParent(npc.currentClone)
npc.currentClone = nil
end
-- 克隆新的Panel
local newPanel = GUI:Clone(npc.ui.Panel_1)
GUI:setVisible(newPanel, true)
-- 插入到当前按钮后面
local itemIndex = GUI:ListView_getItemIndex(npc.ui.ListView_1, child)
GUI:ListView_insertCustomItem(npc.ui.ListView_1, newPanel, itemIndex + 1)
npc.currentClone = newPanel
if GUI:Win_IsNull(newPanel) then
SL:print("容器不存在")
return
end
-- 先设置初始状态:从Y轴最高点开始(缩放为0)
GUI:setScaleY(newPanel, 0)
-- 创建展开动画:从0到1的Y轴缩放
local expand = GUI:ActionScaleTo(0.5, 1, 1)
-- 执行动画
GUI:runAction(newPanel, expand)
-- 让后续按钮向下滑动
for i = clickedIndex + 1, 4 do
local nextBtnName = "Button_" .. i
local nextBtn = mainButtons[i]
if nextBtn and not GUI:Win_IsNull(nextBtn) then
-- 获取原始位置
local originalPos = buttonPositions[nextBtnName]
if originalPos then
-- 计算新的位置(向下滑动100像素)
local newY = originalPos.y - 100
-- 创建滑动动画
local slideDown = GUI:ActionMoveTo(0.3, originalPos.x, newY)
-- 延迟执行,让按钮依次滑动
local delay = GUI:DelayTime((i - clickedIndex) * 0.1)
local sequence = GUI:ActionSequence(delay, slideDown)
GUI:runAction(nextBtn, sequence)
end
end
end
end)
end
end
-- 入场动画
Action_slow_appear(npc._parent, 200, 3, 1)
end
```
主要修改内容:
1. **添加位置记录**:使用`buttonPositions`表记录每个按钮的原始位置,方便后续复位
2. **获取点击索引**:从按钮名称中提取数字,确定点击的是哪个按钮
3. **后续按钮滑动**:点击按钮后,让索引大于当前点击按钮的后续按钮依次向下滑动
4. **动画延迟**:使用`GUI:DelayTime()`创建延迟效果,让按钮依次滑动
5. **滑动动画**:使用`GUI:ActionMoveTo()`创建移动动画
注意:这个实现中,后续按钮会向下滑动100像素。你可以根据需要调整滑动距离和动画时间。如果要实现复位功能,可以在关闭Panel时将所有按钮移回原始位置。
← 返回问答库