鸿蒙应用示例:仿钉钉日历新建日程

【开发环境】

开发工具:DevEco Studio NEXT Beta1 Build Version: 5.0.3.814

工程API版本:12

【组件初始化】

Page02结构体定义了几个状态变量:offsetX, offsetY, positionX, positionY,这些变量用于跟踪用户操作引起的UI位置变化。containerHeight和originalHeight则用来控制UI容器的高度,同时containerHeightChanged方法确保容器的高度不会低于设定的最小值。

【用户界面构建】

build()方法是创建UI的核心逻辑。它首先创建了一个Column布局,用于垂直排列子元素。内部的RelativeContainer则允许更精细的位置控制,通过绝对或相对位置来放置子元素。

【动态UI元素】

在RelativeContainer中,我们添加了一个带有文字的矩形和两个圆点。这两个圆点不仅作为视觉元素,还绑定了拖动手势(PanGesture),这使得用户可以通过拖动操作改变容器的高度,从而模拟日程项的增加或减少过程。

【手势处理】

我们使用了两种不同的拖动手势:一种用于调整容器的高度,另一种则用于整体移动容器的位置。这些手势操作更新了相应的状态变量,并最终反映在UI布局上。

【完整代码】

javascript 复制代码
@Entry
@Component
struct Page02 {
  @State offsetX: number = 0;
  @State offsetY: number = 0;
  @State positionX: number = 0;
  @State positionY: number = 0;
  @State @Watch('containerHeightChanged') containerHeight: number = 50;
  @State originalHeight: number = 50;

  containerHeightChanged() {
    if (this.containerHeight < 50) {
      this.containerHeight = 50
    }
  }

  build() {
    Column() {
      RelativeContainer() {
        Rect()
          .fill("#2fd164")
          .borderColor("#39bd66")
          .borderWidth(1)
          .borderRadius(5)
          .clip(true)
          .width("100%")
          .height("100%")

        Text("再次点击新建日程")
          .fontColor(Color.White)
          .margin({ top: 30, left: 130 })
          .fontSize(15)
          .alignRules({
            center: { anchor: "__container__", align: VerticalAlign.Top },
            middle: { anchor: "__container__", align: HorizontalAlign.Start }
          })

        Stack() {
          Circle({ height: 5, width: 5 })
            .fill(Color.White)
            .stroke(Color.White)
            .strokeWidth(2)
        }
        .hitTestBehavior(HitTestMode.Block)
        .padding(20)
        .margin({ left: 60 })
        .alignRules({
          center: { anchor: "__container__", align: VerticalAlign.Top },
          middle: { anchor: "__container__", align: HorizontalAlign.Start }
        })
        .gesture(PanGesture({
          fingers: 1,
          direction: PanDirection.Vertical,
          distance: 1
        }).onActionUpdate((event: GestureEvent) => {
          if (event) {
            this.offsetY = this.positionY + event.offsetY;
            this.containerHeight = this.originalHeight - event.offsetY;
          }
        }).onActionEnd(() => {
          this.positionX = this.offsetX;
          this.positionY = this.offsetY;
          this.originalHeight = this.containerHeight;
        })
        )

        Stack() {
          Circle({ height: 5, width: 5 })
            .fill(Color.White)
            .stroke(Color.White)
            .strokeWidth(2)
        }
        .hitTestBehavior(HitTestMode.Block)
        .padding(20)
        .margin({ right: 60 })
        .alignRules({
          center: { anchor: "__container__", align: VerticalAlign.Bottom },
          middle: { anchor: "__container__", align: HorizontalAlign.End }
        })
        .gesture(PanGesture({
          fingers: 1,
          direction: PanDirection.Vertical,
          distance: 2
        }).onActionUpdate((event: GestureEvent) => {
          this.containerHeight = this.originalHeight + event.offsetY;
        }).onActionEnd(() => {
          this.originalHeight = this.containerHeight;
        })
        )
      }
      .height(this.containerHeight)
      .width("80%")
      .translate({ x: this.offsetX, y: this.offsetY })
      .gesture(PanGesture({
        fingers: 1,
        direction: PanDirection.Vertical,
        distance: 1
      }).onActionUpdate((event: GestureEvent) => {
        if (event) {
          this.offsetX = this.positionX + event.offsetX;
          this.offsetY = this.positionY + event.offsetY;
        }
      }).onActionEnd(() => {
        this.positionX = this.offsetX;
        this.positionY = this.offsetY;
      })
      )
    }
    .height('100%')
    .width('100%')
  }
}
相关推荐
烬头882114 小时前
React Native鸿蒙跨平台实现二维码联系人APP(QRCodeContactApp)
javascript·react native·react.js·ecmascript·harmonyos
xiaoqi92216 小时前
React Native鸿蒙跨平台如何实现分类页面组件通过searchQuery状态变量管理搜索输入,实现了分类的实时过滤功能
javascript·react native·react.js·ecmascript·harmonyos
听麟16 小时前
HarmonyOS 6.0+ 智慧出行导航APP开发实战:离线地图与多设备位置协同落地
华为·wpf·harmonyos
qq_1777673716 小时前
React Native鸿蒙跨平台实现应用介绍页,实现了应用信息卡片展示、特色功能网格布局、权限/联系信息陈列、评分展示、模态框详情交互等通用场景
javascript·react native·react.js·ecmascript·交互·harmonyos
jin12332218 小时前
基于React Native鸿蒙跨平台地址管理是许多电商、外卖、物流等应用的重要功能模块,实现了地址的添加、编辑、删除和设置默认等功能
javascript·react native·react.js·ecmascript·harmonyos
2501_9209317018 小时前
React Native鸿蒙跨平台医疗健康类的血压记录,包括收缩压、舒张压、心率、日期、时间、备注和状态
javascript·react native·react.js·ecmascript·harmonyos
2501_9209317019 小时前
React Native鸿蒙跨平台使用useState管理健康记录和过滤状态,支持多种健康数据类型(血压、体重等)并实现按类型过滤功能
javascript·react native·react.js·ecmascript·harmonyos
2501_9219308319 小时前
高级进阶 React Native 鸿蒙跨平台开发:InteractionManager 交互优化
react native·harmonyos
前端不太难20 小时前
HarmonyOS PC 文档模型完整范式
华为·状态模式·harmonyos
ITUnicorn21 小时前
【HarmonyOS6】从零实现自定义计时器:掌握TextTimer组件与计时控制
华为·harmonyos·arkts·鸿蒙·harmonyos6