找物源码导读Cocos Creator 3.8 · assets/scripts
0%
已读 0 / 424
目录— 个文件
目录加载中…
…/module/component/testtempscrollview.ts
1// /*
2// Copyright (c) 2013-2016 Chukong Technologies Inc.
3// Copyright (c) 2017-2023 Xiamen Yaji Software Co., Ltd.
4
5// http://www.cocos.com
6
7// Permission is hereby granted, free of charge, to any person obtaining a copy
8// of this software and associated documentation files (the "Software"), to deal
9// in the Software without restriction, including without limitation the rights to
10// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
11// of the Software, and to permit persons to whom the Software is furnished to do so,
12// subject to the following conditions:
13
14// The above copyright notice and this permission notice shall be included in
15// all copies or substantial portions of the Software.
16
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23// THE SOFTWARE.
24// */
25
26// import { ccclass, displayOrder, executionOrder, help, menu, range, requireComponent, serializable, tooltip, type } from 'cc.decorator';
27// import { EDITOR_NOT_IN_PREVIEW, USE_XR } from 'internal:constants';
28// import { UITransform } from '../2d/framework';
29// import { legacyCC } from '../core/global-exports';
30// import { Size, Vec2, Vec3, approx, v2, v3 } from '../core/math';
31// import { errorID, logID } from '../core/platform/debug';
32// import { director, DirectorEvent } from '../game/director';
33// import { input } from '../input/input';
34// import { Event, EventGamepad, EventHandle, EventMouse, EventTouch, SystemEventType, Touch } from '../input/types';
35// import { EventHandler as ComponentEventHandler } from '../scene-graph/component-event-handler';
36// import { Node } from '../scene-graph/node';
37// import { TransformBit } from '../scene-graph/node-enum';
38// import { NodeEventType } from '../scene-graph/node-event';
39// import { DeviceType, XrUIPressEvent, XrUIPressEventType } from '../xr/event/xr-event-handle';
40// import { Layout } from './layout';
41// import { ScrollBar } from './scroll-bar';
42// import { ViewGroup } from './view-group';
43// import { InputEventType } from '../input/types/event-enum';
44
45// const NUMBER_OF_GATHERED_TOUCHES_FOR_MOVE_SPEED = 5;
46// const OUT_OF_BOUNDARY_BREAKING_FACTOR = 0.05;
47// const EPSILON = 1e-4;
48// const TOLERANCE = 1e4;
49// const MOVEMENT_FACTOR = 0.7;
50// const _tempVec3 = v3();
51// const _tempVec3_1 = v3();
52// const _tempVec2 = v2();
53// const _tempVec2_1 = v2();
54
55// const quintEaseOut = (time: number): number => {
56// time -= 1;
57// return time * time * time * time * time + 1;
58// };
59
60// const getTimeInMilliseconds = (): number => {
61// const currentTime = new Date();
62// return currentTime.getMilliseconds();
63// };
64
65// const eventMap = {
66// 'scroll-to-top': 0,
67// 'scroll-to-bottom': 1,
68// 'scroll-to-left': 2,
69// 'scroll-to-right': 3,
70// scrolling: 4,
71// 'bounce-bottom': 6,
72// 'bounce-left': 7,
73// 'bounce-right': 8,
74// 'bounce-top': 5,
75// 'scroll-ended': 9,
76// 'touch-up': 10,
77// 'scroll-ended-with-threshold': 11,
78// 'scroll-began': 12,
79// };
80
81// const _moveDeltaOptions = {
82// anchor: v2(),
83// applyToHorizontal: false,
84// applyToVertical: false,
85// };
86
87// const assignMoveDeltaOption = (x: number, y: number, applyToHorizontal: boolean, applyToVertical: boolean): void => {
88// _moveDeltaOptions.anchor.set(x, y);
89// _moveDeltaOptions.applyToHorizontal = applyToHorizontal;
90// _moveDeltaOptions.applyToVertical = applyToVertical;
91// };
92
93// /**
94// * @en
95// * Enum for ScrollView event type.
96// *
97// * @zh
98// * 滚动视图事件类型。
99// */
100// export enum ScrollViewEventType {
101// /**
102// * @en
103// * It means an invalid event type or "default empty value" of EventType.
104// *
105// * @zh
106// * 代表无效事件, 或者EventType的默认空值。
107// */
108// NONE = '',
109
110// /**
111// * @en
112// * The event emitted when ScrollView scroll to the top boundary of inner container.
113// *
114// * @zh
115// * 滚动视图滚动到顶部边界事件。
116// */
117// SCROLL_TO_TOP = 'scroll-to-top',
118// /**
119// * @en
120// * The event emitted when ScrollView scroll to the bottom boundary of inner container.
121// *
122// * @zh
123// * 滚动视图滚动到底部边界事件。
124// */
125// SCROLL_TO_BOTTOM = 'scroll-to-bottom',
126// /**
127// * @en
128// * The event emitted when ScrollView scroll to the left boundary of inner container.
129// *
130// * @zh
131// * 滚动视图滚动到左边界事件。
132// */
133// SCROLL_TO_LEFT = 'scroll-to-left',
134// /**
135// * @en
136// * The event emitted when ScrollView scroll to the right boundary of inner container.
137// *
138// * @zh
139// * 滚动视图滚动到右边界事件。
140// */
141// SCROLL_TO_RIGHT = 'scroll-to-right',
142// /**
143// * @en
144// * The event emitted when ScrollView scroll began.
145// *
146// * @zh
147// * 滚动视图滚动开始时发出的事件。
148// */
149// SCROLL_BEGAN = 'scroll-began',
150// /**
151// * @en
152// * The event emitted when ScrollView auto scroll ended.
153// *
154// * @zh
155// * 滚动视图滚动结束的时候发出的事件。
156// */
157// SCROLL_ENDED = 'scroll-ended',
158// /**
159// * @en
160// * The event emitted when ScrollView scroll to the top boundary of inner container and start bounce.
161// *
162// * @zh
163// * 滚动视图滚动到顶部边界并且开始回弹时发出的事件。
164// */
165// BOUNCE_TOP = 'bounce-top',
166// /**
167// * @en
168// * The event emitted when ScrollView scroll to the bottom boundary of inner container and start bounce.
169// *
170// * @zh
171// * 滚动视图滚动到底部边界并且开始回弹时发出的事件。
172// */
173// BOUNCE_BOTTOM = 'bounce-bottom',
174// /**
175// * @en
176// * The event emitted when ScrollView scroll to the left boundary of inner container and start bounce.
177// *
178// * @zh
179// * 滚动视图滚动到左边界并且开始回弹时发出的事件。
180// */
181// BOUNCE_LEFT = 'bounce-left',
182// /**
183// * @en
184// * The event emitted when ScrollView scroll to the right boundary of inner container and start bounce.
185// *
186// * @zh
187// * 滚动视图滚动到右边界并且开始回弹时发出的事件。
188// */
189// BOUNCE_RIGHT = 'bounce-right',
190// /**
191// * @en
192// * The event emitted when ScrollView is scrolling.
193// *
194// * @zh
195// * 滚动视图正在滚动时发出的事件。
196// */
197// SCROLLING = 'scrolling',
198// /**
199// * @en
200// * The event emitted when ScrollView auto scroll ended with a threshold.
201// *
202// * @zh
203// * 滚动视图自动滚动快要结束的时候发出的事件。
204// */
205// SCROLL_ENG_WITH_THRESHOLD = 'scroll-ended-with-threshold',
206// /**
207// * @en
208// * The event emitted when user release the touch.
209// *
210// * @zh
211// * 当用户松手的时候会发出一个事件。
212// */
213// TOUCH_UP = 'touch-up',
214// }
215
216// enum XrhoverType {
217// NONE = 0,
218// LEFT = 1,
219// RIGHT = 2
220// }
221
222// /**
223// * @en
224// * Layout container for a view hierarchy that can be scrolled by the user,
225// * allowing it to be larger than the physical display.
226// *
227// * @zh
228// * 滚动视图组件。
229// */
230
231// @ccclass('cc.ScrollView')
232// @help('i18n:cc.ScrollView')
233// @executionOrder(110)
234// @menu('UI/ScrollView')
235// @requireComponent(UITransform)
236// export class ScrollView extends ViewGroup {
237// public static EventType = ScrollViewEventType;
238
239// /**
240// * @en
241// * The elapse time of bouncing back. A value of 0 will bounce back immediately.
242// *
243// * @zh
244// * 回弹持续的时间,0 表示将立即反弹。
245// */
246// @serializable
247// @range([0, 10])
248// @displayOrder(5)
249// @tooltip('i18n:scrollview.bounceDuration')
250// public bounceDuration = 1;
251
252// /**
253// * @en
254// * It determines how quickly the content stop moving. A value of 1 will stop the movement immediately.
255// * A value of 0 will never stop the movement until it reaches to the boundary of scrollview.
256// *
257// * @zh
258// * 开启惯性后,在用户停止触摸后滚动多快停止,0表示永不停止,1表示立刻停止。
259// */
260// @serializable
261// @range([0, 1, 0.1])
262// @displayOrder(3)
263// @tooltip('i18n:scrollview.brake')
264// public brake = 0.5;
265
266// /**
267// * @en
268// * When elastic is set, the content will be bounce back when move out of boundary.
269// *
270// * @zh
271// * 是否允许滚动内容超过边界,并在停止触摸后回弹。
272// */
273// @serializable
274// @displayOrder(3)
275// @tooltip('i18n:scrollview.elastic')
276// public elastic = true;
277
278// /**
279// * @en
280// * When inertia is set, the content will continue to move when touch ended.
281// *
282// * @zh
283// * 是否开启滚动惯性。
284// */
285// @serializable
286// @displayOrder(2)
287// @tooltip('i18n:scrollview.inertia')
288// public inertia = true;
289
290// /**
291// * @en
292// * This is a reference to the UI element to be scrolled.
293// *
294// * @zh
295// * 可滚动展示内容的节点。
296// */
297// @type(Node)
298// @displayOrder(5)
299// @tooltip('i18n:scrollview.content')
300// get content (): Node | null {
301// return this._content;
302// }
303// set content (value) {
304// if (this._content === value) {
305// return;
306// }
307// const viewTrans = value && value.parent && value.parent._getUITransformComp();
308// if (value && (!value || !viewTrans)) {
309// logID(4302);
310// return;
311// }
312
313// this._content = value;
314// this._calculateBoundary();
315// }
316
317// /**
318// * @en
319// * Enable horizontal scroll.
320// *
321// * @zh
322// * 是否开启水平滚动。
323// */
324// @serializable
325// @displayOrder(0)
326// @tooltip('i18n:scrollview.horizontal')
327// public horizontal = true;
328
329// /**
330// * @en
331// * The horizontal scrollbar reference.
332// * @zh
333// * 水平滚动的 ScrollBar。
334// */
335// @type(ScrollBar)
336// @displayOrder(0)
337// @tooltip('i18n:scrollview.horizontal_bar')
338// get horizontalScrollBar (): ScrollBar | null {
339// const horizontalScrollBar = this._horizontalScrollBar;
340// if (horizontalScrollBar && !horizontalScrollBar.isValid) {
341// errorID(4303, 'horizontal', this.node.name);
342// }
343// return horizontalScrollBar;
344// }
345
346// set horizontalScrollBar (value: ScrollBar | null) {
347// if (this._horizontalScrollBar === value) {
348// return;
349// }
350
351// this._horizontalScrollBar = value;
352
353// if (this._horizontalScrollBar) {
354// this._horizontalScrollBar.setScrollView(this);
355// this._updateScrollBar(Vec2.ZERO);
356// }
357// }
358
359// /**
360// * @en
361// * Enable vertical scroll.
362// *
363// * @zh
364// * 是否开启垂直滚动。
365// */
366// @serializable
367// @displayOrder(1)
368// @tooltip('i18n:scrollview.vertical')
369// public vertical = true;
370
371// /**
372// * @en
373// * The vertical scrollbar reference.
374// *
375// * @zh
376// * 垂直滚动的 ScrollBar。
377// */
378// @type(ScrollBar)
379// @displayOrder(1)
380// @tooltip('i18n:scrollview.vertical_bar')
381// get verticalScrollBar (): ScrollBar | null {
382// const verticalScrollBar = this._verticalScrollBar;
383// if (verticalScrollBar && !verticalScrollBar.isValid) {
384// errorID(4303, 'vertical', this.node.name);
385// }
386// return verticalScrollBar;
387// }
388
389// set verticalScrollBar (value: ScrollBar | null) {
390// if (this._verticalScrollBar === value) {
391// return;
392// }
393
394// this._verticalScrollBar = value;
395
396// if (this._verticalScrollBar) {
397// this._verticalScrollBar.setScrollView(this);
398// this._updateScrollBar(Vec2.ZERO);
399// }
400// }
401
402// /**
403// * @en
404// * If cancelInnerEvents is set to true, the scroll behavior will cancel touch events on inner content nodes
405// * It's set to true by default.
406// *
407// * @zh
408// * 如果这个属性被设置为 true,那么滚动行为会取消子节点上注册的触摸事件,默认被设置为 true。<br/>
409// * 注意,子节点上的 touchstart 事件仍然会触发,触点移动距离非常短的情况下 touchmove 和 touchend 也不会受影响。
410// */
411// @serializable
412// @displayOrder(9)
413// @tooltip('i18n:scrollview.cancelInnerEvents')
414// public cancelInnerEvents = true;
415
416// /**
417// * @en
418// * ScrollView events callback.
419// *
420// * @zh
421// * 滚动视图的事件回调函数。
422// */
423// @type([ComponentEventHandler])
424// @serializable
425// @displayOrder(10)
426// @tooltip('i18n:scrollview.scrollEvents')
427// public scrollEvents: ComponentEventHandler[] = [];
428
429// /**
430// * @en The display view in the scroll view component.
431// * @zh scroll view 组件中的显示区域。
432// */
433// get view (): UITransform | null {
434// const parent = this._content && this._content.parent;
435// if (!parent) {
436// return null;
437// }
438// return parent._getUITransformComp();
439// }
440
441// protected _autoScrolling = false;
442// protected _scrolling = false;
443// @serializable
444// protected _content: Node | null = null;
445// @serializable
446// protected _horizontalScrollBar: ScrollBar | null = null;
447// @serializable
448// protected _verticalScrollBar: ScrollBar | null = null;
449
450// protected _topBoundary = 0;
451// protected _bottomBoundary = 0;
452// protected _leftBoundary = 0;
453// protected _rightBoundary = 0;
454
455// protected _touchMoveDisplacements: Vec3[] = [];
456// protected _touchMoveTimeDeltas: number[] = [];
457// protected _touchMovePreviousTimestamp = 0;
458// protected _touchMoved = false;
459// protected _autoScrollAttenuate = false;
460// protected _autoScrollStartPosition = new Vec3();
461// protected _autoScrollTargetDelta = new Vec3();
462// protected _autoScrollTotalTime = 0;
463// protected _autoScrollAccumulatedTime = 0;
464// protected _autoScrollCurrentlyOutOfBoundary = false;
465// protected _autoScrollBraking = false;
466// protected _autoScrollBrakingStartPosition = new Vec3();
467
468// protected _outOfBoundaryAmount = new Vec3();
469// protected _outOfBoundaryAmountDirty = true;
470// protected _stopMouseWheel = false;
471// protected _mouseWheelEventElapsedTime = 0.0;
472// protected _isScrollEndedWithThresholdEventFired = false;
473// // use bit wise operations to indicate the direction
474// protected _scrollEventEmitMask = 0;
475// protected _isBouncing = false;
476// protected _contentPos = new Vec3();
477// protected _deltaPos = new Vec3();
478// protected _deltaAmount = new Vec3();
479
480// protected _hoverIn: XrhoverType = XrhoverType.NONE;
481
482// constructor () {
483// super();
484// }
485
486// /**
487// * @en
488// * Scroll the content to the bottom boundary of ScrollView.
489// *
490// * @zh
491// * 视图内容将在规定时间内滚动到视图底部。
492// *
493// * @param timeInSecond
494// * @en The rolling time(in seconds). If time is up, the content will slide to the bottom border. @zh 滚动时间(s)。 如果超时,内容将立即跳到底部边界。
495// * @param attenuated @en Whether the rolling acceleration is attenuated(The default is true) @zh 滚动加速是否衰减,默认为 true
496// * @example
497// * ```ts
498// * // Scroll to the bottom of the view.
499// * scrollView.scrollToBottom(0.1);
500// * ```
501// */
502// public scrollToBottom (timeInSecond?: number, attenuated = true): void {
503// this._doScroll(0, 0, false, true, timeInSecond, attenuated);
504// }
505
506// /**
507// * @en
508// * Scroll the content to the top boundary of ScrollView.
509// *
510// * @zh
511// * 视图内容将在规定时间内滚动到视图顶部。
512// *
513// * @param timeInSecond
514// * @en The rolling time(in seconds). If time is up, the content will slide to the bottom border. @zh 滚动时间(s)。 如果超时,内容将立即跳到底部边界。
515// * @param attenuated @en Whether the rolling acceleration is attenuated(The default is true). @zh 滚动加速是否衰减,默认为 true
516// * @example
517// * ```ts
518// * // Scroll to the top of the view.
519// * scrollView.scrollToTop(0.1);
520// * ```
521// */
522// public scrollToTop (timeInSecond?: number, attenuated = true): void {
523// this._doScroll(0, 1, false, true, timeInSecond, attenuated);
524// }
525
526// /**
527// * @en
528// * Scroll the content to the left boundary of ScrollView.
529// *
530// * @zh
531// * 视图内容将在规定时间内滚动到视图左边。
532// *
533// * @param timeInSecond
534// * @en The rolling time(in seconds). If time is up, the content will slide to the bottom border. @zh 滚动时间(s)。 如果超时,内容将立即跳到底部边界。
535// * @param attenuated @en Whether the rolling acceleration is attenuated(The default is true). @zh 滚动加速是否衰减,默认为 true。
536// * @example
537// * ```ts
538// * // Scroll to the left of the view.
539// * scrollView.scrollToLeft(0.1);
540// * ```
541// */
542// public scrollToLeft (timeInSecond?: number, attenuated = true): void {
543// this._doScroll(0, 0, true, false, timeInSecond, attenuated);
544// }
545
546// /**
547// * @en
548// * Scroll the content to the right boundary of ScrollView.
549// *
550// * @zh
551// * 视图内容将在规定时间内滚动到视图右边。
552// *
553// * @param timeInSecond
554// * @en The rolling time(in seconds). If time is up, the content will slide to the bottom border. @zh 滚动时间(s)。 如果超时,内容将立即跳到底部边界。
555// * @param attenuated @en Whether the rolling acceleration is attenuated(The default is true). @zh 滚动加速是否衰减,默认为 true。
556// * @example
557// * ```ts
558// * // Scroll to the right of the view.
559// * scrollView.scrollToRight(0.1);
560// * ```
561// */
562// public scrollToRight (timeInSecond?: number, attenuated = true): void {
563// this._doScroll(1, 0, true, false, timeInSecond, attenuated);
564// }
565
566// /**
567// * @en
568// * Scroll the content to the top left boundary of ScrollView.
569// *
570// * @zh
571// * 视图内容将在规定时间内滚动到视图左上角。
572// *
573// * @param timeInSecond
574// * @en The rolling time(in seconds). If time is up, the content will slide to the bottom border. @zh 滚动时间(s)。 如果超时,内容将立即跳到底部边界。
575// * @param attenuated @en Whether the rolling acceleration is attenuated(The default is true). @zh 滚动加速是否衰减,默认为 true。
576// * @example
577// * ```ts
578// * // Scroll to the upper left corner of the view.
579// * scrollView.scrollToTopLeft(0.1);
580// * ```
581// */
582// public scrollToTopLeft (timeInSecond?: number, attenuated = true): void {
583// this._doScroll(0, 1, true, true, timeInSecond, attenuated);
584// }
585
586// /**
587// * @en
588// * Scroll the content to the top right boundary of ScrollView.
589// *
590// * @zh
591// * 视图内容将在规定时间内滚动到视图右上角。
592// *
593// * @param timeInSecond
594// * @en The rolling time(in seconds). If time is up, the content will slide to the bottom border. @zh 滚动时间(s)。 如果超时,内容将立即跳到底部边界。
595// * @param attenuated @en Whether the rolling acceleration is attenuated(The default is true). @zh 滚动加速是否衰减,默认为 true。
596// * @example
597// * ```ts
598// * // Scroll to the top right corner of the view.
599// * scrollView.scrollToTopRight(0.1);
600// * ```
601// */
602// public scrollToTopRight (timeInSecond?: number, attenuated = true): void {
603// this._doScroll(1, 1, true, true, timeInSecond, attenuated);
604// }
605
606// /**
607// * @en
608// * Scroll the content to the bottom left boundary of ScrollView.
609// *
610// * @zh
611// * 视图内容将在规定时间内滚动到视图左下角。
612// *
613// * @param timeInSecond
614// * @en The rolling time(in seconds). If time is up, the content will slide to the bottom border. @zh 滚动时间(s)。 如果超时,内容将立即跳到底部边界。
615// * @param attenuated @en Whether the rolling acceleration is attenuated(The default is true). @zh 滚动加速是否衰减,默认为 true。
616// * @example
617// * ```ts
618// * // Scroll to the lower left corner of the view.
619// * scrollView.scrollToBottomLeft(0.1);
620// * ```
621// */
622// public scrollToBottomLeft (timeInSecond?: number, attenuated = true): void {
623// this._doScroll(0, 0, true, true, timeInSecond, attenuated);
624// }
625
626// /**
627// * @en
628// * Scroll the content to the bottom right boundary of ScrollView.
629// *
630// * @zh
631// * 视图内容将在规定时间内滚动到视图右下角。
632// *
633// * @param timeInSecond
634// * @en The rolling time(in seconds). If time is up, the content will slide to the bottom border. @zh 滚动时间(s)。 如果超时,内容将立即跳到底部边界。
635// * @param attenuated @en Whether the rolling acceleration is attenuated(The default is true). @zh 滚动加速是否衰减,默认为 true。
636// * @example
637// * ```ts
638// * // Scroll to the lower right corner of the view.
639// * scrollView.scrollToBottomRight(0.1);
640// * ```
641// */
642// public scrollToBottomRight (timeInSecond?: number, attenuated = true): void {
643// this._doScroll(1, 0, true, true, timeInSecond, attenuated);
644// }
645
646// /**
647// * @en
648// * Scroll with an offset related to the ScrollView's top left origin, if timeInSecond is omitted,
649// * then it will jump to the specific offset immediately.
650// *
651// * @zh
652// * 视图内容在规定时间内将滚动到 ScrollView 相对左上角原点的偏移位置, 如果 timeInSecond 参数不传,则立即滚动到指定偏移位置。
653// *
654// * @param offset
655// * @en After scrolling the view, the position of the view content relative to the view window. @zh 滚动视图后,视图内容(content)相对于视图窗口(viewport)的位置。
656// * @param timeInSecond
657// * @en Scroll time (s). If it times out, the content immediately jumps to the specified offset. @zh 滚动时间(s)。 如果超时,内容将立即跳到指定偏移量处。
658// * @param attenuated @en Whether the rolling acceleration is attenuated(The default is true). @zh 滚动加速是否衰减,默认为 true。
659// * @example
660// * ```ts
661// * // Scroll to middle position in 0.1 second in x-axis
662// * let maxScrollOffset = this.getMaxScrollOffset();
663// * scrollView.scrollToOffset(new Vec2(maxScrollOffset.x / 2, 0), 0.1);
664// * ```
665// */
666// public scrollToOffset (offset: Vec2, timeInSecond?: number, attenuated = true): void {
667// const maxScrollOffset = this.getMaxScrollOffset();
668
669// const anchor = v2();
670// // if maxScrollOffset is 0, then always align the content's top left origin to the top left corner of its parent
671// if (maxScrollOffset.x === 0) {
672// anchor.x = 0;
673// } else {
674// anchor.x = offset.x / maxScrollOffset.x;
675// }
676
677// if (maxScrollOffset.y === 0) {
678// anchor.y = 1;
679// } else {
680// anchor.y = (maxScrollOffset.y - offset.y) / maxScrollOffset.y;
681// }
682
683// this.scrollTo(anchor, timeInSecond, attenuated);
684// }
685
686// /**
687// * @en
688// * Get the position of the scrolling view relative to the origin in the upper-left corner of the viewport.
689// *
690// * @zh
691// * 获取滚动视图相对于视图窗口左上角原点的位置。
692// *
693// * @return @en Current rolling offset. @zh 当前滚动偏移量。
694// */
695// public getScrollOffset (): Vec2 {
696// const topDelta = this._getContentTopBoundary() - this._topBoundary;
697// const leftDelta = this._getContentLeftBoundary() - this._leftBoundary;
698
699// return new Vec2(leftDelta, topDelta);
700// }
701
702// /**
703// * @en
704// * Get the maximize available scroll offset.
705// *
706// * @zh
707// * 获取滚动视图最大可以滚动的偏移量。
708// *
709// * @return @en Maximum scrollable offset. @zh 最大可滚动偏移量。
710// */
711// public getMaxScrollOffset (): Vec2 {
712// if (!this._content || !this.view) {
713// return Vec2.ZERO;
714// }
715// const contentSize = this._content._getUITransformComp()!.contentSize;
716// let horizontalMaximizeOffset = contentSize.width - this.view.width;
717// let verticalMaximizeOffset = contentSize.height - this.view.height;
718// horizontalMaximizeOffset = horizontalMaximizeOffset >= 0 ? horizontalMaximizeOffset : 0;
719// verticalMaximizeOffset = verticalMaximizeOffset >= 0 ? verticalMaximizeOffset : 0;
720
721// return new Vec2(horizontalMaximizeOffset, verticalMaximizeOffset);
722// }
723
724// /**
725// * @en
726// * Scroll the content to the horizontal percent position of ScrollView.
727// *
728// * @zh
729// * 视图内容在规定时间内将滚动到 ScrollView 水平方向的百分比位置上。
730// *
731// * @param percent
732// * @en Scroll to the destination which is located at the percent interpolation from left border to the right border @zh 滚动到从左到右指定百分比插值的位置
733// * @param timeInSecond
734// * @en Scroll time (s). If it times out, the content immediately jumps to the specified offset. @zh 滚动时间(s)。 如果超时,内容将立即跳到指定偏移量处。
735// * @param attenuated @en Whether the rolling acceleration is attenuated(The default is true). @zh 滚动加速是否衰减,默认为 true。
736// * @example
737// * ```ts
738// * // Scroll to middle position.
739// * scrollView.scrollToBottomRight(0.5, 0.1);
740// * ```
741// */
742// public scrollToPercentHorizontal (percent: number, timeInSecond: number, attenuated: boolean): void {
743// this._doScroll(percent, 0, true, false, timeInSecond, attenuated);
744// }
745
746// /**
747// * @en
748// * Scroll the content to the percent position of ScrollView in any direction.
749// *
750// * @zh
751// * 视图内容在规定时间内进行垂直方向和水平方向的滚动,并且滚动到指定百分比位置上。
752// *
753// * @param anchor
754// * @en Scroll to the destination which is located at the anchor interpolation from left/top border to the right/bottom border.
755// * @zh 滚动到从左/上到右/下指定锚点对应分量插值的位置。
756// * @param timeInSecond
757// * @en Scroll time (s). If it times out, the content immediately jumps to the specified offset. @zh 滚动时间(s)。 如果超时,内容将立即跳到指定偏移量处。
758// * @param attenuated @en Whether the rolling acceleration is attenuated(The default is true). @zh 滚动加速是否衰减,默认为 true。
759// * @example
760// * ```ts
761// * // Vertical scroll to the bottom of the view.
762// * scrollView.scrollTo(new Vec2(0, 1), 0.1);
763// *
764// * // Horizontal scroll to view right.
765// * scrollView.scrollTo(new Vec2(1, 0), 0.1);
766// * ```
767// */
768// public scrollTo (anchor: Vec2, timeInSecond?: number, attenuated?: boolean): void {
769// this._doScroll(anchor.x, anchor.y, true, true, timeInSecond, attenuated);
770// }
771
772// /**
773// * @en
774// * Scroll the content to the vertical percent position of ScrollView.
775// *
776// * @zh
777// * 视图内容在规定时间内滚动到 ScrollView 垂直方向的百分比位置上。
778// *
779// * @param percent
780// * @en Scroll to the destination which is located at the percent interpolation from top border to the bottom border. @zh 滚动到从上到下指定百分比插值的位置。
781// * @param timeInSecond
782// * @en Scroll time (s). If it times out, the content immediately jumps to the specified offset. @zh 滚动时间(s)。 如果超时,内容将立即跳到指定偏移量处。
783// * @param attenuated @en Whether the rolling acceleration is attenuated(The default is true). @zh 滚动加速是否衰减,默认为 true。
784// * @example
785// * ```ts
786// * scrollView.scrollToPercentVertical(0.5, 0.1);
787// * ```
788// */
789// public scrollToPercentVertical (percent: number, timeInSecond?: number, attenuated?: boolean): void {
790// this._doScroll(0, percent, false, true, timeInSecond, attenuated);
791// }
792
793// private _doScroll (x: number, y: number, applyToHorizontal: boolean, applyToVertical: boolean, timeInSecond?: number, attenuated = true): void {
794// assignMoveDeltaOption(x, y, applyToHorizontal, applyToVertical);
795// const moveDelta = this._calculateMovePercentDelta(_moveDeltaOptions);
796
797// if (timeInSecond) {
798// this._startAutoScroll(moveDelta, timeInSecond, attenuated);
799// } else {
800// this._moveContent(moveDelta);
801// }
802// }
803
804// /**
805// * @en
806// * Stop auto scroll immediately.
807// *
808// * @zh
809// * 停止自动滚动, 调用此 API 可以让 ScrollView 立即停止滚动。
810// */
811// public stopAutoScroll (): void {
812// this._autoScrolling = false;
813// this._autoScrollAccumulatedTime = this._autoScrollTotalTime;
814// }
815
816// /**
817// * @en
818// * Modify the content position.
819// *
820// * @zh
821// * 设置当前视图内容的坐标点。
822// *
823// * @param position @en Current content position. @zh 希望设置内容框体的位置。
824// * @deprecated Since 3.1.0, setContentPosition is deprecated, please use scrollToOffset instead.
825// */
826// public setContentPosition (position: Vec3): void {
827// this._setContentPosition(position);
828// }
829
830// // Should not use _tempVec3 and also can not invoke any functions that use _tempVec3 as invoker may pass _tempVec3.
831// private _setContentPosition (position: Readonly<Vec3>): void {
832// if (!this._content) {
833// return;
834// }
835// const contentPos = this._getContentPosition();
836// if (Math.abs(position.x - contentPos.x) < EPSILON && Math.abs(position.y - contentPos.y) < EPSILON) {
837// return;
838// }
839
840// this._content.setPosition(position);
841// this._outOfBoundaryAmountDirty = true;
842// }
843
844// /**
845// * @en
846// * Query the content's position in its parent space.
847// *
848// * @zh
849// * 获取当前视图内容的坐标点。
850// *
851// * @returns @en current content position. @zh 当前视图内容的坐标点。
852// * @deprecated Since 3.1.0, getContentPosition is deprecated.
853// */
854// public getContentPosition (): Vec3 {
855// return this._getContentPosition();
856// }
857
858// private _getContentPosition (): Vec3 {
859// if (!this._content) {
860// return Vec3.ZERO.clone();
861// }
862
863// this._contentPos.set(this._content.position);
864// return this._contentPos;
865// }
866
867// /**
868// * @en
869// * Query whether the user is currently dragging the ScrollView to scroll it.
870// *
871// * @zh
872// * 用户是否在拖拽当前滚动视图。
873// *
874// * @returns @en If or not the current scrolling view is being dragged. @zh 是否在拖拽当前滚动视图。
875// */
876// public isScrolling (): boolean {
877// return this._scrolling;
878// }
879
880// /**
881// * @en
882// * Query whether the ScrollView is currently scrolling because of a bounceback or inertia slowdown.
883// *
884// * @zh
885// * 当前滚动视图是否在惯性滚动。
886// *
887// * @returns @en Whether the scrolling view is scrolling inertially. @zh 滚动视图是否在惯性滚动。
888// */
889// public isAutoScrolling (): boolean {
890// return this._autoScrolling;
891// }
892
893// /**
894// * @en Get the minimum precision time of the end-of-scroll event.
895// * @zh 获得滚动结束的事件的最小精度时间。
896// * @returns @en Minimum time. @zh 最小时间。
897// */
898// public getScrollEndedEventTiming (): number {
899// return EPSILON;
900// }
讲解2100 行
滚动备份人工通读

testtempscrollview.ts

整份文件保存了 Cocos ScrollView 的注释版源码,包含触摸捕获、惯性滚动、回弹和滚动条同步,但所有语句均被行注释屏蔽。它不参与当前组件注册,也没有运行态存储或可调用方法,阅读时应把它视作输入模型参考资料。

先确认文件头到末尾都被注释,再对照模拟取消事件与惯性回弹段阅读。

特殊处理与易错点

  • 特殊处理全部 import、类和 legacyCC 注册都在注释中,不产生运行时组件。
  • 设计注释版用模拟 TOUCH_CANCEL 取消子按钮,可作为分页组件输入模型的参考。
这个文件没有可解析的注释,直接读代码即可。