UI Toolbox 徽标UI Toolbox
支持

本页为译文。技术支持和视频教程主要使用英语,内容以英文页面为准。 英文版

FlipView

UIToolbox 中的 FlipView 一次显示一页。每个子元素是一页。它也可以并排显示多页,例如角色选择中的三张卡片。

并排的三张英雄卡片,两侧有箭头,下方有圆点并排的三张英雄卡片,两侧有箭头,下方有圆点

<tb:FlipView
    name="tour"
    loop="true"
    transition="Fade"
    transition-duration="400"
    auto-advance="5000"
    style="height: 220px;">
    <ui:VisualElement><ui:Label text="Welcome" /></ui:VisualElement>
    <ui:VisualElement><ui:Label text="Swipe" /></ui:VisualElement>
    <ui:VisualElement><ui:Button text="Start" /></ui:VisualElement>
</tb:FlipView>
var flip = root.Q<FlipView>("tour");
flip.SelectionChanged += (old, now) => Debug.Log($"page {old} > {now}");
flip.AddPage(new Label("One more"));
flip.GoTo(0);

// A transition of your own; the view clears translate, scale, rotate and
// opacity after the change
flip.CustomTransition = (from, to, t, direction) =>
{
    from.style.opacity = 1 - t;
    to.style.scale = new Scale(Vector2.one * (0.7f + 0.3f * t));
};
成员 说明
Pages, PageCount, SelectedIndex (selected-index), SelectedPage 各页和当前显示的页。设置 SelectedIndex 会不带过渡地换页。
Next(), Previous(), GoTo(index, animate) 换页。
ItemsPerView (items-per-view) 并排显示的页数。默认 1。
Spacing (spacing) 视图显示多页时,两页之间的间距,单位为像素。默认 0。
Alignment (alignment) 视图显示多页时,选中的页所在的位置:Start(默认)或 Center。类型是 FlipViewAlignment。
StepSize (step-size) 视图显示多页时,一步走多远:Item(默认,一页)或 Page(所有显示的页)。类型是 FlipViewStepSize。
Loop (loop) 在最后一页按 Next 会回到第一页,在第一页按 Previous 会到最后一页。
Vertical (vertical) 各页上下排列:向上拖动显示下一页,Slide 让页面上下移动,上下键换页。箭头移到顶部和底部,圆点在右侧排成一列。
Transition (transition), TransitionDuration (transition-duration) 一个 FlipViewTransition:Slide(默认)、Fade 或 None,以及以毫秒为单位的时长(300)。
TransitionEasing (transition-easing) 换页的曲线,是缓动曲线之一。默认 OutCubic。使用 OutBack 或 OutElastic 时,各页会越过它们的位置再回来。
CustomTransition 替代 Transition 的 Action<from, to, t, direction>。t 从 0 到 1;TransitionEasing 为 OutBack 或 OutElastic 时,在两端之间会小于 0 或大于 1;direction 对下一页为 +1,对上一页为 -1。要写自己的滑动,请读取 Vertical 来选择轴。
ShowArrows (show-arrows), ShowIndicators (show-indicators) 上一页和下一页按钮以及圆点。默认开启。
AutoAdvance (auto-advance) 自动换页之间的毫秒数。0(默认)表示关闭。它总是循环回到第一页。
AddPage(page), Refresh() AddPage 添加一页并更新圆点。视图在面板中时,用 Add 或 Remove 添加或移除页面之后,请调用 Refresh。
IsTransitioning, SelectionChanged(old, now) 换页或拖动期间为 true。事件在换页结束之后发送。

每个视图多个项

<tb:FlipView
    name="heroes"
    items-per-view="3"
    spacing="12"
    alignment="Center"
    style="height: 160px;">
    <ui:VisualElement class="hero-card">
        <ui:Label text="Knight" />
    </ui:VisualElement>
    <ui:VisualElement class="hero-card">
        <ui:Label text="Mage" />
    </ui:VisualElement>
    <ui:VisualElement class="hero-card">
        <ui:Label text="Rogue" />
    </ui:VisualElement>
    <ui:VisualElement class="hero-card">
        <ui:Label text="Cleric" />
    </ui:VisualElement>
</tb:FlipView>
var heroes = root.Q<FlipView>("heroes");
heroes.SelectionChanged += (old, now) => showHero(heroes.SelectedPage);

把 ItemsPerView 设为大于 1。每页获得视图的相等一份(竖向时是高度的一份),减去页与页之间的 Spacing。视图会设置每页的 width(竖向时是 height)及其 translate。

提示

样式

类 元素
tb-flip-view 视图(flex 列,可伸展)。拖动期间会添加 tb-flip-view--dragging。
tb-flip-view--vertical 竖向视图:flex 行,箭头位于顶部和底部中央并旋转 90 度,圆点在右侧排成一列。
tb-flip-view__stage, tb-flip-view__viewport 页面和箭头所在的区域,以及被裁剪的页面区域(surface-alt 背景,中等圆角,获得焦点时有强调色边框)。
tb-flip-view__page 每一页:绝对定位,填满视口,隐藏。--shown 显示它;选中的页和正在进入的页带有它(每个视图多个项时,是视图内的页)。--current 标记选中的页。
tb-flip-view__arrow, --previous, --next 页面上方圆形的上一页和下一页按钮(叠加层背景,悬停时为强调色,禁用时变淡)。
tb-flip-view__indicators, tb-flip-view__dot, tb-flip-view__dot--selected 圆点所在的行、一个圆点(8 px,边框颜色),以及选中页的圆点(强调色)。

FlipViewDemo 场景展示了:四步的教程;视图中有三位英雄的角色选择;带有自己翻页效果的魔法书;按计时器翻页的新闻动态;以及木质游戏主题的关卡选择。