Mobile Scroller 滚动物理
提供独立于绘制的单轴滚动物理,支持拖拽、惯性、回弹和吸附。
何时使用
- 自定义滚动容器、轮盘或分页吸附时使用
mobile.Scroller。 - 只需要滚动页面内容时使用更高层的
mobile.ScrollView。 - 行高固定且数量很大时使用
ScrollViewW.Rows,不要手动为所有行创建盒子。 - 组件需要手势接管时,先用
Area或现成滚动组件,不要在 UI 回调里直接改位置而跳过物理状态。
在线体验
下方真实 gallery 展示下拉刷新和上拉加载;它们内部使用 Scroller 的惯性和边缘处理。
基本用法
Scroller 是纯状态类型:调用方传入当前时间给 Step,再读取 Pos。
import (
"time"
"github.com/tangoui/tango/ui/mobile"
)
func physics() float64 {
s := mobile.Scroller{Min: 0, Max: 720, Dim: 360}
s.BeginDrag()
s.DragBy(80)
s.Release(-240, time.Now())
s.Settle()
return s.Pos
}常见用法
拖拽与释放
拖拽开始前调用 BeginDrag,每次手指移动调用 DragBy,抬手时把速度交给 Release。
func drag(s *mobile.Scroller, now time.Time) {
s.BeginDrag()
s.DragBy(32)
s.Release(-180, now)
}逐帧推进惯性
在页面刷新循环中用同一个时钟持续调用 Step,返回 false 时表示已经停止。
func tick(s *mobile.Scroller, now time.Time) bool {
return s.Step(now)
}禁用回弹
NoBounce 让边缘位置直接夹紧,适合不希望内容越过边界的自定义滚动器。
func clamped() float64 {
s := mobile.Scroller{Min: 0, Max: 300, Dim: 200, NoBounce: true}
s.BeginDrag()
s.DragBy(-500)
s.Settle()
return s.Pos
}吸附到固定间距
Snap 表示从 Min 开始的吸附间距,适合分页和滚轮;需要明确目标时可调用 SnapTo。
func snapped(s *mobile.Scroller, now time.Time) {
s.Snap = 48
s.SnapTo(96, now)
}注意事项
Pos 是滚动偏移,不是屏幕坐标;内容向上或向左移动时偏移增大。每个滚动物理实例应放在稳定的页面状态中,不能每帧重新初始化,否则惯性会被清零。调用 Step 时使用单调递增时间。触摸事件、可见行裁剪和布局请优先交给 ScrollView。
示例代码
以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
Mobile · PullToRefresh / InfiniteList
gallery 演示 MobilePull 的 Render 函数,源码 page_mobile_gallery.go#L134。
加载中…
Mobile · 列表:List / Cell / SwipeCell / Badge / Tag
gallery 演示 MobileList 的 Render 函数,源码 page_mobile_gallery.go#L134。
加载中…
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
mobile.Decay
func Decay(p0, v0, t float64) (p, v float64)Decay returns the position and velocity t seconds into a fling that started at p0 with velocity v0 (px/s).
mobile.Spring
func Spring(x0, v0, omega, t float64) (x, v float64)Spring returns the displacement and velocity t seconds after a critically damped spring was released at displacement x0 with velocity v0 (target is 0).
mobile.Rubber
func Rubber(d, dim float64) float64Rubber maps an overshoot d (px past the edge, ≥ 0) to the displayed overshoot for a viewport of size dim.
相关类型与成员
mobile.Scroller
Scroller is the scroll physics of one axis: finger tracking with rubber band past the edges, inertial fling with exponential deceleration, critically damped bounce back, and optional snapping (picker wheels, pages). Positions grow as content moves up/left (like scroll offsets). It is pure: callers pass the time and read Pos.
Scroller 方法
mobile.Scroller:Scroller is the scroll physics of one axis: finger tracking with rubber band past the edges, inertial fling with exponential deceleration, critically damped bounce back, and optional snapping (picker wheels, pages). Positions grow as content moves up/left (like scroll offsets). It is pure: callers pass the time and read Pos.
| 字段 | 说明 | 类型 |
|---|---|---|
Pos | current offset | float64 |
Min | scroll range | float64 |
Max | scroll range | float64 |
Dim | viewport size on this axis (rubber band scale) | float64 |
Snap | > 0: rest only on multiples of Snap (from Min) | float64 |
NoBounce | NoBounce clamps at the edges instead of rubber-banding. | bool |
Mode | ScrollMode |
Scroller 的方法:
| 方法 | 说明 | 签名 |
|---|---|---|
Active | Active reports whether the scroller is moving on its own. | Active() bool |
BeginDrag | BeginDrag stops any motion and starts following the finger. | BeginDrag() |
DragBy | DragBy moves to the drag-start position plus delta (finger travel in content direction, i.e. negative finger movement for natural scrolling). | DragBy(delta float64) |
Release | Release ends a drag with velocity v (px/s, content direction) at time now and starts the fling, bounce or snap that follows. | Release(v float64, now time.Time) |
Settle | Settle jumps to the rest position of the current motion. | Settle() |
SnapTo | SnapTo glides to target (decelerating so it lands exactly there). | SnapTo(target float64, now time.Time) |
Step | Step advances the motion to now and reports whether it is still moving. | Step(now time.Time) bool |
Stop | Stop halts the motion where it is (a finger caught a fling). | Stop() |