vanilladocs
vanillapunch.com
API / ui/uisnapscrollview

ui/uisnapscrollview

스냅 스크롤뷰 컴포넌트.

Snap scroll view component.

UISnapScrollViewextends UIScrollView

짝Counterpart: vanilla-engine UISnapScrollView

스냅 스크롤뷰 컴포넌트.

  • ScrollView를 상속하며, 드래그 해제 시 지정한 아이템 중앙으로 스냅된다.
  • 아이템 크기가 각각 달라도 각 아이템 중앙이 뷰포트 중앙에 오도록 스냅된다.
  • 가로 스냅: setHorizontal(true), setVertical(false) (기본값)
  • 세로 스냅: setHorizontal(false), setVertical(true)

[스냅 동작 설정]

  • setSnapToNearest(false) : 1회 스와이프 시 1칸만 이동. (기본값) 끄는 동안 오프셋은 지금 칸 ±1 칸 범위를 넘지 않는다. 놓을 때 드래그 속도가 setVelocityThreshold 이상이면 그 방향으로 한 칸 넘기고(플링), 아니면 끈 거리가 옆 칸 크기의 setPageChangeThreshold 비율 이상일 때 넘긴다.
  • setSnapToNearest(true) : 드래그한 위치에서 가장 가까운 아이템으로 스냅. (끄는 동안 칸 범위를 자르지 않아 여러 칸을 지날 수 있다)
  • setVelocityThreshold(px/s): 1칸 모드에서 방향을 결정할 최소 속도. (기본값: 200, 스크롤 오프셋이 움직인 초당 픽셀)

[페이지 이동]

  • pageTo(index, smooth): 지정 인덱스로 이동. smooth=true이면 트윈, false이면 즉시 이동.

Snap scroll view component.

  • Inherits ScrollView, and snaps to the center of an item when a drag is released.
  • Even when items differ in size, each item center snaps to the viewport center.
  • Horizontal snap: setHorizontal(true), setVertical(false) (default)
  • Vertical snap: setHorizontal(false), setVertical(true)

[Snap behavior]

  • setSnapToNearest(false) : moves only one item per swipe. (default) While dragging, the offset never goes beyond one item on either side of the current one. On release, if the drag velocity is at least setVelocityThreshold it moves one item in that direction (fling), otherwise it moves when the dragged distance is at least the setPageChangeThreshold ratio of the neighbor item size.
  • setSnapToNearest(true) : snaps to the item nearest the dragged position. (The drag is not clamped to neighbor items, so it can pass several items)
  • setVelocityThreshold(px/s): minimum velocity that decides the direction in one-item mode. (default: 200, pixels per second the scroll offset moved)

[Paging]

  • pageTo(index, smooth): moves to the given index. smooth=true tweens, false moves immediately.
예제Example가로로 넘기는 다섯 쪽Five pages swiped horizontally
import { Engine, EngineConfiguration, UIScene, UISnapScrollView, UILabel, WorldNode, Paint, Color, Pivot, Vector2 } from "./build/vanilla.js";

class PagerScene extends UIScene {
	create() {
		super.create();
		const pageWidth = 360;
		const pageHeight = 200;
		const pageCount = 5;
		const viewportNode = new WorldNode();
		viewportNode.setPivot(Pivot.topLeft.clone());
		viewportNode.setAnchor(Pivot.topLeft.clone());
		viewportNode.setContentSize(Vector2.create(pageWidth, pageHeight));
		viewportNode.setLocalPosition(Vector2.create(140, 80));
		const snapScrollView = viewportNode.addComponent(UISnapScrollView);
		snapScrollView.setPageChangeThreshold(0.25);

		const content = snapScrollView.getContent();
		for (let pageIndex = 0; pageIndex < pageCount; ++pageIndex) {
			const pageNode = new WorldNode();
			pageNode.setPivot(Pivot.topLeft.clone());
			pageNode.setAnchor(Pivot.topLeft.clone());
			pageNode.setContentSize(Vector2.create(pageWidth, pageHeight));
			pageNode.setLocalPosition(Vector2.create(pageIndex * pageWidth, 0));
			const pagePaint = pageNode.addComponent(Paint);
			pagePaint.setColor(new Color(0.2 + pageIndex * 0.15, 0.3, 0.6, 1));
			const pageLabel = pageNode.addComponent(UILabel);
			pageLabel.setText("Page " + (pageIndex + 1));
			pageLabel.setFontSize(28);
			content.addChild(pageNode);
		}
		snapScrollView.setScrollContentSize(Vector2.create(pageWidth * pageCount, pageHeight));
		snapScrollView.pageTo(2, false);
		const root = this.getRoot();
		root.addChild(viewportNode);
	}
}

