设置穿透
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-05-12 20:31:23 |
👁️ 阅读: 11
要设置控件穿透(即允许触摸事件穿透到下层控件),可以使用 `GUI:setSwallowTouches` 函数。
**函数说明:**
```lua
-- 设置控件是否触摸吞噬
-- widget: 控件对象
-- value: 是否吞噬(true为吞噬触摸,false为不吞噬/穿透)
GUI:setSwallowTouches(widget, value)
```
**使用示例:**
```lua
-- 1. 先创建父界面
local parent = GUI:Win_Create("GMMain", 0, 0, 0, 0, false, false, true, false, false, false, 1)
-- 2. 创建一个上层控件(比如按钮)
local button = GUI:Button_Create(parent, "btn_top", 100, 100, "res/public/1900000612.png")
GUI:Button_setTitleText(button, "上层按钮")
-- 3. 设置上层控件不吞噬触摸(允许穿透)
GUI:setSwallowTouches(button, false)
-- 4. 创建下层控件并设置点击事件
local bottomImage = GUI:Image_Create(parent, "img_bottom", 0, 0, "res/public/1900000610.png")
GUI:setTouchEnabled(bottomImage, true)
GUI:addOnClickEvent(bottomImage, function()
SL:Print("下层图片被点击了!")
end)
```
**关键点:**
- `setSwallowTouches(widget, false)`:设置控件不吞噬触摸事件,点击时会穿透到下层控件。
- 通常与 `setTouchEnabled(widget, true)` 配合使用,确保控件能接收触摸事件。
- 穿透效果常用于透明按钮、重叠界面等场景。
如果你想让控件完全忽略触摸(既不响应也不穿透),可以设置 `setTouchEnabled(widget, false)`。
← 返回问答库