📜  如何将滚动条转到底部 roblox (1)

📅  最后修改于: 2023-12-03 15:09:07.899000             🧑  作者: Mango

如何将滚动条转到底部 Roblox

在 Roblox 中,我们有时需要将游戏进度条滚动到底部。下面介绍几种方法可以达到这个目的。

方法一:

使用 ScrollingFrame,然后设置 CanvasPosition 属性。

local scrollingFrame = script.Parent.ScrollingFrame
scrollingFrame.CanvasPosition = Vector2.new(0, scrollingFrame.CanvasSize.Y.Offset)

以上代码会将滚动条滑块移动到底部。

方法二:

使用 UserInputServiceCamera,然后通过相机跟踪滚动条位置。

local userInputService = game:GetService("UserInputService")
local camera = workspace.CurrentCamera

local scrollingFrame = script.Parent.ScrollingFrame
local canvasPosition = Vector2.new()

ScrollingFrame.CanvasPosition = Vector2.new(0, ScrollingFrame.CanvasSize.Y.Offset)

local function OnScroll(input, gameProcessedEvent)
    if not gameProcessedEvent then
        local delta = input.Delta.Y
        canvasPosition = canvasPosition + Vector2.new(0, delta * 16)
        canvasPosition = Vector2.new(0, math.clamp(canvasPosition.Y, 0, scrollingFrame.CanvasSize.Y.Offset))
        scrollingFrame.CanvasPosition = canvasPosition
    end
end

userInputService.InputChanged:Connect(function(input, gameProcessedEvent)
    if input.UserInputType == Enum.UserInputType.MouseWheel then
        OnScroll(input, gameProcessedEvent)
    end
end)

camera:GetPropertyChangedSignal("CoordinateFrame"):Connect(function()
    if camera.Focus and camera.Focus:IsDescendantOf(scrollingFrame) then
        local offset = camera.Focus.Position.Y - scrollingFrame.AbsolutePosition.Y
        if offset > scrollingFrame.AbsoluteSize.Y then
            canvasPosition = Vector2.new(0, canvasPosition.Y + offset - scrollingFrame.AbsoluteSize.Y)
            scrollingFrame.CanvasPosition = canvasPosition
        end
    end
end)

以上代码会在鼠标滚轮滑动时滚动条跟随滚动,并且在相机焦点移动时跟踪相机位置。

以上均为一些简单的方法,如果需要实现更高级的效果,可以考虑使用 TweenService 等工具来实现。