.onClick((e) => {
this.indexArray[this.index].top = e.y;
this.indexArray[this.index].left = e.x;
this.display(this.indexArray[this.index]);
this.index = this.index + 1 >= 20 ? 0 : this.index + 1;
}); ForEach(this.indexArray, (test: Test) => {
Dot({ test: test });
}, (test: Test, index: number) => {
return test.top + ':' + test.left + ':' + index;
}); hide(test: Test) {
this.getUIContext().animateTo({ duration: 1000 }, () => {
test.is = false;
});
} 完整示例参考如下:
@Entry
@Component
struct ClickDotPage {
@State indexArray: Test[] = [];
@State index: number = 0;
display(test: Test) {
this.getUIContext().animateTo({ duration: 1000 }, () => {
test.is = true;
});
setTimeout(() => {
this.hide(test);
}, 1000);
}
hide(test: Test) {
this.getUIContext().animateTo({ duration: 1000 }, () => {
test.is = false;
});
}
aboutToAppear(): void {
// 初始化数据
for (let i = 0; i < 20; i++) {
this.indexArray.push(new Test(0, 0));
}
}
build() {
Stack() {
// 在Stack组件中循环渲染数组或Map中记录的小圆点,每个小圆点根据其位置信息进行定位
ForEach(this.indexArray, (test: Test) => {
Dot({ test: test });
}, (test: Test, index: number) => {
return test.top + ':' + test.left + ':' + index;
});
}.width('100%')
.height('100%')
.alignContent(Alignment.TopStart)
// 点击事件获取坐标
.onClick((e) => {
this.indexArray[this.index].top = e.y;
this.indexArray[this.index].left = e.x;
this.display(this.indexArray[this.index]);
this.index = this.index + 1 >= 20 ? 0 : this.index + 1;
});
}
}
@Component
struct Dot {
@ObjectLink test: Test;
private w: number = 30;
build() {
if (this.test.is) {
Row()
.width(this.w)
.height(this.w)
.borderRadius(this.w)
.backgroundColor(Color.Blue)
.margin({ left: this.test.left, top: this.test.top })
.transition({
opacity: 0
});
}
}
}
@Observed
class Test {
top: number;
left: number;
is: boolean = false;
constructor(top: number, left: number) {
this.top = top;
this.left = left;
}
}