const engineConfiguration = new EngineConfiguration();
const engine = new Engine(engineConfiguration);
const scene = new PagerScene();
scene.setLoadingMinDurationMs(0);
engine.run(scene);
생성자constructorconstructor()#0.2.0 부터since 0.2.0소스source

생성.

Constructor.

짝Counterpart: vanilla-engine UISnapScrollView.new

메서드methodsetSnapToNearest(isSnapToNearest)#0.2.0 부터since 0.2.0소스source

스냅 모드 설정.

  • false: 1회 스와이프 시 1칸만 이동. (기본값)
  • true : 드래그 위치에서 가장 가까운 아이템으로 스냅.

Sets the snap mode.

  • false: moves only one item per swipe. (default)
  • true : snaps to the item nearest the drag position.
isSnapToNearestboolean
참이면 가장 가까운 아이템 모드. (끄는 동안 칸 범위를 자르지 않고, 놓으면 스냅 오프셋이 가장 가까운 아이템으로 간다)True selects nearest item mode. (The drag is not clamped to neighbor items, and on release it goes to the item whose snap offset is nearest)

짝Counterpart: vanilla-engine UISnapScrollView.set_snap_to_nearest

예제Example여러 칸을 끌고 놓으면 가장 가까운 칸으로 붙기Drag across several items and snap to the nearest one on release
import { UISnapScrollView, WorldNode, Pivot, Vector2 } from "./build/vanilla.js";

const viewportNode = new WorldNode();
viewportNode.setContentSize(Vector2.create(200, 120));
const snapScrollView = viewportNode.addComponent(UISnapScrollView);
snapScrollView.setSnapToNearest(true);
const content = snapScrollView.getContent();
for (let pageIndex = 0; pageIndex < 5; ++pageIndex) {
	const pageNode = new WorldNode();
	pageNode.setPivot(Pivot.topLeft.clone());
	pageNode.setAnchor(Pivot.topLeft.clone());
	pageNode.setContentSize(Vector2.create(200, 120));
	pageNode.setLocalPosition(Vector2.create(pageIndex * 200, 0));
	content.addChild(pageNode);
}
snapScrollView.setScrollContentSize(Vector2.create(1000, 120));
snapScrollView.touchPress(Vector2.create(190, 60));
snapScrollView.tick(0.016);
snapScrollView.touchMove(Vector2.create(-300, 60));
snapScrollView.tick(0.016);
snapScrollView.touchRelease(Vector2.create(-300, 60));
snapScrollView.tick(0.5);
const scrollOffset = snapScrollView.getScrollOffset();
console.log(snapScrollView.getSnapCurrentIndex(), scrollOffset.x);
메서드methodgetSnapToNearest()#0.2.0 부터since 0.2.0소스source

스냅 모드 반환.

Returns the snap mode.

반환Returnsboolean 가장 가까운 아이템 모드면 참. (기본 거짓)True if in nearest item mode. (default false)

짝Counterpart: vanilla-engine UISnapScrollView.get_snap_to_nearest

메서드methodsetVelocityThreshold(velocityThreshold)#0.2.0 부터since 0.2.0소스source

방향성 스냅 발동 최소 속도 설정. (px/s, 1칸 모드에서만 사용)

Sets the minimum speed that triggers a directional snap. (px/s, used only in one item mode)

