指针事件(PointerEvent)
表示一个指针交互事件(Pointer Interaction Event),包含位置与指针 ID。
字段(Fields)
position
指针在本地坐标(local coordinates)中的位置。
function handlePointerDown(self: RenderInstance, event: PointerEvent)
if self.instance then
local localPos = event.position - Vector.xy(self.x, self.y)
local localEvent = PointerEvent.new(event.id, localPos)
local hit = self.instance:pointerDown(localEvent)
if hit ~= 0 then
print(
string.format(
'Local hit at (%.1f, %.1f)',
localEvent.position.x,
localEvent.position.y
)
)
end
end
end
id
指针唯一标识(unique identifier)。
function handlePointerDown(self: RenderInstance, event: PointerEvent)
if self.instance then
local localPos = event.position - Vector.xy(self.x, self.y)
local localEvent = PointerEvent.new(event.id, localPos)
local hit = self.instance:pointerDown(localEvent)
if hit ~= 0 then
print(localEvent.id)
end
end
end
type
事件类型(如 pointerDown、pointerUp、click、pointerEnter、pointerMove 等)。
previousPosition
指针在本地坐标(local coordinates)中的上一次位置。节点指针处理器读取原点。
timeStamp
交互发生的时间,单位为秒。
构造器(Constructors)
new
new(id: number, position: Vector) -> PointerEvent
使用给定的 id 与 position 创建新的 PointerEvent。
function handlePointerDown(self: RenderInstance, event: PointerEvent)
if self.instance then
local localPos = event.position - Vector.xy(self.x, self.y)
local localEvent = PointerEvent.new(event.id, localPos)
local hit = self.instance:pointerDown(localEvent)
if hit ~= 0 then
print(
string.format(
'Local hit at (%.1f, %.1f)',
localEvent.position.x,
localEvent.position.y
)
)
end
end
end
方法(Methods)
hit
hit(isTranslucent: boolean?) -> ()
消费该事件。脚本拥有的每一次事件都要调用它——按下、拖动中的每次移动,以及松开——否则事件会继续向外传播,被外层命中目标抢走手势。常见情况是 ScrollConstraint 视口:不调用时,在滚动视图内拖动控件会变成滚动视图本身。
参数含义是反过来的:单独调用 hit() 表示不透明,事件在此停止;hit(true) 将本目标标为半透明,事件会继续传给后面的目标。
function handlePointerDown(self: PointerExample, event: PointerEvent)
if self.instance then
local hit = self.instance:pointerDown(event)
if hit ~= 0 then
-- Mark as handled but still allow translucent targets behind to receive it.
event:hit(true)
end
end
end