.rotate({ angle: this.rotateAngle, centerX: 30, centerY: 100 }); // 图片旋转属性 // 显式动画执行,逆时针旋转30度
this.getUIContext().animateTo({
duration: 800,
curve: Curve.Friction,
delay: 300,
iterations: 1, // 设置-1表示动画无限循环
playMode: PlayMode.Alternate,
expectedFrameRateRange: {
min: 20,
max: 120,
expected: 90,
}
}, () => {
if (this.flag) {
this.rotateAngle = -30;
} else {
this.rotateAngle = 0;
}
}); 完整示例参考如下:
@Entry
@Component
struct AttrAnimationExample {
@State rotateAngle: number = 0; // 旋转弧度
@State flag: boolean = false; // 是否播放
build() {
Column() {
Stack() {
Row() {
Text('唱片机内容').fontSize(16);
}
.width('100%')
.height(200)
.backgroundColor('#87CEEB')
.justifyContent(FlexAlign.Center);
Row() {
Image($r('app.media.startIcon')) // 此处仅为样例,请开发者更换为可用图片
.width(60)
.height(100)
.rotate({ angle: this.rotateAngle, centerX: 30, centerY: 100 }); // 图片旋转属性
}
.alignItems(VerticalAlign.Bottom)
.width('100%')
.justifyContent(FlexAlign.End)
.height(200);
};
// 播放/暂停按钮
Button(this.flag ? '暂停' : '播放').onClick(() => {
this.flag = !this.flag;
// 显式动画执行,逆时针旋转30度
this.getUIContext().animateTo({
duration: 800,
curve: Curve.Friction,
delay: 300,
iterations: 1, // 设置-1表示动画无限循环
playMode: PlayMode.Alternate,
expectedFrameRateRange: {
min: 20,
max: 120,
expected: 90,
}
}, () => {
if (this.flag) {
this.rotateAngle = -30;
} else {
this.rotateAngle = 0;
}
});
})
.margin({ top: 40 });
}
.padding({ left: 12, right: 12 })
.height('100%')
.width('100%');
}
}