前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >【HarmonyOS之旅】基于ArkTS开发(三) -> 兼容JS的类Web开发(四) -> 构建用户界面(二)

【HarmonyOS之旅】基于ArkTS开发(三) -> 兼容JS的类Web开发(四) -> 构建用户界面(二)

作者头像
枫叶丹
发布2025-02-04 09:44:48
发布2025-02-04 09:44:48
4800
代码可运行
举报
文章被收录于专栏:C++C++
运行总次数:0
代码可运行

1 -> 添加交互

添加交互可以通过在组件上关联事件实现。本节将介绍如何用div、text、image组件关联click事件,构建一个如下图所示的点赞按钮。

点赞按钮通过一个div组件关联click事件实现。div组件包含一个image组件和一个text组件:

  • image组件用于显示未点赞和点赞的效果。click事件函数会交替更新点赞和未点赞图片的路径。
  • text组件用于显示点赞数,点赞数会在click事件的函数中同步更新。

click事件作为一个函数定义在js文件中,可以更改isPressed的状态,从而更新显示的image组件。如果isPressed为真,则点赞数加1。该函数在hml文件中对应的div组件上生效,点赞按钮各子组件的样式设置在css文件当中。具体的实现示例如下:

代码语言:javascript
代码运行次数:0
复制
<!-- test.hml -->
<!-- 点赞按钮 -->
<div>
  <div class="like" onclick="likeClick">
    <image class="like-img" src="{
  
  {likeImage}}" focusable="true"></image>
    <text class="like-num" focusable="true">{
  
  {total}}</text>
  </div>
</div>
代码语言:javascript
代码运行次数:0
复制
/* test.css */
.like {
  width: 104px;
  height: 54px;
  border: 2px solid #bcbcbc;
  justify-content: space-between;
  align-items: center;
  margin-left: 72px;
  border-radius: 8px;
}
.like-img {
  width: 33px;
  height: 33px;
  margin-left: 14px;
}
.like-num {
  color: #bcbcbc;
  font-size: 20px;
  margin-right: 17px;
}
代码语言:javascript
代码运行次数:0
复制
// test.js
export default {
  data: {
    likeImage: '/common/unLike.png',
    isPressed: false,
    total: 20,
  },
  likeClick() {
    var temp;
    if (!this.isPressed) {
      temp = this.total + 1;
      this.likeImage = '/common/like.png';
    } else {
      temp = this.total - 1;
      this.likeImage = '/common/unLike.png';
    }
    this.total = temp;
    this.isPressed = !this.isPressed;
  },
}

除此之外,还提供了很多表单组件,例如开关、标签、滑动选择器等。

2 -> 动画

动画分为静态动画和连续动画。

2.1 -> 静态动画

静态动画的核心是transform样式,主要可以实现以下三种变换类型,一次样式设置只能实现一种类型变换。

  • translate:沿水平或垂直方向将指定组件移动所需距离。
  • scale:横向或纵向将指定组件缩小或放大到所需比例。
  • rotate:将指定组件沿横轴或纵轴或中心点旋转指定的角度。
代码语言:javascript
代码运行次数:0
复制
<!-- test.hml -->
<div class="container">
  <text class="translate">hello</text>
  <text class="rotate">hello</text>
  <text class="scale">hello</text>
</div>
代码语言:javascript
代码运行次数:0
复制
/* test.css */
.container {
  flex-direction: column;
  align-items: center;
}
.translate {
  height: 150px;
  width: 300px;
  font-size: 50px;
  background-color: #008000;
  transform: translate(200px);
}
.rotate {
  height: 150px;
  width: 300px;
  font-size: 50px;
  background-color: #008000;
  transform-origin: 200px 100px;
  transform: rotateX(45deg);
}
.scale {
  height: 150px;
  width: 300px;
  font-size: 50px;
  background-color: #008000;
  transform: scaleX(1.5);
}

2.2 -> 连续动画

静态动画只有开始状态和结束状态,没有中间状态,如果需要设置中间的过渡状态和转换效果,需要使用连续动画实现。

连续动画的核心是animation样式,它定义了动画的开始状态、结束状态以及时间和速度的变化曲线。通过animation样式可以实现的效果有:

  • animation-name:设置动画执行后应用到组件上的背景颜色、透明度、宽高和变换类型。
  • animation-delayanimation-duration:分别设置动画执行后元素延迟和持续的时间。
  • animation-timing-function:描述动画执行的速度曲线,使动画更加平滑。
  • animation-iteration-count:定义动画播放的次数。
  • animation-fill-mode:指定动画执行结束后是否恢复初始状态。

animation样式需要在css文件中先定义keyframe,在keyframe中设置动画的过渡效果,并通过一个样式类型在hml文件中调用。animation-name的使用示例如下:

代码语言:javascript
代码运行次数:0
复制
<!-- test.hml -->
<div class="item-container">
  <text class="header">animation-name</text>
  <div class="item {
  
  {colorParam}}">
    <text class="txt">color</text>
  </div>
  <div class="item {
  
  {opacityParam}}">
    <text class="txt">opacity</text>
  </div>
  <input class="button" type="button" name="" value="show" onclick="showAnimation"/>
