文档

滚动

滚动视图(Rust 中的 scroll(),TSX 中的 <Scroll>)就是平台自己的滚动视图(iOS 上的 UIScrollView,Android 上的 ScrollView):惯性滑动、回弹、滚动条、下拉刷新以及吸附都是原生实现。你的代码通过事件得知滚动情况,也可以把滚动偏移绑定到动画值上,做出逐帧跟随手指的效果。

guide-scrolling.tsx
guide-scrolling.tsx运行中
import { mount, Scroll, type ScrollRef, signal, Text, View } from "nsh";
import { createAnimatedValue } from "nsh";
import { button, buttonText, desktop, size, tint, ui, wide } from "./palette";

// Tints of the info, violet, danger, warn and up colours under the text colour.
const hues = ["info", "violet", "down", "warn", "up", "faint"] as const;
const hue = (i: number) => tint(ui()[hues[i % hues.length]], 0.28);

/** Pages as wide as the scroll view, with a page indicator. */
function Pager() {
  const width = signal(340);
  const page = signal(0);
  return (
    <View style={{ gap: 8 }}>
      <Scroll
        horizontal
        pagingEnabled
        scrollbar={false}
        style={{ height: 110, borderRadius: 12 }}
        onLayout={({ bounds }) => width.set(bounds.size.width)}
        onMomentumEnd={({ offset }) => page.set(Math.round(offset.x / width()))}
        onScrollEnd={({ offset }) => page.set(Math.round(offset.x / width()))}
      >
        <View style={{ flexDirection: "row" }}>
          <Each of={[0, 1, 2, 3]}>
            {(i) => (
              <View style={{ width: width(), height: 110, backgroundColor: hue(i()), alignItems: "center", justifyContent: "center" }}>
                <Text style={{ color: ui().text, fontSize: 24, fontWeight: "700" }}>Page {i() + 1}</Text>
              </View>
            )}
          </Each>
        </View>
      </Scroll>
      <View style={{ flexDirection: "row", justifyContent: "center", gap: 6 }}>
        <Each of={[0, 1, 2, 3]}>
          {(i) => <View style={{ width: 8, height: 8, borderRadius: 4, backgroundColor: page() === i() ? ui().accent : ui().border }} />}
        </Each>
      </View>
    </View>
  );
}

/** Cards that snap: one stop every card plus gap. */
function Cards() {
  return (
    <Scroll horizontal scrollbar={false} snapToInterval={152} decelerationRate="fast" style={{ height: 90 }}>
      <View style={{ flexDirection: "row", gap: 12, paddingHorizontal: 4 }}>
        <Each of={Array.from({ length: 12 }, (_, i) => i)}>
          {(i) => (
            <View
              style={{
                width: 140,
                height: 90,
                borderRadius: 14,
                backgroundColor: hue(i()),
                boxShadow: "inset 0 1px 0 rgba(255,255,255,0.08)",
                alignItems: "center",
                justifyContent: "center",
              }}
            >
              <Text style={{ color: ui().text, fontWeight: "700" }}>Card {i() + 1}</Text>
            </View>
          )}
        </Each>
      </View>
    </Scroll>
  );
}