velocityThresholdnumber
최소 속도. (스크롤 오프셋이 움직인 초당 픽셀, 1칸 모드에서 놓을 때 드래그 속도가 이 이상이면 끈 거리와 상관없이 그 방향으로 한 칸 넘긴다)Minimum velocity. (Pixels per second the scroll offset moved, in one-item mode a release at or above this velocity moves one item in that direction regardless of the dragged distance)

짝Counterpart: vanilla-engine UISnapScrollView.set_velocity_threshold

메서드methodgetVelocityThreshold()#0.2.0 부터since 0.2.0소스source

방향성 스냅 발동 최소 속도 반환.

Returns the minimum speed that triggers a directional snap.

반환Returnsnumber 최소 속도. (초당 픽셀, 기본 200)Minimum velocity. (pixels per second, default 200)

짝Counterpart: vanilla-engine UISnapScrollView.get_velocity_threshold

메서드methodsetPageChangeThreshold(pageChangeThreshold)#0.2.0 부터since 0.2.0소스source

느린 스와이프 시 페이지 전환 임계값 설정. (아이템 크기 대비 비율, 기본값: 0.35)

Sets the page change threshold for slow swipes. (ratio to the item size, default: 0.35)

pageChangeThresholdnumber
옆 아이템 크기에 대한 비율. (뗄 때 이만큼 넘게 끌었으면 그 아이템으로 넘긴다)Ratio to the neighboring item's size. (on release, moves to that item if dragged further than this)

짝Counterpart: vanilla-engine UISnapScrollView.set_page_change_threshold

메서드methodgetPageChangeThreshold()#0.2.0 부터since 0.2.0소스source

느린 스와이프 시 페이지 전환 임계값 반환.

Returns the page change threshold for slow swipes.

반환Returnsnumber 옆 아이템 크기에 대한 비율. (기본 0.35)Ratio to the neighboring item's size. (default 0.35)

짝Counterpart: vanilla-engine UISnapScrollView.get_page_change_threshold

메서드methodgetDragVelocity()#0.4.0 부터since 0.4.0소스source

드래그 속도 반환.

Returns the drag velocity.

반환Returnsnumber 끄는 동안 스크롤 축 오프셋이 움직인 속도. (초당 픽셀, 다음 칸 쪽이 음수, 마지막으로 끈 tick 의 값이고 끌기를 시작하면 0)Velocity of the scroll axis offset while dragging. (Pixels per second, negative toward the next item, the value of the last dragging tick, 0 when a drag starts)

짝Counterpart: vanilla-engine UISnapScrollView.get_drag_velocity

메서드methodtick(timeDelta)#0.2.0 부터since 0.2.0소스source

갱신.

Update.

timeDeltanumber
지난 프레임부터의 시간. (초, 스냅 트윈도 이만큼 나아간다)Time since the last frame. (seconds, the snap tween also advances by this much)
메서드methodclampScrollOffsetToAdjacentPages()#0.2.0 부터since 0.2.0소스source

드래그 중 스크롤 오프셋을 현재 페이지의 인접 페이지 범위로 제한.

Limits the scroll offset during drag to the range of pages adjacent to the current page.

짝Counterpart: vanilla-engine UISnapScrollView.clamp_scroll_offset_to_adjacent_pages

메서드methodcomputeSnapOffsets()#0.2.0 부터since 0.2.0소스source

자식 노드별 스냅 오프셋 목록 계산.

  • 각 아이템 중앙이 뷰포트 중앙에 오는 스크롤 오프셋 배열을 반환한다.
  • 아이템 크기가 불균일해도 올바르게 계산된다.

Computes the snap offset list per child node.

  • Returns an array of scroll offsets at which each item's center lands at the viewport center.
  • Computed correctly even if item sizes are uneven.

반환Returnsnumber[] 콘텐츠 노드의 자식 차례대로 스크롤 오프셋. (스크롤하는 축의 값, 자식이 없거나 노드에 붙기 전이면 빈 배열)Scroll offsets in the order of the content node's children. (values on the scrolling axis, empty array if there are no children or before attaching to a node)

