温馨提示:本篇博客的详细代码已发布到 git : https://gitcode.com/nutpi/HarmonyosNext 可以下载运行哦!
这组模型类主要用于处理手势交互中的各种变换操作,包括:
@Observed
是HarmonyOS中的一个重要装饰器,用于实现数据响应式。当被@Observed
装饰的类的属性发生变化时,会自动触发UI更新。
@Observed
export class PositionModel {
x: number;
y: number;
constructor(x: number = 0, y: number = 0) {
this.x = x;
this.y = y;
}
}
属性 | 类型 | 默认值 | 说明 |
---|---|---|---|
x | number | 0 | 横坐标位置 |
y | number | 0 | 纵坐标位置 |
@Observed
export class OffsetModel {
public currentX: number;
public currentY: number;
public lastX: number = 0;
public lastY: number = 0;
// ... 其他方法
}
属性 | 类型 | 说明 |
---|---|---|
currentX | number | 当前X轴偏移量 |
currentY | number | 当前Y轴偏移量 |
lastX | number | 上一次X轴偏移量 |
lastY | number | 上一次Y轴偏移量 |
// 重置所有偏移量为0
reset(): void {
this.currentX = 0;
this.currentY = 0;
this.lastX = 0;
this.lastY = 0;
}
// 保存当前偏移量到last变量
stash(): void {
this.lastX = this.currentX;
this.lastY = this.currentY;
}
@Observed
export class RotateModel {
public currentRotate: number;
public lastRotate: number = 0;
public startAngle: number = 20;
// ... 其他方法
}
属性 | 类型 | 默认值 | 说明 |
---|---|---|---|
currentRotate | number | 0 | 当前旋转角度 |
lastRotate | number | 0 | 上次旋转角度 |
startAngle | number | 20 | 触发旋转的最小角度 |
stash(): void {
// 将角度规范化到0-360度范围内
let angle = 360;
this.lastRotate = this.currentRotate % angle;
}
@Observed
export class ScaleModel {
public scaleValue: number;
public lastValue: number;
public maxScaleValue: number;
public extraScaleValue: number;
public readonly defaultScaleValue: number = 1;
// ... 其他方法
}
属性 | 类型 | 默认值 | 说明 |
---|---|---|---|
scaleValue | number | 1.0 | 当前缩放值 |
lastValue | number | 1.0 | 上次缩放值 |
maxScaleValue | number | 1.5 | 最大缩放限制 |
extraScaleValue | number | 0.2 | 额外缩放系数 |
defaultScaleValue | number | 1 | 默认缩放值 |
reset(): void {
this.scaleValue = this.defaultScaleValue;
this.lastValue = this.scaleValue;
}
stash(): void {
this.lastValue = this.scaleValue;
}
这些模型类通常配合使用,实现复杂的手势交互功能:
// 示例:创建一个支持移动、缩放、旋转的组件
class GestureHandler {
private position = new PositionModel();
private offset = new OffsetModel();
private rotate = new RotateModel();
private scale = new ScaleModel();
// 处理手势开始
onGestureStart() {
// 保存初始状态
this.offset.stash();
this.rotate.stash();
this.scale.stash();
}
// 处理手势变化
onGestureChange(dx: number, dy: number, angle: number, scale: number) {
// 更新各个模型的值
this.offset.currentX += dx;
this.offset.currentY += dy;
this.rotate.currentRotate = angle;
this.scale.scaleValue = scale;
}
}
stash()
方法保存状态reset()
方法重置状态通过这些模型的组合使用,可以实现丰富的手势交互功能,如图片查看器、地图操作、可视化编辑器等复杂交互场景。理解这些模型的工作原理,对于开发高质量的HarmonyOS应用至关重要。