function App() {
  // The vertical offset, written natively in the frame it changes.
  const offset = createAnimatedValue(0);
  const y = signal(0);
  const refreshing = signal(false);
  const refreshed = signal(0);
  let scroll!: ScrollRef;
  const refresh = () => {
    refreshing.set(true);
    setTimeout(() => {
      refreshed.update((n) => n + 1);
      refreshing.set(false);
    }, 1200);
  };
  return (
    <View style={{ flex: 1, backgroundColor: ui().page, paddingTop: 24 }}>
      {/* A bar that fades in and shrinks its title as the page scrolls: no JS per frame. */}
      <View
        style={{
          height: 48,
          paddingHorizontal: 16,
          justifyContent: "center",
          backgroundColor: offset.interpolate({ inputRange: [0, 80], outputRange: [ui().page, ui().card.backgroundColor] }),
          borderBottomWidth: 1,
          borderBottomColor: ui().line,
        }}
      >
        <Text
          style={{
            fontSize: 22,
            fontWeight: "700",
            color: ui().text,
            transformOrigin: "left center",
            scale: offset.interpolate({ inputRange: [0, 80], outputRange: [1, 0.8] }),
          }}
        >
          Scrolling
        </Text>
      </View>
      <Scroll
        ref={scroll}
        style={{ flex: 1 }}
        scrollValue={offset}
        scrollEventThrottle={100}
        onScroll={(e) => y.set(Math.round(e.offset.y))}
        refreshing={refreshing()}
        onRefresh={refresh}
        keyboardDismissMode="on-drag"
      >
        <View style={{ padding: 16, gap: 18, maxWidth: wide() ? 720 : undefined }}>
          <Text style={{ color: ui().muted }}>
            {desktop ? `Scroll with the wheel or the trackpad. onScroll: y = ${y()}` : `Pull down to refresh (${refreshed()}×). onScroll: y = ${y()}`}
          </Text>
          <Text style={{ fontWeight: "600", color: ui().text }}>pagingEnabled</Text>
          <Pager />
          <Text style={{ fontWeight: "600", color: ui().text }}>snapToInterval</Text>
          <Cards />
          <View style={{ gap: desktop ? 8 : 18 }}>
            <Each of={Array.from({ length: 24 }, (_, i) => i + 1)}>
              {(n) => (
                <View
                  style={
                    desktop
                      ? { ...ui().row, height: size.row, justifyContent: "center", paddingHorizontal: 12, borderRadius: 8 }
                      : { ...ui().row, padding: 14, borderRadius: 12 }
                  }
                >
                  <Text style={{ color: ui().text, fontSize: desktop ? size.label : undefined }}>Row {n()}</Text>
                </View>
              )}
            </Each>
          </View>
          <View
            onPress={() => scroll.scrollTo({ y: 0 })}
            {...button("primary")}
          >
            <Text style={buttonText("primary")}>Back to the top (ref.scrollTo)</Text>
          </View>
        </View>
      </Scroll>
    </View>
  );
}

mount(App);

滚动视图适合尺寸已知、规模适中的内容:设置页、表单、文章、一排标签。长列表或无限数据请用列表,它会复用行,并接受同样的滚动属性。

#滚动视图

滚动视图本身是视口,它的子元素是内容。视口需要父元素给出尺寸(通常是 flex: 1、.flex_1());内容按视口的宽度(纵向)或高度(横向)排版,长度按需延伸。

import { mount, Scroll, Text, View } from "nsh";
import { Window } from "nsh/desktop";

function App() {
  return (
    <Scroll style={{ flex: 1, backgroundColor: "#f6f8fa" }}>
      <View style={{ padding: 16, gap: 12 }}>
        <Each of={Array.from({ length: 50 }, (_, i) => i + 1)}>
          {(n) => (
            <View style={{ padding: 16, borderRadius: 10, backgroundColor: "#ffffff" }}>
              <Text>Row {n()}</Text>
            </View>
          )}
        </Each>
      </View>
    </Scroll>
  );
}

mount(() => (
  <Window id="main">
    <App />
  </Window>
));

上面的子视图就是滚动视图的内容容器;在 TSX 中,contentStyle(或 contentClass)会替你创建一个,把子元素包进一个带该样式、随内容一起滚动的视图。flexGrow: 1(.flex_grow(1.0))让内容至少和视口一样高,内容较短时可以居中,较长时照常滚动:

<Scroll style={{ flex: 1 }} contentStyle={{ flexGrow: 1, justifyContent: "center", padding: 16 }}>
  <If when={messages().length > 0} else={<Text>No messages yet</Text>}>
    <Each of={messages()}>{(m) => <Text>{m().text}</Text>}</Each>
  </If>
</Scroll>

横向滚动视图使用 horizontal(.horizontal()),并以一个横排视图作为子元素:

<Scroll horizontal scrollbar={false}>
  <View style={{ flexDirection: "row", gap: 8, paddingHorizontal: 16 }}>
    <Each of={["Today", "This week", "This month", "This year"]}>
      {(label) => <Text style={{ paddingHorizontal: 14, paddingVertical: 8, borderRadius: 16, overflow: "hidden", backgroundColor: "#ffffff" }}>{label()}</Text>}
    </Each>
  </View>
</Scroll>

在从右到左布局中(见设备与环境),横向滚动视图从最右端开始,并保持与右端的距离。

手指拖动滚动视图。在桌面(macOS、Windows、Linux)和浏览器中,鼠标像在桌面应用中一样滚动:用滚轮(Shift + 滚轮横向滚动)、触控板、键盘和滚动指示条(鼠标在视图上移动时显示,可以像滚动条一样拖动);鼠标拖动交给应用的拖拽手势、响应者和文字选择。桌面上的触摸屏像手指一样拖动滚动视图。scrollbar={false} 在两个方向上都隐藏指示条:没有横向滚轮的鼠标用 Shift + 滚轮、在周围没有纵向滚动时用滚轮,以及用键盘来移动横向视图。

#行为属性

属性 Rust 类型 默认值 说明
horizontal .horizontal() boolean false 横向滚动。
scrollable .scrollable(..) boolean true 用户可以滚动。从代码滚动不受影响。
bounces .bounces(..) boolean true 允许越过边缘回弹。
smoothWheel .smooth_wheel(..) boolean false 鼠标滚轮每一格平滑滑到目标位置,像 Chrome 那样在 100–200 ms 内先加速后减速,连续拨动是一整段滑动。macOS、Windows、Linux 和 GPUI 生效(系统开启减弱动态效果时不滑);iOS、Android 和 Web 沿用系统和浏览器自己的滚动。触控板不受影响。
wheelLineHeight .wheel_line_height(px(..)) number 0 鼠标滚轮每行滚动多少点;0 保持平台默认(macOS 10,Windows 16,Linux 随视图大小)。Chrome 和 Safari 在 macOS 上每行 40 点。桌面平台生效。
scrollbar .scrollbar(..) boolean true 显示滚动条。
contentInset .content_inset(EdgeInsets { .. }) Partial<Edges> 内容四周可以滚动进入的留白。
scrollbarInset .scrollbar_inset(EdgeInsets { .. }) Partial<Edges> 滚动指示条与视图各边保持的距离。
contentOffset .content_offset(point(..)) { x?, y? } 初始位置。
decelerationRate .deceleration(rate) "normal" | "fast" | number 平台默认 惯性滑动减速的快慢:"normal"、"fast",或每毫秒保留的速度比例(0.998 为 normal,0.99 为 fast;0 表示平台默认)。

#分页与吸附

吸附在手指抬起时由原生决定,依据松手位置、预计滑行距离和速度;随后滚动视图滑到选中的停靠点,整个过程不涉及你的代码。

属性 Rust 类型 说明
pagingEnabled .paging(true) boolean 停在视口尺寸的整数倍处。
snapToInterval .snap_interval(px(..)) number 停在该长度的整数倍处(0 为关闭)。
snapToOffsets .snap_offsets(vec![..]) number[] 停在这些偏移处。
snapToAlignment .snap_alignment(SnapAlignment::Center) "start" | "center" | "end" 配合 snapToInterval:条目的哪个位置与视口对齐。
disableIntervalMomentum .disable_interval_momentum(true) boolean 一次快速滑动最多移动一个停靠点。

一个分页器:每页与滚动视图等宽,滑行结束时更新页码指示器。

import { mount, type Point, Scroll, signal, Text, View } from "nsh";
import { Window } from "nsh/desktop";

const colors = ["#0969da", "#8250df", "#bf3989", "#1a7f37"];

function Pager() {
  const width = signal(0);
  const page = signal(0);
  const settle = ({ offset }: { offset: Point }) => page.set(Math.round(offset.x / width()));
  return (
    <View style={{ gap: 8, padding: 16 }}>
      <Scroll
        horizontal
        pagingEnabled
        scrollbar={false}
        style={{ height: 160, borderRadius: 12 }}
        onLayout={({ bounds }) => width.set(bounds.size.width)}
        onScrollEnd={settle}
      >
        <View style={{ flexDirection: "row" }}>
          <Each of={colors}>
            {(color, i) => (
              <View style={{ width: width(), height: 160, backgroundColor: color(), alignItems: "center", justifyContent: "center" }}>
                <Text style={{ color: "#ffffff", fontSize: 24, fontWeight: "700" }}>Page {i() + 1}</Text>
              </View>
            )}
          </Each>
        </View>
      </Scroll>
      <View style={{ flexDirection: "row", justifyContent: "center", gap: 6 }}>
        <Each of={colors}>
          {(_, i) => <View style={{ width: 8, height: 8, borderRadius: 4, backgroundColor: page() === i() ? "#0969da" : "#d0d7de" }} />}
        </Each>
      </View>
    </View>
  );
}

mount(() => (
  <Window id="main">
    <Pager />
  </Window>
));

吸附到中间、每次滑动只移动一张的卡片轮播:

<Scroll
  horizontal
  snapToInterval={232}          // 卡片宽度 (220) + 间距 (12)
  snapToAlignment="center"
  decelerationRate="fast"
  disableIntervalMomentum
  scrollbar={false}
>
  <View style={{ flexDirection: "row", gap: 12, paddingHorizontal: 16 }}>…</View>
</Scroll>

列表接受同样的吸附属性,长轮播用它更合适。

#滚动事件

属性 Rust 参数 触发时机
onScroll .on_scroll(..) ScrollEvent 偏移改变。每帧最多一次;scrollEventThrottle 可以进一步拉开间隔。
scrollEventThrottle .event_throttle(ms) number 两次 onScroll 之间的最少毫秒数。
onScrollDragStart .on_scroll_drag_start(..) { offset } 用户开始拖动。
onScrollDragEnd .on_scroll_drag_end(..) { offset } 用户抬起手指。
onMomentumStart .on_momentum_start(..) { offset } 快速滑动后的惯性滑行开始。
onMomentumEnd .on_momentum_end(..) { offset } 惯性滑行结束(被新的拖动打断时也会触发)。
onScrollEnd .on_scroll_end(..) { offset } 用户的滚动停了下来:惯性滑行结束后,或没有惯性的拖动结束时。

ScrollEvent 包含偏移(offset,一个 Point;Rust 中同名)、内容尺寸,含缩放(contentSize,一个 Size;content_size)、视口(size)以及 zoomScale(zoom_scale)。其他事件携带偏移:TSX 中为 { offset },其值是一个 Point;Rust 中为 Point<Points>。

const atBottom = signal(false);

<Scroll
  style={{ flex: 1 }}
  scrollEventThrottle={100}
  onScroll={(e) => atBottom.set(e.offset.y + e.size.height >= e.contentSize.height - 20)}
>
  …
</Scroll>

#滚动联动效果:scrollValue

scrollValue(.scroll_value(&value))接收一个动画值,并在内容移动的同一帧,由原生把(主轴方向的)滚动偏移写进去。把这个值,或由它经 interpolate、diffClamp(diff_clamp)、算术运算得到的节点,绑定到样式上。

import { mount, Scroll, Text, View } from "nsh";
import { createAnimatedValue } from "nsh";
import { Window } from "nsh/desktop";

function App() {
  const offset = createAnimatedValue(0);
  return (
    <View style={{ flex: 1 }}>
      <View
        style={{
          height: 56,
          justifyContent: "center",
          paddingHorizontal: 16,
          // 顶部透明,滚过 80 点后变为白色。
          backgroundColor: offset.interpolate({ inputRange: [0, 80], outputRange: ["rgba(255,255,255,0)", "#ffffff"] }),
        }}
      >
        <Text
          style={{
            fontSize: 22,
            fontWeight: "700",
            transformOrigin: "left center",
            scale: offset.interpolate({ inputRange: [0, 80], outputRange: [1, 0.8] }),
          }}
        >
          Inbox
        </Text>
      </View>
      <Scroll style={{ flex: 1 }} scrollValue={offset}>
        <View style={{ padding: 16, gap: 8 }}>…</View>
      </Scroll>
    </View>
  );
}

mount(() => (
  <Window id="main">
    <App />
  </Window>
));

内容上滑时隐藏、一往下滑就重新出现的标题栏,就是对偏移做 diffClamp:

const offset = createAnimatedValue(0);
const header = offset.diffClamp(0, 56);

<View style={{ flex: 1 }}>
  <Scroll style={{ flex: 1 }} scrollValue={offset}>
    <View style={{ paddingTop: 56 }}>…</View>
  </Scroll>
  <View style={{ position: "absolute", top: 0, left: 0, right: 0, height: 56, translate: [0, header.mul(-1)] }}>…</View>
</View>

插值在输入范围之外默认会截断(clamp),所以下拉越过顶部(偏移为负)时效果停在终点;想做随下拉放大的弹性头图,传入 extrapolate: "extend"(Rust 中为 interpolate_with(.., Interpolation::extrapolate(Extrapolate::Extend)))。

#下拉刷新

refreshing 和 onRefresh(.refreshing(..) 和 .on_refresh(..))会加上平台的下拉刷新控件。refreshing 为 true 时控件显示;在 onRefresh 中把它设为 true,工作完成后再清除。

const refreshing = signal(false);
const reload = async () => {
  refreshing.set(true);
  try {
    items.set(await (await fetch("https://api.example.com/items")).json());
  } finally {
    refreshing.set(false);
  }
};

<Scroll style={{ flex: 1 }} refreshing={refreshing()} onRefresh={reload}>…</Scroll>

iOS 上的控件是 UIRefreshControl;在 Mac Catalyst 的 Mac 界面风格中,NSH 显示自己的加载指示器,行为相同。列表也接受这些属性。由于鼠标滚动时不拖动,在桌面和浏览器中下拉是触摸屏(或 macOS 的触控板,它能拉过顶部)的手势;请在这些地方另外提供刷新方式,比如按钮或键盘快捷键。

#自己的指示器

下拉的样子由应用决定。refreshIndicator={{ height }} 表示应用自己绘制指示器,高 height 点:平台的转圈隐藏,下拉到 height 即刷新,刷新中在内容上方为指示器留出 height。pullValue 把内容被拉过顶部的距离写进一个动画值:静止时为 0,随下拉增大,刷新中为 height。把指示器画在滚动视图顶部之上,用这个值插值出它的样式:样式在 UI 线程上跟随手指,每帧不经过 JS。

const pull = createAnimatedValue(0);

<View style={{ flex: 1 }}>
  <Scroll
    style={{ flex: 1 }}
    refreshing={refreshing()}
    onRefresh={reload}
    pullValue={pull}
    refreshIndicator={{ height: 72 }}
  >
    …
  </Scroll>
  <View
    style={{
      pointerEvents: "none",
      position: "absolute",
      top: 0,
      left: 0,
      right: 0,
      height: 72,
      alignItems: "center",
      justifyContent: "center",
      opacity: pull.interpolate({ inputRange: [0, 36], outputRange: [0, 1] }),
      transform: [{ rotate: pull.interpolate({ inputRange: [36, 72], outputRange: ["0deg", "180deg"] }) }],
    }}
  >
    <Arrow />
  </View>
</View>;

height 应不超过视口的一半:拉过顶部后,越往下拉阻力越大,太高就永远拉不到。在 iOS 上,应用自己的指示器会取代 UIRefreshControl,改由 NSH 自己检测下拉,和 Mac 界面风格一样。

#缩放

当最大缩放比例大于最小缩放比例时,滚动视图(列表保持 1 倍缩放)就可以缩放:捏合会缩放内容,放大后可以双向滚动。

属性 Rust 类型 默认值 说明
minimumZoomScale .zoom_range(min, max) number 1 最小缩放。
maximumZoomScale .zoom_range(min, max) number 1 最大缩放;大于最小值时捏合即可缩放。
zoomScale .zoom(scale) number 该属性改变时缩放到这个比例;否则保留用户的缩放。
bouncesZoom .bounces_zoom(..) boolean true 捏合可以越过限制并弹回。
zoomable .zoomable(..) boolean true 用户可以捏合缩放。
centerContent .center_content(..) boolean false 比视口小的内容(缩小时)居中显示。

滚动事件会报告缩放比例和缩放后的内容尺寸。在代码中,zoom_to(scale, center, animated) / zoomTo({ scale, x, y, animated }) 会缩放,使内容中的点(未缩放的点坐标)落在视口中央;双击放大就是在按压处理函数里调用它:

import { Image, mount, Scroll, type ScrollRef, signal } from "nsh";
import { Window } from "nsh/desktop";

function Photo() {
  let scroll!: ScrollRef;
  const scale = signal(1);
  return (
    <Scroll
      ref={scroll}
      style={{ flex: 1, backgroundColor: "#000000" }}
      minimumZoomScale={1}
      maximumZoomScale={4}
      centerContent
      onScroll={(e) => scale.set(e.zoomScale)}
    >
      <Image
        src="https://picsum.photos/id/1015/1200/800"
        style={{ width: 390, height: 260 }}
        onPress={() => scroll.zoomTo({ scale: scale() > 1.5 ? 1 : 3 })}
      />
    </Scroll>
  );
}

mount(() => (
  <Window id="main">
    <Photo />
  </Window>
));

在 iOS 上,缩放结束后,缩放视图里的文字和画布会按新的分辨率重新绘制,保持清晰。在 Android 上,NSH 使用自己的滚动视图实现缩放:围绕捏合焦点缩放,支持任意手指数平移、惯性滑动,并在边缘和缩放极限处产生橡皮筋效果。与系统的手势检测一致,Android 在两指间距达到约 27 mm 时开始捏合。

可缩放的滚动视图放在另一个滚动视图里时(例如图片查看器的横向分页),在触屏上(iOS、Android 和 Web)的表现与相册一致:未放大的图片上滑动会翻页,放大后滑动会平移图片,平移到图片边缘后,剩下的滑动会继续翻到下一页或上一页。

#从代码滚动

给滚动视图设置 ref(Rust 中用 element_ref() 和 .element_ref(&scroller),TSX 中用 ref)并调用它的方法。命令会在同一次更新的其他改动之后执行,所以滚动到刚刚添加的内容也没问题。

Rust TSX 说明
scroll_to(point, animated) scrollTo({ x?, y?, animated? }) 滚动到某个偏移。在 TSX 中默认带动画。
zoom_to(scale, center, animated) zoomTo({ scale, x?, y?, animated? }) 可缩放的滚动视图:缩放到 scale(限制在范围内),以内容点或中心为基准。
let scroll!: ScrollRef;

<Scroll ref={scroll} style={{ flex: 1 }}>…</Scroll>
<View onPress={() => scroll.scrollTo({ y: 0 })}>…</View>

列表另外还有 scrollToIndex、scrollToOffset 和 scrollToEnd;见列表。

#把视图滚到可见处

每个元素的 ref 都有 reveal:它按这次更新的布局,从内到外滚动视图外围的滚动视图,让视图显示出来。vertical 决定视图在纵向滚动视图里的位置,horizontal 决定在横向滚动视图里的位置:"start"(vertical 的默认值)、"center"、"end" 或 "nearest"(horizontal 的默认值)。"nearest" 下已经完整可见或铺满视口的视图不动,否则把它越出的那一边对齐到视口的同一边(比视口长的视图则把另一边对齐到视口的另一边)。"start" 和 "end" 指物理方向:纵向滚动视图里是上和下,横向滚动视图里是左和右,从左到右和从右到左的布局都一样。每个滚动视图从之前发给它的滚动所到的位置出发(无论平台是否已经回报),停在内容两端(含 contentInset 留出的部分)。视图的位置按布局计算,与 measure() 一样,因此不计入变换(包括动画),也不计入平台尚未应用的 zoomTo。默认带动画,animated: false 直接跳到位置。List 的行用列表自己的 scrollToIndex 滚动。