짝Counterpart: vanilla-engine UISnapScrollView.compute_snap_offsets

메서드methodplaySnapTween()#0.2.0 부터since 0.2.0소스source

현재 오프셋과 속도를 기반으로 스냅 인덱스를 결정하고 트윈을 시작한다.

  • 가장 가까운 아이템 모드: 스냅 오프셋이 지금 오프셋과 가장 가까운 아이템.
  • 1칸 모드: 드래그 속도가 setVelocityThreshold 이상이면 속도 방향으로 한 칸, 아니면 끈 방향으로 끈 거리가 옆 칸 크기의 비율 이상일 때 한 칸.

Decides the snap index from the current offset and velocity and starts the tween.

  • Nearest item mode: the item whose snap offset is nearest the current offset.
  • One-item mode: one item in the velocity direction if the drag velocity is at least setVelocityThreshold, otherwise one item in the drag direction when the dragged distance is at least the ratio of the neighbor item size.

짝Counterpart: vanilla-engine UISnapScrollView.play_snap_tween

메서드methodpageTo(index, smooth)#0.2.0 부터since 0.2.0소스source

지정 인덱스로 이동.

  • smooth: true이면 트윈으로 부드럽게, false이면 즉시 이동.

Moves to the given index.

  • smooth: if true, smoothly with a tween, if false, moves immediately.
indexnumber
아이템 순서. (콘텐츠 노드의 자식 순서, 0 ~ 자식 수 - 1 로 자른다)Item index. (child index of the content node, clamped to 0 ~ child count - 1)
smoothboolean
참이면 0.3 초 트윈으로, 거짓이면 바로 옮긴다.If true, moves with a 0.3 second tween, if false, moves immediately.

짝Counterpart: vanilla-engine UISnapScrollView.page_to

예제Example바로 옮기기와 트윈으로 옮기기Moving immediately and moving with a tween
import { UISnapScrollView, WorldNode, Pivot, Vector2 } from "./build/vanilla.js";

const viewportNode = new WorldNode();
viewportNode.setContentSize(Vector2.create(200, 120));
const snapScrollView = viewportNode.addComponent(UISnapScrollView);
const content = snapScrollView.getContent();
for (let pageIndex = 0; pageIndex < 3; ++pageIndex) {
	const pageNode = new WorldNode();
	pageNode.setPivot(Pivot.topLeft.clone());
	pageNode.setAnchor(Pivot.topLeft.clone());
	pageNode.setContentSize(Vector2.create(200, 120));
	pageNode.setLocalPosition(Vector2.create(pageIndex * 200, 0));
	content.addChild(pageNode);
}
snapScrollView.setScrollContentSize(Vector2.create(600, 120));
snapScrollView.pageTo(2, false);
const jumpedOffset = snapScrollView.getScrollOffset();
console.log(jumpedOffset.x);
snapScrollView.pageTo(1, true);
snapScrollView.tick(0.5);
const tweenedOffset = snapScrollView.getScrollOffset();
console.log(tweenedOffset.x);
메서드methodsetSnapCurrentIndex(index)#0.2.0 부터since 0.2.0소스source

특정 인덱스로 스냅 트윈 이동.

Snap tween move to a specific index.

indexnumber
아이템 순서. (0 ~ 자식 수 - 1 로 자른다, 자식이 없으면 아무것도 하지 않는다)Item index. (clamped to 0 ~ child count - 1, does nothing if there are no children)

짝Counterpart: vanilla-engine UISnapScrollView.set_snap_current_index

메서드methodgetSnapCurrentIndex()#0.2.0 부터since 0.2.0소스source

현재 스냅 인덱스 반환.

Returns the current snap index.

반환Returnsnumber 지금 스냅한(또는 트윈으로 가는) 아이템 순서.Index of the item currently snapped to (or being tweened to).

짝Counterpart: vanilla-engine UISnapScrollView.get_snap_current_index