本示例主要介绍了利用position和onTouch来实现首页下拉进入二楼、二楼上划进入首页的效果场景,利用translate和opacity实现动效的移动和缩放,并将界面沉浸式(全屏)显示。
使用说明
本例涉及的关键特性和实现方案如下:
Column() {
// 二楼页面
Column() {
this.floorViewBuilder();
}
// 固定二楼刚开始位置
.position({
x: 0,
// Y轴大小
y: this.mainPageOffsetY
})
...
// 一楼页面
Column() {
this.mainPageBuilder();
}
.position({
x: 0,
// Y轴大小加上二楼高度
y: this.offsetY + this.floorHeight
})
}
.clip(true) // TODO:知识点:按指定的形状对当前组件进行裁剪,参数为boolean类型时,设置是否按照父容器边缘轮廓进行裁剪。
Column() {
...
// 一楼页面
Column() {
this.mainPageBuilder();
}
...
}
.onTouch((event) => {
switch (event.type) {
case TouchType.Down:
this.onTouchDown(event);
break;
case TouchType.Move:
this.onTouchMove(event);
break;
...
break;
}
event.stopPropagation(); // 阻止冒泡
})
/**
* 按下事件、获取按下事件的位置
* @param event 触屏事件
*/
private onTouchDown(event: TouchEvent) {
// 获取触发按压事件Y轴的位置
this.lastY = event.touches[0].windowY;
...
}
/**
* 滑动事件
* @param event 触屏事件
*/
private onTouchMove(event: TouchEvent) {
...
let currentY = event.touches[0].windowY;
// onTouch事件中本次Y轴大小减去上一次获取的Y轴大小,为负值则是向上滑动,为正值则是向下滑动
let deltaY = currentY - this.lastY;
...
}
Row() {
// this.floorHeight - Math.abs(this.offsetY)为下拉距离,下拉距离超过MINI_SHOW_DISTANCE(动效最小展示距离)且小于TRIGGER_HEIGHT(触发动画高度或者动效消失高度)展示动画
if ((this.floorHeight - Math.abs(this.offsetY)) > MINI_SHOW_DISTANCE && (this.floorHeight - Math.abs(this.offsetY)) <= TRIGGER_HEIGHT) {
Row() {
// 向左偏移圆
Blank()
.width(this.roundSize)
.height(this.roundSize)
.borderRadius($r('app.integer.second_floor_circular_border_radius'))
.scale(this.immediatelyScale)
.backgroundColor($r('app.color.second_floor_circular_color'))
.translate({ x: this.animationXLeft })
.opacity(((this.mFloorHeight - Math.abs(this.offsetY)) / this.mFloorHeight)) // 使用下拉距离除以二楼高度获得圆的透明度
// 中心加载点
Blank()
...
// 向右偏移圆
Blank()
...
}
}
}
欢迎大家关注公众号<程序猿百晓生>,可以了解到以下内容:
1.OpenHarmony开发基础
2.OpenHarmony北向开发环境搭建
3.鸿蒙南向开发环境的搭建
4.鸿蒙生态应用开发白皮书V2.0 & V3.0
5.鸿蒙开发面试真题(含参考答案)
6.TypeScript入门学习手册
7.OpenHarmony 经典面试题(含参考答案)
8.OpenHarmony设备开发入门【最新版】
9.沉浸式剖析OpenHarmony源代码
10.系统定制指南
11.【OpenHarmony】Uboot 驱动加载流程
12.OpenHarmony构建系统--GN与子系统、部件、模块详解
13.ohos开机init启动流程
14.鸿蒙版性能优化指南
.......
/**
* 滑动事件
* @param event 触屏事件
*/
private onTouchMove(event: TouchEvent) {
...
// TODO:知识点:确定是滑动状态后,进入动效界面,this.floorHeight减去this.offsetY的绝对值为滑动距离,在大于60(60指的是中心圆加载范围)和隐藏动效高度范围对左右圆的平移距离和和缩放进行设置
if (((this.floorHeight - Math.abs(this.offsetY)) <= TRIGGER_HEIGHT) && (this.floorHeight - Math.abs(this.offsetY)) >= 60) {
this.roundSize = 20;
this.animationXLeft = 60;
this.animationXRight = -60;
// (this.floorHeight - Math.abs(this.offsetY))除以TRIGGER_HEIGHT 获取下拉百分比,使用百分比乘以60(60是根据圆最开始的位置获取)获得每次平移的距离,用来达到左右圆的X轴最后为0
this.animationXLeft = this.animationXLeft - ((this.floorHeight - Math.abs(this.offsetY)) / TRIGGER_HEIGHT) * 60;
this.animationXRight = this.animationXRight + ((this.floorHeight - Math.abs(this.offsetY)) / TRIGGER_HEIGHT) * 60;
// 使用移动距离除以动效消失的高度,用来获取左右圆的缩放比例
this.immediatelyScale = {
x: ((this.floorHeight - Math.abs(this.offsetY)) / TRIGGER_HEIGHT),
y: ((this.floorHeight - Math.abs(this.offsetY)) / TRIGGER_HEIGHT)
};
} else if (((this.floorHeight - Math.abs(this.offsetY)) < 60)) {
// TODO:知识点:在中心圆加载的时候,左右圆是不显示的,因此将左右圆缩放比例大小调整为0,使用移动高度除以60(中心圆加载高度)再乘以20(圆的最终大小),以此来达到中心圆的加载效果
this.roundSize = 0;
this.roundSize = 20 * ((this.floorHeight - Math.abs(this.offsetY)) / 60);
this.immediatelyScale = {
x: 0,
y: 0
};
} else {
// 设置当二楼回收显示一楼时,三个圆属于加载成功状态
this.roundSize = 20;
this.immediatelyScale = { x: 1, y: 1 };
this.animationXLeft = 0;
this.animationXRight = 0;
}
...
}
/**
* 触摸抬起或取消触摸事件
*/
private onTouchUp() {
if (this.dragging) {
// 二楼自身的高度减去向下Y轴的位移的绝对值大于触发值进入二楼,否则回弹
if ((this.floorHeight - Math.abs(this.offsetY)) > this.expandFloorTriggerDistance) {
// 进入二楼
this.expandSecondFloor();
} else {
// 未达到触发距离回弹
this.scrollByTop();
}
}
}
本例使用了onTouch事件实时监听获取相关数据,避免在函数中进行冗余或耗时操作,例如应该减少或避免在函数打印日志,会有较大的性能损耗。
本示例使用了setInterval进行页面移动控制,在页面移动到相应的位置后使用clearInterval销毁以降低内存占用。
secondfloorloadanimation // har类型
|---model
| |---AppInfo.ets // App信息
| |---UserInformation.ets // 用户信息
|---view
| |---SecondFloorLoadAnimation.ets // 视图层-应用主页面
| |---SecondFloor.ets // 视图层-应用一楼页面
| |---FloorView.ets // 视图层-应用二楼页面
如果你觉得这篇内容对你还蛮有帮助,我想邀请你帮我三个小忙:
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。