</div>
代码语言:javascript
代码运行次数:0
复制
/* test.css */
.item-container {
  margin-right: 60px;
  margin-left: 60px;
  flex-direction: column;
}
.header {
  margin-bottom: 20px;
}
.item {
  background-color: #f76160;
}
.txt {
  text-align: center;
  width: 200px;
  height: 100px;
}
.button {
  width: 200px;
  font-size: 30px;
  background-color: #09ba07;
}
.color {
  animation-name: Color;
  animation-duration: 8000ms;
}
.opacity {
  animation-name: Opacity;
  animation-duration: 8000ms;
}
@keyframes Color {
  from {
    background-color: #f76160;
  }
  to {
    background-color: #09ba07;
  }
}
@keyframes Opacity {
  from {
    opacity: 0.9;
  }
  to {
    opacity: 0.1;
  }
}
代码语言:javascript
代码运行次数:0
复制
// test.js
export default {
  data: {
    colorParam: '',
    opacityParam: '',
  },
  showAnimation: function () {
    this.colorParam = '';
    this.opacityParam = '';
    this.colorParam = 'color';
    this.opacityParam = 'opacity';
  },
}

3 -> 手势事件

手势表示由单个或多个事件识别的语义动作(例如:点击、拖动和长按)。一个完整的手势也可能由多个事件组成,对应手势的生命周期。支持的事件有:

触摸

  • touchstart:手指触摸动作开始。
  • touchmove:手指触摸后移动。
  • touchcancel:手指触摸动作被打断,如来电提醒、弹窗。
  • touchend:手指触摸动作结束。

点击

click:用户快速轻敲屏幕。

长按

longpress:用户在相同位置长时间保持与屏幕接触。

具体的使用示例如下:

代码语言:javascript
代码运行次数:0
复制
<!-- test.hml -->
<div class="container">
  <div class="text-container" onclick="click">
    <text class="text-style">{
  
  {onClick}}</text>
  </div>
  <div class="text-container" ontouchstart="touchStart">
    <text class="text-style">{
  
  {touchstart}}</text>
  </div>
  <div class="text-container" ontouchmove="touchMove">
    <text class="text-style">{
  
  {touchmove}}</text>
  </div>
  <div class="text-container" ontouchend="touchEnd">
    <text class="text-style">{
  
  {touchend}}</text>
  </div>
  <div class="text-container" ontouchcancel="touchCancel">
    <text class="text-style">{
  
  {touchcancel}}</text>
  </div>
  <div class="text-container" onlongpress="longPress">
    <text class="text-style">{
  
  {onLongPress}}</text>
  </div>
</div>
代码语言:javascript
代码运行次数:0
复制
/* test.css */
.container {
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.text-container {
  margin-top: 10px;
  flex-direction: column;
  width: 750px;
  height: 50px;
  background-color: #09ba07;
}
.text-style {
  width: 100%;
  line-height: 50px;
  text-align: center;
  font-size: 24px;
  color: #ffffff;
}
代码语言:javascript
代码运行次数:0
复制
// test.js
export default {
  data: {
    touchstart: 'touchstart',
    touchmove: 'touchmove',
    touchend: 'touchend',
    touchcancel: 'touchcancel',
    onClick: 'onclick',
    onLongPress: 'onlongpress',
  },
  touchCancel: function (event) {
    this.touchcancel = 'canceled';
  },
  touchEnd: function(event) {
    this.touchend = 'ended';
  },
  touchMove: function(event) {
    this.touchmove = 'moved';
  }, 
  touchStart: function(event) {
    this.touchstart = 'touched';
  },
  longPress: function() {
    this.onLongPress = 'longpressed';
  },
  click: function() {
    this.onClick = 'clicked';
  },
}

4 -> 页面路由

很多应用由多个页面组成,比如用户可以从音乐列表页面点击歌曲,跳转到该歌曲的播放界面。需要通过页面路由将这些页面串联起来,按需实现跳转。

页面路由router根据页面的url找到目标页面,从而实现跳转。以最基础的两个页面之间的跳转为例,具体实现步骤如下:

  1. 在“Project“窗口,打开entry > src > mainjsdefault,右键点击pages文件夹,选择NewJS Page,创建一个详情页。
  2. 调用router.push()路由到详情页。
  3. 调用router.back()回到首页。

4.1 -> 构建页面布局

index和detail这两个页面均包含一个text组件和button组件:text组件用来指明当前页面,button组件用来实现两个页面之间的相互跳转。hml文件代码示例如下:

代码语言:javascript
代码运行次数:0
复制
<!-- index.hml -->
<div class="container">
  <text class="title">This is the index page.</text>
  <button type="capsule" value="Go to the second page" class="button" onclick="launch"></button>
</div>
代码语言:javascript
代码运行次数:0
复制
<!-- detail.hml -->
<div class="container">
  <text class="title">This is the detail page.</text>
  <button type="capsule" value="Go back" class="button" onclick="launch"></button>
</div>

4.2 -> 构建页面样式

构建index和detail页面的页面样式,text组件和button组件居中显示,两个组件之间间距为50px。css代码如下(两个页面样式代码一致):

代码语言:javascript
代码运行次数:0
复制
/* index.css */
/* detail.css */
.container {
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.title {
  font-size: 50px;
  margin-bottom: 50px;
}

4.3 -> 实现跳转

为了使button组件的launch方法生效,需要在页面的js文件中实现跳转逻辑。调用router.push()接口将url指定的页面添加到路由栈中,即跳转到url指定的页面。在调用router方法之前,需要导入router模块。代码示例如下:

代码语言:javascript
代码运行次数:0
复制
// index.js
import router from '@ohos.router';
export default {
  launch() {
    router.push ({
      url: 'pages/detail/detail',
    });
  },
}
代码语言:javascript
代码运行次数:0
复制
// detail.js
import router from '@ohos.router';
export default {
  launch() {
    router.back();
  },
}

运行效果如下图所示:

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-02-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1 -> 添加交互
  • 2 -> 动画
    • 2.1 -> 静态动画
    • 2.2 -> 连续动画
  • 3 -> 手势事件
  • 4 -> 页面路由
    • 4.1 -> 构建页面布局
    • 4.2 -> 构建页面样式
    • 4.3 -> 实现跳转
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档