@Prop @Watch('changeNumber') scrollNumber: number = 0;
@State bottomNum: number = 0;
@State showNumber: string = '0';
@State cacheNumber: string = '';
changeNumberFinal: string = '';
isScrolling: boolean = false;
isInit: boolean = false;
changeNumber() {
if (this.isInit) {
this.animateToScroll(this.scrollNumber);
}
} initAnimateToScroll() {
setTimeout(() => {
this.getUIContext().animateTo({
duration: 600,
onFinish: () => {
this.isInit = true;
}
}, () => {
this.bottomNum = -50;
});
}, 50);
}
animateToScroll(newNum: number) {
this.changeNumberFinal = String(newNum);
if (this.isScrolling) {
return;
}
this.cacheNumber = this.changeNumberFinal;
this.getUIContext().animateTo({
duration: 600,
onFinish: () => {
this.bottomNum = -50;
this.showNumber = this.cacheNumber;
this.cacheNumber = '';
this.isScrolling = false;
if (this.changeNumberFinal !== this.showNumber) {
this.animateToScroll(Number(this.changeNumberFinal));
}
}
}, () => {
this.bottomNum = 0;
this.isScrolling = true;
});
} Row() {
Text(this.showNumber)
.fontSize(35)
.fontColor('#0A59F7');
}
.margin({ bottom: this.bottomNum })
.height(50)
.translate({ y: this.bottomNum }); 完整示例参考如下:
@Entry
@Component
struct ScrollNum {
@State num: number = 0;
@State isEnabled: boolean = true;
build() {
Column({ space: 50 }) {
ScrollNumComponent({ scrollNumber: this.num });
Button('点击添加')
.enabled(this.isEnabled)
.onClick(() => {
this.isEnabled = !this.isEnabled;
setTimeout(() => {
this.num += 50;
this.isEnabled = !this.isEnabled;
}, 600);
});
}
.alignItems(HorizontalAlign.Center)
.justifyContent(FlexAlign.Center)
.width('100%')
.height('100%');
}
}
@Component
export struct ScrollNumComponent {
@Prop @Watch('changeNumber') scrollNumber: number = 0;
@State bottomNum: number = 0;
@State showNumber: string = '0';
@State cacheNumber: string = '';
changeNumberFinal: string = '';
isScrolling: boolean = false;
isInit: boolean = false;
changeNumber() {
if (this.isInit) {
this.animateToScroll(this.scrollNumber);
}
}
aboutToAppear(): void {
this.initAnimateToScroll();
}
initAnimateToScroll() {
setTimeout(() => {
this.getUIContext().animateTo({
duration: 600,
onFinish: () => {
this.isInit = true;
}
}, () => {
this.bottomNum = -50;
});
}, 50);
}
animateToScroll(newNum: number) {
this.changeNumberFinal = String(newNum);
if (this.isScrolling) {
return;
}
this.cacheNumber = this.changeNumberFinal;
this.getUIContext().animateTo({
duration: 600,
onFinish: () => {
this.bottomNum = -50;
this.showNumber = this.cacheNumber;
this.cacheNumber = '';
this.isScrolling = false;
if (this.changeNumberFinal !== this.showNumber) {
this.animateToScroll(Number(this.changeNumberFinal));
}
}
}, () => {
this.bottomNum = 0;
this.isScrolling = true;
});
}
build() {
Column() {
Row() {
Text(this.cacheNumber)
.fontSize(35)
.fontColor('#0A59F7');
}
.height(50)
.translate({ y: this.bottomNum });
Row() {
Text(this.showNumber)
.fontSize(35)
.fontColor('#0A59F7');
}
.margin({ bottom: this.bottomNum })
.height(50)
.translate({ y: this.bottomNum });
}
.clip(true)
.height(50);
}
}