UI文件:
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:setTag(PPopUI, -1)
-
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-06-16 19:10:36 |
👁️ 阅读: 6
要让这个界面可以移动,需要修改以下几个地方:
1. **在UI文件中添加触摸事件**:
```lua
-- 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, true) -- 添加吞噬触摸
GUI:setTag(PPopUI, -1)
-- 添加触摸事件
local isMoving = false
local startPos = {x = 0, y = 0}
local nodeStartPos = {x = 0, y = 0}
GUI:addTouchEventListener(PPopUI, function(eventType, x, y)
if eventType == 0 then -- 触摸开始
isMoving = true
startPos.x = x
startPos.y = y
local nodePos = GUI:getPosition(AutoUsePop._Node)
nodeStartPos.x = nodePos.x
nodeStartPos.y = nodePos.y
return true
elseif eventType == 1 then -- 触摸移动
if isMoving then
local deltaX = x - startPos.x
local deltaY = y - startPos.y
GUI:setPosition(AutoUsePop._Node, nodeStartPos.x + deltaX, nodeStartPos.y + deltaY)
end
elseif eventType == 2 then -- 触摸结束
isMoving = false
end
end)
```
2. **修改背景触摸设置**:
```lua
-- Create pBg
local pBg = GUI:Image_Create(PPopUI, "pBg", 0.00, 0.00, "res/public/bg_hhdb_02.jpg")
GUI:setChineseName(pBg, "快捷使用_背景图")
GUI:setTouchEnabled(pBg, true) -- 改为true以接收触摸
GUI:setSwallowTouches(pBg, true) -- 添加吞噬触摸
GUI:setTag(pBg, -1)
```
3. **在GUILAYOUT文件中调整位置计算**:
```lua
function AutoUsePop.main(data)
-- ... 现有代码 ...
-- 移除原来的固定位置设置
-- GUI:setPosition(AutoUsePop._Node, screenW - baseOffX, posY)
-- 改为初始位置(可根据需要调整)
local initX = screenW - baseOffX
local initY = posY
GUI:setPosition(AutoUsePop._Node, initX, initY)
-- ... 其他代码 ...
end
```
主要修改点:
- 为PPopUI添加触摸事件监听
- 设置背景图片可触摸
- 记录触摸开始位置和节点原始位置
- 在触摸移动时更新节点位置
- 确保触摸事件被正确吞噬,避免穿透到下层界面
这样修改后,用户就可以按住这个快捷使用界面拖动到屏幕任意位置了。
← 返回问答库