local ui = {}
function ui.init(parent)
-- Create PPopUI
local PPopUI = GUI:Layout_Create(parent, "PPopUI", 0.00, 0.00, 171.00, 186.00, false)
GUI:setChineseName(PPopUI, "快捷使用组合")
GUI:setTouchEnabled(PPopUI, true)
GUI:setSwallowTouches(PPopUI, tr
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-06-16 19:18:28 |
👁️ 阅读: 4
根据浮生梦老师前端课程内容,这个界面已经具备了基本的UI结构,但缺少拖动功能。要实现界面移动,需要添加触摸事件处理。
以下是修改建议,在`AutoUsePop.main()`函数中添加触摸事件处理:
```lua
function AutoUsePop.main(data)
local parent = GUI:Attach_Parent()
local id = data and data.id or 0
local node = GUI:Node_Create(parent, "Node_" .. id, 0, 0)
GUI:LoadExport(node, "auto_use_pop")
GUI:setTag(node, id)
AutoUsePop._data = data
AutoUsePop._Node = node
AutoUsePop._ui = GUI:ui_delegate(node)
-- 添加触摸事件使界面可拖动
local PPopUI = AutoUsePop._ui["PPopUI"]
local isDragging = false
local startX, startY = 0, 0
local nodeX, nodeY = 0, 0
GUI:addOnTouchEvent(PPopUI, function(eventType, x, y)
if eventType == 0 then -- 触摸开始
isDragging = true
startX, startY = x, y
local pos = GUI:getPosition(AutoUsePop._Node)
nodeX, nodeY = pos.x, pos.y
return true
elseif eventType == 1 then -- 触摸移动
if isDragging then
local deltaX = x - startX
local deltaY = y - startY
GUI:setPosition(AutoUsePop._Node, nodeX + deltaX, nodeY + deltaY)
end
elseif eventType == 2 then -- 触摸结束
isDragging = false
end
end)
-- 显示适配
local posY = 140
local baseOffX = 350
local screenH = SL:GetMetaValue("SCREEN_HEIGHT")
if SL:GetMetaValue("WINPLAYMODE") then
GUI:setMouseEnabled(PPopUI, true)
baseOffX = 220
posY = screenH - 330 - GUI:getContentSize(PPopUI).height
end
local notch, rect = SL:GetMetaValue("NOTCH_PHONE_INFO")
if notch then
baseOffX = baseOffX + rect.x
end
local screenW = SL:GetMetaValue("SCREEN_WIDTH")
GUI:setPosition(AutoUsePop._Node, screenW - baseOffX, posY)
GUI:setVisible(PPopUI, true)
AutoUsePop.InitUI()
end
```
**关键点说明:**
1. **触摸事件处理**:使用`GUI:addOnTouchEvent()`函数为`PPopUI`添加触摸事件
- `eventType == 0`:触摸开始,记录起始位置
- `eventType == 1`:触摸移动,计算偏移量并更新界面位置
- `eventType == 2`:触摸结束,重置拖动状态
2. **位置计算**:
- 获取节点当前坐标:`GUI:getPosition(AutoUsePop._Node)`
- 设置新位置:`GUI:setPosition(AutoUsePop._Node, x, y)`
3. **触摸吞噬**:UI文件中已经设置了`GUI:setSwallowTouches(PPopUI, true)`,确保触摸事件不会穿透到下层界面
这样修改后,用户就可以通过拖动`PPopUI`区域来移动整个快捷使用界面了。
← 返回问答库