let row!: ViewRef;

<View ref={row}>…</View>
<View onPress={() => row.reveal({ vertical: "center" })}>…</View>

#滚动视图与键盘

NSH 会自动让获得焦点的输入框保持可见:键盘出现时,输入框所在的滚动视图会得到与键盘等高的底部 inset,并把输入框滚动到可见位置。这是原生完成的,应用中不需要任何代码。滚动视图之外的内容(例如固定在底部的消息栏)以 env(keyboard-inset-height) 作为底部内边距(见滚动视图之外的内容)。

有两个属性决定滚动和点击如何与键盘交互:

属性 Rust 类型 默认值 说明
keyboardDismissMode .keyboard_dismiss_mode(KeyboardDismissMode::OnDrag) "none" | "on-drag" | "interactive" "none" 拖动内容时是否收起键盘。iOS 上 "interactive" 让键盘随手指下移;Android 上它与 "on-drag" 相同。
keyboardShouldPersistTaps .keyboard_taps(KeyboardTaps::Handled) "never" | "always" | "handled" "never" 键盘弹出时点击的行为。

keyboardShouldPersistTaps:

  • "never":点击输入框之外只会收起键盘,按钮接收下一次点击。用户要先点一次收起键盘,再点一次才能按下。里面的视图都不会响应这次点击:不触发 onPress(手指按下时 onPressIn / onPressOut 和 active: 样式照常显示),responder 的触摸以 onResponderTerminate 而不是 onResponderRelease 结束,web view 等原生视图收不到点击。只有点击会被拦下并收起键盘:长按(onLongPress、上下文菜单)、拖动和滚动与键盘未弹出时一样。
  • "handled":点在会处理它的东西(按钮)上时,点击照常传递,键盘保持;点在空白处则收起键盘。表单通常应选它。
  • "always":键盘在每次点击后都保持显示。

两者都在点击或拖动发生时由原生决定,无需等待你的代码。要从代码收起键盘,在 Rust 中调用 dismiss_keyboard(),在 TSX 中调用 nsh 的 Keyboard.dismiss()。

#浮在滚动视图上的视图

放在滚动视图顶部或底部之上的工具栏、悬浮按钮可以指明它下面的滚动视图,平台就像对待那里的栏一样对待它。iOS 26 起,滚动视图的边缘效果会按这个视图显示的内容成形:滚到它下面的内容在它的按钮周围渐隐。这个边缘效果来自 iOS 26;其他版本和平台的界面需要同样的分隔时,由应用在视图下面画一层渐隐。用 contentInset 在内容那一端给这个视图留出位置。

const list = signal<ViewRef>();

<View style={{ flex: 1 }}>
  <Scroll ref={list} contentInset={{ bottom: 80 }}>{/* 行 */}</Scroll>
  <View
    style={{ position: "absolute", left: 0, right: 0, bottom: 0, padding: 16 }}
    ios={{ scrollEdgeElement: list() ? { scrollView: list()!, edge: "bottom" } : null }}
  >
    <Button><Text>New chat</Text></Button>
  </View>
</View>

Rust 中在浮着的视图上调用 .scroll_edge_element(Some(list), ScrollEdge::Bottom),list 是滚动视图的 ElementRef。edge 取 "top" 或 "bottom"(默认)。

#嵌套滚动视图

纵向滚动视图里放横向滚动视图会如预期工作:各自接管自己方向上的拖动。同方向的两个滚动视图也可以嵌套(内层先滚动)。需要独占手指的响应者,比如滚动视图里的绘图画布,会阻止外层滚动视图接管触摸:Rust 中用 .keep_touches(true),TSX 中在 onResponderGrant 中返回 true;见触摸与手势。

  • Scroll:元素参考,包含所有属性和事件。
  • 列表:使用相同滚动属性的虚拟化列表。
  • 动画:用于滚动联动效果的动画值、interpolate 和 diffClamp。
  • 表单与键盘:输入框、焦点和键盘。