概要
属性
方法
TweenPosition(endPosition: UDim2,easingDirection: Enum.EasingDirection,easingStyle: Enum.EasingStyle,time: number,override: boolean,callback: function):boolean |
TweenSize(endSize: UDim2,easingDirection: Enum.EasingDirection,easingStyle: Enum.EasingStyle,time: number,override: boolean,callback: function):boolean |
TweenSizeAndPosition(endSize: UDim2,endPosition: UDim2,easingDirection: Enum.EasingDirection,easingStyle: Enum.EasingStyle,time: number,override: boolean,callback: function):boolean |
活动
DragBegin(initialPosition: UDim2):RBXScriptSignal |
DragStopped(x: number,y: number):RBXScriptSignal |
InputBegan(input: InputObject):RBXScriptSignal |
InputChanged(input: InputObject):RBXScriptSignal |
InputEnded(input: InputObject):RBXScriptSignal |
MouseEnter(x: number,y: number):RBXScriptSignal |
MouseLeave(x: number,y: number):RBXScriptSignal |
MouseMoved(x: number,y: number):RBXScriptSignal |
TouchLongPress(touchPositions: {any},state: Enum.UserInputState):RBXScriptSignal |
TouchPan(touchPositions: {any},totalTranslation: Vector2,velocity: Vector2,state: Enum.UserInputState):RBXScriptSignal |
TouchPinch(touchPositions: {any},scale: number,velocity: number,state: Enum.UserInputState):RBXScriptSignal |
TouchRotate(touchPositions: {any},rotation: number,velocity: number,state: Enum.UserInputState):RBXScriptSignal |
TouchSwipe(swipeDirection: Enum.SwipeDirection,numberOfTouches: number):RBXScriptSignal |
TouchTap(touchPositions: {any}):RBXScriptSignal |
继承自
API 参考
属性
Active
代码示例
文本按钮活动去抖动
-- 将此 LocalScript 放置在一个 TextButton(或 ImageButton)中
local textButton = script.Parent
textButton.Text = "点击我"
textButton.Active = true
local function onActivated()
-- 这就像一个去抖动
textButton.Active = false
-- 从 5 倒计时
for i = 5, 1, -1 do
textButton.Text = "时间: " .. i
task.wait(1)
end
textButton.Text = "点击我"
textButton.Active = true
end
textButton.Activated:Connect(onActivated)AnchorPoint
代码示例
锚点演示
local guiObject = script.Parent
while true do
-- 左上角
guiObject.AnchorPoint = Vector2.new(0, 0)
guiObject.Position = UDim2.new(0, 0, 0, 0)
task.wait(1)
-- 顶部
guiObject.AnchorPoint = Vector2.new(0.5, 0)
guiObject.Position = UDim2.new(0.5, 0, 0, 0)
task.wait(1)
-- 右上角
guiObject.AnchorPoint = Vector2.new(1, 0)
guiObject.Position = UDim2.new(1, 0, 0, 0)
task.wait(1)
-- 左侧
guiObject.AnchorPoint = Vector2.new(0, 0.5)
guiObject.Position = UDim2.new(0, 0, 0.5, 0)
task.wait(1)
-- 正中心
guiObject.AnchorPoint = Vector2.new(0.5, 0.5)
guiObject.Position = UDim2.new(0.5, 0, 0.5, 0)
task.wait(1)
-- 右侧
guiObject.AnchorPoint = Vector2.new(1, 0.5)
guiObject.Position = UDim2.new(1, 0, 0.5, 0)
task.wait(1)
-- 左下角
guiObject.AnchorPoint = Vector2.new(0, 1)
guiObject.Position = UDim2.new(0, 0, 1, 0)
task.wait(1)
-- 底部
guiObject.AnchorPoint = Vector2.new(0.5, 1)
guiObject.Position = UDim2.new(0.5, 0, 1, 0)
task.wait(1)
-- 右下角
guiObject.AnchorPoint = Vector2.new(1, 1)
guiObject.Position = UDim2.new(1, 0, 1, 0)
task.wait(1)
endAutomaticSize
代码示例
屏幕 GUI 中的 LocalScript
-- 输出文本标签/字体/大小的数组
local labelArray = {
{ text = "Lorem", font = Enum.Font.Creepster, size = 50 },
{ text = "ipsum", font = Enum.Font.IndieFlower, size = 35 },
{ text = "dolor", font = Enum.Font.Antique, size = 55 },
{ text = "sit", font = Enum.Font.SpecialElite, size = 65 },
{ text = "amet", font = Enum.Font.FredokaOne, size = 40 },
}
-- 创建一个自动调整大小的父框架
local parentFrame = Instance.new("Frame")
parentFrame.AutomaticSize = Enum.AutomaticSize.XY
parentFrame.BackgroundColor3 = Color3.fromRGB(90, 90, 90)
parentFrame.Size = UDim2.fromOffset(25, 100)
parentFrame.Position = UDim2.fromScale(0.1, 0.1)
parentFrame.Parent = script.Parent
-- 添加列表布局
local listLayout = Instance.new("UIListLayout")
listLayout.Padding = UDim.new(0, 5)
listLayout.Parent = parentFrame
-- 设置圆角和填充以获得视觉美感
local roundedCornerParent = Instance.new("UICorner")
roundedCornerParent.Parent = parentFrame
local uiPaddingParent = Instance.new("UIPadding")
uiPaddingParent.PaddingTop = UDim.new(0, 5)
uiPaddingParent.PaddingLeft = UDim.new(0, 5)
uiPaddingParent.PaddingRight = UDim.new(0, 5)
uiPaddingParent.PaddingBottom = UDim.new(0, 5)
uiPaddingParent.Parent = parentFrame
for i = 1, #labelArray do
-- 从数组中创建一个自动调整大小的文本标签
local childLabel = Instance.new("TextLabel")
childLabel.AutomaticSize = Enum.AutomaticSize.XY
childLabel.Size = UDim2.fromOffset(75, 15)
childLabel.Text = labelArray[i]["text"]
childLabel.Font = labelArray[i]["font"]
childLabel.TextSize = labelArray[i]["size"]
childLabel.TextColor3 = Color3.new(1, 1, 1)
childLabel.Parent = parentFrame
-- 视觉美感
local roundedCorner = Instance.new("UICorner")
roundedCorner.Parent = childLabel
local uiPadding = Instance.new("UIPadding")
uiPadding.PaddingTop = UDim.new(0, 5)
uiPadding.PaddingLeft = UDim.new(0, 5)
uiPadding.PaddingRight = UDim.new(0, 5)
uiPadding.PaddingBottom = UDim.new(0, 5)
uiPadding.Parent = childLabel
task.wait(2)
endBackgroundColor
BackgroundColor3
代码示例
彩虹框架
-- 将这段代码放在框架中的 LocalScript 中
local frame = script.Parent
while true do
for hue = 0, 255, 4 do
-- HSV = 色相,饱和度,亮度
-- 如果我们不断循环从 0 到 1,我们就会得到一条彩虹!
frame.BorderColor3 = Color3.fromHSV(hue / 256, 1, 1)
frame.BackgroundColor3 = Color3.fromHSV(hue / 256, 0.5, 0.8)
task.wait()
end
endBorderColor
BorderColor3
代码示例
按钮高亮
-- 将我放在某个 GuiObject 中,最好是 ImageButton/TextButton
local button = script.Parent
local function onEnter()
button.BorderSizePixel = 2
button.BorderColor3 = Color3.new(1, 1, 0) -- 黄色
end
local function onLeave()
button.BorderSizePixel = 1
button.BorderColor3 = Color3.new(0, 0, 0) -- 黑色
end
-- 连接事件
button.MouseEnter:Connect(onEnter)
button.MouseLeave:Connect(onLeave)
-- 我们的默认状态是 "未悬停"
onLeave()BorderSizePixel
代码示例
按钮高亮
-- 将我放在某个 GuiObject 中,最好是 ImageButton/TextButton
local button = script.Parent
local function onEnter()
button.BorderSizePixel = 2
button.BorderColor3 = Color3.new(1, 1, 0) -- 黄色
end
local function onLeave()
button.BorderSizePixel = 1
button.BorderColor3 = Color3.new(0, 0, 0) -- 黑色
end
-- 连接事件
button.MouseEnter:Connect(onEnter)
button.MouseLeave:Connect(onLeave)
-- 我们的默认状态是 "未悬停"
onLeave()Draggable
NextSelectionDown
代码示例
创建游戏手柄选择网格
-- 使用下面的代码设置游戏手柄选择网格
local container = script.Parent:FindFirstChild("Container")
local grid = container:GetChildren()
local rowSize = container:FindFirstChild("UIGridLayout").FillDirectionMaxCells
for _, gui in pairs(grid) do
if gui:IsA("GuiObject") then
local pos = gui.Name
-- 左边缘
gui.NextSelectionLeft = container:FindFirstChild(pos - 1)
-- 右边缘
gui.NextSelectionRight = container:FindFirstChild(pos + 1)
-- 上面
gui.NextSelectionUp = container:FindFirstChild(pos - rowSize)
-- 下面
gui.NextSelectionDown = container:FindFirstChild(pos + rowSize)
end
end
-- 使用下面的代码测试游戏手柄选择网格
local GuiService = game:GetService("GuiService")
local UserInputService = game:GetService("UserInputService")
GuiService.SelectedObject = container:FindFirstChild("1")
function updateSelection(input)
if input.UserInputType == Enum.UserInputType.Keyboard then
local key = input.KeyCode
local selectedObject = GuiService.SelectedObject
if not selectedObject then
return
end
if key == Enum.KeyCode.Up then
if not selectedObject.NextSelectionUp then
GuiService.SelectedObject = selectedObject
end
elseif key == Enum.KeyCode.Down then
if not selectedObject.NextSelectionDown then
GuiService.SelectedObject = selectedObject
end
elseif key == Enum.KeyCode.Left then
if not selectedObject.NextSelectionLeft then
GuiService.SelectedObject = selectedObject
end
elseif key == Enum.KeyCode.Right then
if not selectedObject.NextSelectionRight then
GuiService.SelectedObject = selectedObject
end
end
end
end
UserInputService.InputBegan:Connect(updateSelection)NextSelectionLeft
代码示例
创建游戏手柄选择网格
-- 使用下面的代码设置游戏手柄选择网格
local container = script.Parent:FindFirstChild("Container")
local grid = container:GetChildren()
local rowSize = container:FindFirstChild("UIGridLayout").FillDirectionMaxCells
for _, gui in pairs(grid) do
if gui:IsA("GuiObject") then
local pos = gui.Name
-- 左边缘
gui.NextSelectionLeft = container:FindFirstChild(pos - 1)
-- 右边缘
gui.NextSelectionRight = container:FindFirstChild(pos + 1)
-- 上面
gui.NextSelectionUp = container:FindFirstChild(pos - rowSize)
-- 下面
gui.NextSelectionDown = container:FindFirstChild(pos + rowSize)
end
end
-- 使用下面的代码测试游戏手柄选择网格
local GuiService = game:GetService("GuiService")
local UserInputService = game:GetService("UserInputService")
GuiService.SelectedObject = container:FindFirstChild("1")
function updateSelection(input)
if input.UserInputType == Enum.UserInputType.Keyboard then
local key = input.KeyCode
local selectedObject = GuiService.SelectedObject
if not selectedObject then
return
end
if key == Enum.KeyCode.Up then
if not selectedObject.NextSelectionUp then
GuiService.SelectedObject = selectedObject
end
elseif key == Enum.KeyCode.Down then
if not selectedObject.NextSelectionDown then
GuiService.SelectedObject = selectedObject
end
elseif key == Enum.KeyCode.Left then
if not selectedObject.NextSelectionLeft then
GuiService.SelectedObject = selectedObject
end
elseif key == Enum.KeyCode.Right then
if not selectedObject.NextSelectionRight then
GuiService.SelectedObject = selectedObject
end
end
end
end
UserInputService.InputBegan:Connect(updateSelection)NextSelectionRight
代码示例
创建游戏手柄选择网格
-- 使用下面的代码设置游戏手柄选择网格
local container = script.Parent:FindFirstChild("Container")
local grid = container:GetChildren()
local rowSize = container:FindFirstChild("UIGridLayout").FillDirectionMaxCells
for _, gui in pairs(grid) do
if gui:IsA("GuiObject") then
local pos = gui.Name
-- 左边缘
gui.NextSelectionLeft = container:FindFirstChild(pos - 1)
-- 右边缘
gui.NextSelectionRight = container:FindFirstChild(pos + 1)
-- 上面
gui.NextSelectionUp = container:FindFirstChild(pos - rowSize)
-- 下面
gui.NextSelectionDown = container:FindFirstChild(pos + rowSize)
end
end
-- 使用下面的代码测试游戏手柄选择网格
local GuiService = game:GetService("GuiService")
local UserInputService = game:GetService("UserInputService")
GuiService.SelectedObject = container:FindFirstChild("1")
function updateSelection(input)
if input.UserInputType == Enum.UserInputType.Keyboard then
local key = input.KeyCode
local selectedObject = GuiService.SelectedObject
if not selectedObject then
return
end
if key == Enum.KeyCode.Up then
if not selectedObject.NextSelectionUp then
GuiService.SelectedObject = selectedObject
end
elseif key == Enum.KeyCode.Down then
if not selectedObject.NextSelectionDown then
GuiService.SelectedObject = selectedObject
end
elseif key == Enum.KeyCode.Left then
if not selectedObject.NextSelectionLeft then
GuiService.SelectedObject = selectedObject
end
elseif key == Enum.KeyCode.Right then
if not selectedObject.NextSelectionRight then
GuiService.SelectedObject = selectedObject
end
end
end
end
UserInputService.InputBegan:Connect(updateSelection)NextSelectionUp
代码示例
创建游戏手柄选择网格
-- 使用下面的代码设置游戏手柄选择网格
local container = script.Parent:FindFirstChild("Container")
local grid = container:GetChildren()
local rowSize = container:FindFirstChild("UIGridLayout").FillDirectionMaxCells
for _, gui in pairs(grid) do
if gui:IsA("GuiObject") then
local pos = gui.Name
-- 左边缘
gui.NextSelectionLeft = container:FindFirstChild(pos - 1)
-- 右边缘
gui.NextSelectionRight = container:FindFirstChild(pos + 1)
-- 上面
gui.NextSelectionUp = container:FindFirstChild(pos - rowSize)
-- 下面
gui.NextSelectionDown = container:FindFirstChild(pos + rowSize)
end
end
-- 使用下面的代码测试游戏手柄选择网格
local GuiService = game:GetService("GuiService")
local UserInputService = game:GetService("UserInputService")
GuiService.SelectedObject = container:FindFirstChild("1")
function updateSelection(input)
if input.UserInputType == Enum.UserInputType.Keyboard then
local key = input.KeyCode
local selectedObject = GuiService.SelectedObject
if not selectedObject then
return
end
if key == Enum.KeyCode.Up then
if not selectedObject.NextSelectionUp then
GuiService.SelectedObject = selectedObject
end
elseif key == Enum.KeyCode.Down then
if not selectedObject.NextSelectionDown then
GuiService.SelectedObject = selectedObject
end
elseif key == Enum.KeyCode.Left then
if not selectedObject.NextSelectionLeft then
GuiService.SelectedObject = selectedObject
end
elseif key == Enum.KeyCode.Right then
if not selectedObject.NextSelectionRight then
GuiService.SelectedObject = selectedObject
end
end
end
end
UserInputService.InputBegan:Connect(updateSelection)Selectable
代码示例
限制文本框选择
local GuiService = game:GetService("GuiService")
local textBox = script.Parent
local function gainFocus()
textBox.Selectable = true
GuiService.SelectedObject = textBox
end
local function loseFocus(_enterPressed, _inputObject)
GuiService.SelectedObject = nil
textBox.Selectable = false
end
-- 当 textBox
-- 是 TextBox 类型时,FocusLost 和 FocusGained 事件会触发
textBox.Focused:Connect(gainFocus)
textBox.FocusLost:Connect(loseFocus)Size
代码示例
生命值条
local Players = game:GetService("Players")
local player = Players.LocalPlayer
-- 将脚本粘贴到一个 LocalScript 中,该脚本
-- 是一个在框架内的框架的父对象
local frame = script.Parent
local container = frame.Parent
container.BackgroundColor3 = Color3.new(0, 0, 0) -- 黑色
-- 当人形的生命值发生变化时调用此函数
local function onHealthChanged()
local human = player.Character.Humanoid
local percent = human.Health / human.MaxHealth
-- 改变内框的大小
frame.Size = UDim2.new(percent, 0, 1, 0)
-- 改变生命值条的颜色
if percent < 0.1 then
frame.BackgroundColor3 = Color3.new(1, 0, 0) -- 黑色
elseif percent < 0.4 then
frame.BackgroundColor3 = Color3.new(1, 1, 0) -- 黄色
else
frame.BackgroundColor3 = Color3.new(0, 1, 0) -- 绿色
end
end
-- 当角色生成时调用此函数
local function onCharacterAdded(character)
local human = character:WaitForChild("Humanoid")
-- 模式:一次更新,现在以及每次生命值变化时
human.HealthChanged:Connect(onHealthChanged)
onHealthChanged()
end
-- 连接我们的生成监听器;如果已经生成,则调用它
player.CharacterAdded:Connect(onCharacterAdded)
if player.Character then
onCharacterAdded(player.Character)
endTransparency
Visible
代码示例
用户界面窗口
local gui = script.Parent
local window = gui:WaitForChild("Window")
local toggleButton = gui:WaitForChild("ToggleWindow")
local closeButton = window:WaitForChild("Close")
local function toggleWindowVisbility()
-- 使用 `not` 关键字翻转布尔值
window.Visible = not window.Visible
end
toggleButton.Activated:Connect(toggleWindowVisbility)
closeButton.Activated:Connect(toggleWindowVisbility)方法
TweenPosition
TweenSize
TweenSizeAndPosition
活动
DragBegin
DragStopped
InputBegan
参数
代码示例
追踪 GuiObject 上输入的开始
-- 为了使用 InputBegan 事件,您必须指定 GuiObject
local gui = script.Parent
-- 一个提供多种用户输入类型用法案例的示例函数
local function inputBegan(input)
if input.UserInputType == Enum.UserInputType.Keyboard then
print("一个按键被按下!按键:", input.KeyCode)
elseif input.UserInputType == Enum.UserInputType.MouseButton1 then
print("左键鼠标按钮已被按下,位置为:", input.Position)
elseif input.UserInputType == Enum.UserInputType.MouseButton2 then
print("右键鼠标按钮已被按下,位置为:", input.Position)
elseif input.UserInputType == Enum.UserInputType.Touch then
print("触摸屏输入已在位置开始:", input.Position)
elseif input.UserInputType == Enum.UserInputType.Gamepad1 then
print("游戏手柄上的一个按钮被按下!按钮:", input.KeyCode)
end
end
gui.InputBegan:Connect(inputBegan)InputChanged
参数
代码示例
GuiObject 输入改变演示
local UserInputService = game:GetService("UserInputService")
local gui = script.Parent
local function printMovement(input)
print("位置:", input.Position)
print("移动增量:", input.Delta)
end
local function inputChanged(input)
if input.UserInputType == Enum.UserInputType.MouseMovement then
print("鼠标已移动!")
printMovement(input)
elseif input.UserInputType == Enum.UserInputType.MouseWheel then
print("鼠标滚轮已滚动!")
print("滚轮移动:", input.Position.Z)
elseif input.UserInputType == Enum.UserInputType.Gamepad1 then
if input.KeyCode == Enum.KeyCode.Thumbstick1 then
print("左摇杆已移动!")
printMovement(input)
elseif input.KeyCode == Enum.KeyCode.Thumbstick2 then
print("右摇杆已移动!")
printMovement(input)
elseif input.KeyCode == Enum.KeyCode.ButtonL2 then
print("施加在左扳机上的压力已改变!")
print("压力:", input.Position.Z)
elseif input.KeyCode == Enum.KeyCode.ButtonR2 then
print("施加在右扳机上的压力已改变!")
print("压力:", input.Position.Z)
end
elseif input.UserInputType == Enum.UserInputType.Touch then
print("用户的手指正在屏幕上移动!")
printMovement(input)
elseif input.UserInputType == Enum.UserInputType.Gyro then
local _rotInput, rotCFrame = UserInputService:GetDeviceRotation()
local rotX, rotY, rotZ = rotCFrame:toEulerAnglesXYZ()
local rot = Vector3.new(math.deg(rotX), math.deg(rotY), math.deg(rotZ))
print("用户的移动设备的旋转已改变!")
print("位置", rotCFrame.p)
print("旋转:", rot)
elseif input.UserInputType == Enum.UserInputType.Accelerometer then
print("用户的移动设备的加速度已改变!")
printMovement(input)
end
end
gui.InputChanged:Connect(inputChanged)InputEnded
参数
代码示例
跟踪 GuiObject 的输入结束
-- 要使用 InputChanged 事件,您必须指定一个 GuiObject
local gui = script.Parent
-- 一个示例函数提供了各种用户输入类型的多种使用案例
local function inputEnded(input)
if input.UserInputType == Enum.UserInputType.Keyboard then
print("一个键已被释放! 键:", input.KeyCode)
elseif input.UserInputType == Enum.UserInputType.MouseButton1 then
print("左键鼠标已在", input.Position, "释放")
elseif input.UserInputType == Enum.UserInputType.MouseButton2 then
print("右键鼠标已在", input.Position, "释放")
elseif input.UserInputType == Enum.UserInputType.Touch then
print("触摸屏输入已在", input.Position, "释放")
elseif input.UserInputType == Enum.UserInputType.Gamepad1 then
print("游戏手柄上的一个按钮已被释放! 按钮:", input.KeyCode)
end
end
gui.InputEnded:Connect(inputEnded)MouseEnter
代码示例
打印鼠标进入GuiObject的位置
local guiObject = script.Parent
guiObject.MouseEnter:Connect(function(x, y)
print("用户的鼠标光标已进入GuiObject,位置为", x, ",", y)
end)MouseWheelBackward
SelectionGained
代码示例
处理 GUI 选择获得
local guiObject = script.Parent
local function selectionGained()
print("用户使用游戏手柄选择了这个按钮.")
end
guiObject.SelectionGained:Connect(selectionGained)SelectionLost
代码示例
处理 GUI 选择丢失
local guiObject = script.Parent
local function selectionLost()
print("用户不再用他们的游戏手柄选择这个。")
end
guiObject.SelectionLost:Connect(selectionLost)TouchLongPress
参数
代码示例
按住长按移动 UI 元素
local frame = script.Parent
frame.Active = true
local dragging = false
local basePosition
local startTouchPosition
local borderColor3
local backgroundColor3
local function onTouchLongPress(touchPositions, state)
if state == Enum.UserInputState.Begin and not dragging then
-- 开始拖动
dragging = true
basePosition = frame.Position
startTouchPosition = touchPositions[1]
-- 改变框的颜色以表示正在拖动
borderColor3 = frame.BorderColor3
backgroundColor3 = frame.BackgroundColor3
frame.BorderColor3 = Color3.new(1, 1, 1) -- 白色
frame.BackgroundColor3 = Color3.new(0, 0, 1) -- 蓝色
elseif state == Enum.UserInputState.Change then
local touchPosition = touchPositions[1]
local deltaPosition =
UDim2.new(0, touchPosition.X - startTouchPosition.X, 0, touchPosition.Y - startTouchPosition.Y)
frame.Position = basePosition + deltaPosition
elseif state == Enum.UserInputState.End and dragging then
-- 停止拖动
dragging = false
frame.BorderColor3 = borderColor3
frame.BackgroundColor3 = backgroundColor3
end
end
frame.TouchLongPress:Connect(onTouchLongPress)TouchPan
GuiObject.TouchPan(
参数
代码示例
平移 UI 元素
local innerFrame = script.Parent
local outerFrame = innerFrame.Parent
outerFrame.BackgroundTransparency = 0.75
outerFrame.Active = true
outerFrame.Size = UDim2.new(1, 0, 1, 0)
outerFrame.Position = UDim2.new(0, 0, 0, 0)
outerFrame.AnchorPoint = Vector2.new(0, 0)
outerFrame.ClipsDescendants = true
local dragging = false
local basePosition
local function onTouchPan(_touchPositions, totalTranslation, _velocity, state)
if state == Enum.UserInputState.Begin and not dragging then
dragging = true
basePosition = innerFrame.Position
outerFrame.BackgroundTransparency = 0.25
elseif state == Enum.UserInputState.Change then
innerFrame.Position = basePosition + UDim2.new(0, totalTranslation.X, 0, totalTranslation.Y)
elseif state == Enum.UserInputState.End and dragging then
dragging = false
outerFrame.BackgroundTransparency = 0.75
end
end
outerFrame.TouchPan:Connect(onTouchPan)TouchPinch
GuiObject.TouchPinch(
参数
代码示例
捏合/拉伸缩放
local innerFrame = script.Parent
local outerFrame = innerFrame.Parent
outerFrame.BackgroundTransparency = 0.75
outerFrame.Active = true
outerFrame.Size = UDim2.new(1, 0, 1, 0)
outerFrame.Position = UDim2.new(0, 0, 0, 0)
outerFrame.AnchorPoint = Vector2.new(0, 0)
outerFrame.ClipsDescendants = true
local dragging = false
local uiScale = Instance.new("UIScale")
uiScale.Parent = innerFrame
local baseScale
local function onTouchPinch(_touchPositions, scale, _velocity, state)
if state == Enum.UserInputState.Begin and not dragging then
dragging = true
baseScale = uiScale.Scale
outerFrame.BackgroundTransparency = 0.25
elseif state == Enum.UserInputState.Change then
uiScale.Scale = baseScale * scale -- 注意这里的乘法
elseif state == Enum.UserInputState.End and dragging then
dragging = false
outerFrame.BackgroundTransparency = 0.75
end
end
outerFrame.TouchPinch:Connect(onTouchPinch)TouchRotate
GuiObject.TouchRotate(
参数
代码示例
触摸旋转
local innerFrame = script.Parent
local outerFrame = innerFrame.Parent
outerFrame.BackgroundTransparency = 0.75
outerFrame.Active = true
outerFrame.Size = UDim2.new(1, 0, 1, 0)
outerFrame.Position = UDim2.new(0, 0, 0, 0)
outerFrame.AnchorPoint = Vector2.new(0, 0)
outerFrame.ClipsDescendants = true
local dragging = false
local baseRotation = innerFrame.Rotation
local function onTouchRotate(_touchPositions, rotation, _velocity, state)
if state == Enum.UserInputState.Begin and not dragging then
dragging = true
baseRotation = innerFrame.Rotation
outerFrame.BackgroundTransparency = 0.25
elseif state == Enum.UserInputState.Change then
innerFrame.Rotation = baseRotation + rotation
elseif state == Enum.UserInputState.End and dragging then
dragging = false
outerFrame.BackgroundTransparency = 0.75
end
end
outerFrame.TouchRotate:Connect(onTouchRotate)TouchSwipe
参数
代码示例
跳动的颜色选择器
local frame = script.Parent
frame.Active = true
-- 成功滑动时框架应跳动的距离
local BOUNCE_DISTANCE = 50
-- 框架的当前状态
local basePosition = frame.Position
local hue = 0
local saturation = 128
local function updateColor()
frame.BackgroundColor3 = Color3.fromHSV(hue / 256, saturation / 256, 1)
end
local function onTouchSwipe(swipeDir, _touchCount)
-- 根据滑动方向改变 BackgroundColor3
local deltaPos
if swipeDir == Enum.SwipeDirection.Right then
deltaPos = UDim2.new(0, BOUNCE_DISTANCE, 0, 0)
hue = (hue + 16) % 255
elseif swipeDir == Enum.SwipeDirection.Left then
deltaPos = UDim2.new(0, -BOUNCE_DISTANCE, 0, 0)
hue = (hue - 16) % 255
elseif swipeDir == Enum.SwipeDirection.Up then
deltaPos = UDim2.new(0, 0, 0, -BOUNCE_DISTANCE)
saturation = (saturation + 16) % 255
elseif swipeDir == Enum.SwipeDirection.Down then
deltaPos = UDim2.new(0, 0, 0, BOUNCE_DISTANCE)
saturation = (saturation - 16) % 255
else
deltaPos = UDim2.new()
end
-- 更新颜色并稍微跳动框架
updateColor()
frame.Position = basePosition + deltaPos
frame:TweenPosition(basePosition, Enum.EasingDirection.Out, Enum.EasingStyle.Bounce, 0.7, true)
end
frame.TouchSwipe:Connect(onTouchSwipe)
updateColor()TouchTap
参数
代码示例
点击透明度切换
local frame = script.Parent
frame.Active = true
local function onTouchTap()
-- 切换背景透明度
if frame.BackgroundTransparency > 0 then
frame.BackgroundTransparency = 0
else
frame.BackgroundTransparency = 0.75
end
end
frame.TouchTap:Connect(onTouchTap)