概要
屬性
方法
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
範例程式碼
ScreenGui 中的 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
-- FocusLost 事件和 FocusGained 事件將會觸發,因為 textBox
-- 是 TextBox 類型
textBox.Focused:Connect(gainFocus)
textBox.FocusLost:Connect(loseFocus)Size
範例程式碼
健康條
local Players = game:GetService("Players")
local player = Players.LocalPlayer
-- 將腳本粘貼到一個 LocalScript 中,該 LocalScript
-- 被放置到一個 Frame 內部的 Frame 中
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)