按压组件后让组件随着手指拖动自旋转,实现步骤如下:
// 计算两个点关于圆心点的角度差
function calculateAngleBetweenPoints(p0: Position = { x: 0, y: 0 }, p1: Position = { x: 0, y: 0 },
p2: Position = { x: 0, y: 0 }): number {
const startY = Number(p1.y) - Number(p0.y);
const startX = Number(p1.x) - Number(p0.x);
// 通过反正切函数得到的弧度值
const startRadians: number = Math.atan2(startY, startX);
// 相对于正x轴,角度范围是0到360度
const startDeg = atanToDegrees(startRadians);
const endY = Number(p2.y) - Number(p0.y);
const endX = Number(p2.x) - Number(p0.x);
const endRadians: number = Math.atan2(endY, endX);
const endDeg = atanToDegrees(endRadians);
return endDeg - startDeg;
} .onTouch((event: TouchEvent) => {
if (event.type === TouchType.Down) {
this.upRotates = 0;
this.startRotates = this.rotateAngle;
this.starPosition = { x: event.touches[0].windowX, y: event.touches[0].windowY };
} else if (event.type === TouchType.Move) {
this.updatePosition = { x: event.touches[0].windowX, y: event.touches[0].windowY };
// 手指移动时的角度
this.upRotates = calculateAngleBetweenPoints(this.circleCenterPoint, this.starPosition, this.updatePosition);
}
this.rotateAngle = this.upRotates + this.startRotates;
}); 完整代码如下:
import display from '@ohos.display';
// 反正切结果转化成角度
function atanToDegrees(atanResult: number) {
const degrees = atanResult * (180 / Math.PI);
return degrees < 0 ? degrees + 360 : degrees;
}
// 计算两个点关于圆心点的角度差
function calculateAngleBetweenPoints(p0: Position = { x: 0, y: 0 }, p1: Position = { x: 0, y: 0 },
p2: Position = { x: 0, y: 0 }): number {
const startY = Number(p1.y) - Number(p0.y);
const startX = Number(p1.x) - Number(p0.x);
// 通过反正切函数得到的弧度值
const startRadians: number = Math.atan2(startY, startX);
// 相对于正x轴,角度范围是0到360度
const startDeg = atanToDegrees(startRadians);
const endY = Number(p2.y) - Number(p0.y);
const endX = Number(p2.x) - Number(p0.x);
const endRadians: number = Math.atan2(endY, endX);
const endDeg = atanToDegrees(endRadians);
return endDeg - startDeg;
}
@Entry
@Component
struct RotatableDisc {
@State startRotates: number = 0;
@State upRotates: number = 0;
@State rotateAngle: number = 0;
@State starPosition: Position = {};
@State updatePosition: Position = {};
// 图片中心点坐标
@State circleCenterPoint: Position = {};
private screenH: number = 0;
private screenW: number = 0;
aboutToAppear(): void {
let displayClass: display.Display | null = null;
displayClass = display.getDefaultDisplaySync();
this.screenH = this.getUIContext().px2vp(displayClass.height);
this.screenW = this.getUIContext().px2vp(displayClass.width);
// 计算圆心点坐标,可根据实际需求修改
this.circleCenterPoint = { x: this.screenW / 2, y: this.screenH / 2 };
}
build() {
Column() {
Image($r('app.media.startIcon'))
.width(300)
.height(300)
.objectFit(ImageFit.Contain)
.borderRadius(150)
.rotate({
centerX: '50%',
centerY: '50%',
angle: this.rotateAngle
})
.onTouch((event: TouchEvent) => {
if (event.type === TouchType.Down) {
this.upRotates = 0;
this.startRotates = this.rotateAngle;
this.starPosition = { x: event.touches[0].windowX, y: event.touches[0].windowY };
} else if (event.type === TouchType.Move) {
this.updatePosition = { x: event.touches[0].windowX, y: event.touches[0].windowY };
// 手指移动时的角度
this.upRotates = calculateAngleBetweenPoints(this.circleCenterPoint, this.starPosition, this.updatePosition);
}
this.rotateAngle = this.upRotates + this.startRotates;
});
}
.alignItems(HorizontalAlign.Center)
.justifyContent(FlexAlign.Center)
.width('100%')
.height('100%');
}
} 效果图:
