Skip to content

Commit

Permalink
avoid creating object as possible (#17782)
Browse files Browse the repository at this point in the history
  • Loading branch information
minggo authored Oct 30, 2024
1 parent edeb5c7 commit fa8b683
Showing 1 changed file with 50 additions and 55 deletions.
105 changes: 50 additions & 55 deletions cocos/ui/scroll-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ const eventMap = {
'scroll-began': 12,
};

const _moveDeltaOptions = {
anchor: v2(),
applyToHorizontal: false,
applyToVertical: false,
};

/**
* @en
* Enum for ScrollView event type.
Expand Down Expand Up @@ -486,11 +492,10 @@ export class ScrollView extends ViewGroup {
* ```
*/
public scrollToBottom (timeInSecond?: number, attenuated = true): void {
const moveDelta = this._calculateMovePercentDelta({
anchor: new Vec2(0, 0),
applyToHorizontal: false,
applyToVertical: true,
});
_moveDeltaOptions.anchor.set(0, 0);
_moveDeltaOptions.applyToHorizontal = false;
_moveDeltaOptions.applyToVertical = true;
const moveDelta = this._calculateMovePercentDelta(_moveDeltaOptions);

if (timeInSecond) {
this._startAutoScroll(moveDelta, timeInSecond, attenuated !== false);
Expand All @@ -516,11 +521,10 @@ export class ScrollView extends ViewGroup {
* ```
*/
public scrollToTop (timeInSecond?: number, attenuated = true): void {
const moveDelta = this._calculateMovePercentDelta({
anchor: new Vec2(0, 1),
applyToHorizontal: false,
applyToVertical: true,
});
_moveDeltaOptions.anchor.set(0, 1);
_moveDeltaOptions.applyToHorizontal = false;
_moveDeltaOptions.applyToVertical = true;
const moveDelta = this._calculateMovePercentDelta(_moveDeltaOptions);

if (timeInSecond) {
this._startAutoScroll(moveDelta, timeInSecond, attenuated !== false);
Expand All @@ -546,11 +550,10 @@ export class ScrollView extends ViewGroup {
* ```
*/
public scrollToLeft (timeInSecond?: number, attenuated = true): void {
const moveDelta = this._calculateMovePercentDelta({
anchor: new Vec2(0, 0),
applyToHorizontal: true,
applyToVertical: false,
});
_moveDeltaOptions.anchor.set(0, 0);
_moveDeltaOptions.applyToHorizontal = true;
_moveDeltaOptions.applyToVertical = false;
const moveDelta = this._calculateMovePercentDelta(_moveDeltaOptions);

if (timeInSecond) {
this._startAutoScroll(moveDelta, timeInSecond, attenuated !== false);
Expand All @@ -576,11 +579,10 @@ export class ScrollView extends ViewGroup {
* ```
*/
public scrollToRight (timeInSecond?: number, attenuated = true): void {
const moveDelta = this._calculateMovePercentDelta({
anchor: new Vec2(1, 0),
applyToHorizontal: true,
applyToVertical: false,
});
_moveDeltaOptions.anchor.set(1, 0);
_moveDeltaOptions.applyToHorizontal = true;
_moveDeltaOptions.applyToVertical = false;
const moveDelta = this._calculateMovePercentDelta(_moveDeltaOptions);

if (timeInSecond) {
this._startAutoScroll(moveDelta, timeInSecond, attenuated !== false);
Expand All @@ -606,11 +608,10 @@ export class ScrollView extends ViewGroup {
* ```
*/
public scrollToTopLeft (timeInSecond?: number, attenuated = true): void {
const moveDelta = this._calculateMovePercentDelta({
anchor: new Vec2(0, 1),
applyToHorizontal: true,
applyToVertical: true,
});
_moveDeltaOptions.anchor.set(0, 1);
_moveDeltaOptions.applyToHorizontal = true;
_moveDeltaOptions.applyToVertical = true;
const moveDelta = this._calculateMovePercentDelta(_moveDeltaOptions);

if (timeInSecond) {
this._startAutoScroll(moveDelta, timeInSecond, attenuated !== false);
Expand All @@ -636,11 +637,10 @@ export class ScrollView extends ViewGroup {
* ```
*/
public scrollToTopRight (timeInSecond?: number, attenuated = true): void {
const moveDelta = this._calculateMovePercentDelta({
anchor: new Vec2(1, 1),
applyToHorizontal: true,
applyToVertical: true,
});
_moveDeltaOptions.anchor.set(1, 1);
_moveDeltaOptions.applyToHorizontal = true;
_moveDeltaOptions.applyToVertical = true;
const moveDelta = this._calculateMovePercentDelta(_moveDeltaOptions);

if (timeInSecond) {
this._startAutoScroll(moveDelta, timeInSecond, attenuated !== false);
Expand All @@ -666,11 +666,10 @@ export class ScrollView extends ViewGroup {
* ```
*/
public scrollToBottomLeft (timeInSecond?: number, attenuated = true): void {
const moveDelta = this._calculateMovePercentDelta({
anchor: new Vec2(0, 0),
applyToHorizontal: true,
applyToVertical: true,
});
_moveDeltaOptions.anchor.set(0, 0);
_moveDeltaOptions.applyToHorizontal = true;
_moveDeltaOptions.applyToVertical = true;
const moveDelta = this._calculateMovePercentDelta(_moveDeltaOptions);

if (timeInSecond) {
this._startAutoScroll(moveDelta, timeInSecond, attenuated !== false);
Expand All @@ -696,11 +695,10 @@ export class ScrollView extends ViewGroup {
* ```
*/
public scrollToBottomRight (timeInSecond?: number, attenuated = true): void {
const moveDelta = this._calculateMovePercentDelta({
anchor: new Vec2(1, 0),
applyToHorizontal: true,
applyToVertical: true,
});
_moveDeltaOptions.anchor.set(1, 0);
_moveDeltaOptions.applyToHorizontal = true;
_moveDeltaOptions.applyToVertical = true;
const moveDelta = this._calculateMovePercentDelta(_moveDeltaOptions);

if (timeInSecond) {
this._startAutoScroll(moveDelta, timeInSecond, attenuated !== false);
Expand Down Expand Up @@ -806,11 +804,10 @@ export class ScrollView extends ViewGroup {
* ```
*/
public scrollToPercentHorizontal (percent: number, timeInSecond: number, attenuated: boolean): void {
const moveDelta = this._calculateMovePercentDelta({
anchor: new Vec2(percent, 0),
applyToHorizontal: true,
applyToVertical: false,
});
_moveDeltaOptions.anchor.set(percent, 0);
_moveDeltaOptions.applyToHorizontal = true;
_moveDeltaOptions.applyToVertical = false;
const moveDelta = this._calculateMovePercentDelta(_moveDeltaOptions);

if (timeInSecond) {
this._startAutoScroll(moveDelta, timeInSecond, attenuated !== false);
Expand Down Expand Up @@ -842,11 +839,10 @@ export class ScrollView extends ViewGroup {
* ```
*/
public scrollTo (anchor: Vec2, timeInSecond?: number, attenuated?: boolean): void {
const moveDelta = this._calculateMovePercentDelta({
anchor: new Vec2(anchor),
applyToHorizontal: true,
applyToVertical: true,
});
_moveDeltaOptions.anchor.set(anchor);
_moveDeltaOptions.applyToHorizontal = true;
_moveDeltaOptions.applyToVertical = true;
const moveDelta = this._calculateMovePercentDelta(_moveDeltaOptions);

if (timeInSecond) {
this._startAutoScroll(moveDelta, timeInSecond, attenuated);
Expand All @@ -873,11 +869,10 @@ export class ScrollView extends ViewGroup {
* ```
*/
public scrollToPercentVertical (percent: number, timeInSecond?: number, attenuated?: boolean): void {
const moveDelta = this._calculateMovePercentDelta({
anchor: new Vec2(0, percent),
applyToHorizontal: false,
applyToVertical: true,
});
_moveDeltaOptions.anchor.set(0, percent);
_moveDeltaOptions.applyToHorizontal = false;
_moveDeltaOptions.applyToVertical = true;
const moveDelta = this._calculateMovePercentDelta(_moveDeltaOptions);

if (timeInSecond) {
this._startAutoScroll(moveDelta, timeInSecond, attenuated);
Expand Down

0 comments on commit fa8b683

Please sign in to comment.