logo
积分中心
鸿蒙UI开发——基于onTouch事件实现表情选择胶囊
头像 蓝亭书序 2026-11-18 13:43:32    发布
0 浏览 0 收藏

1、背 景

有朋友留言说,抖音APP中,长按评论按钮触发的快捷表情选择胶囊动画比较好(效果如下图),希望使用鸿蒙ArkTs也实现一个类似的。

图片

本文在鸿蒙ArkTs下也实现一个类似的效果,如下:

首先,核心交互流程与抖音APP保持一致,即:

    1. 长按一个按钮,我们可以在按钮旁边生成一个快捷表情选择胶囊;
    2. 手指可以快捷的在候选表情上滑动,在选中的表情上停留时,该表情会稍微放大;

动画细节上做了一些个人效果,例如:

  • 胶囊展示到界面时,候选表情做了一个类似IOS解锁时的类飞入效果;
  • 我们在表情胶囊上滑动选择时,整个表情胶囊不会随着动画效果的改变发生宽度抖动。


下面开始介绍如何实现,文末有源代码,有需要的同学自取。

2、问题分析

上述的交互效果其实难点并不在于动画效果,在于onTouch事件的管理,因为,我们在长按评论按钮时,此时系统将会分配事件流给评论按钮消费。

如果我们想在用户不松手移动到其他位置,让其他组件也生效,实现过程将稍微复杂一些。

本文的实现思路是:监听评论按钮的onTouch事件,在事件move过程中,实时获取该事件的发生坐标,基于坐标去判断坐落的表情包位置,从而控制焦点放大效果。

📢📢注意onTouch事件的调试千万要在真机或者模拟器中执行,在预览器中执行可能会出现非预期的问题。


❓我们怎么获取指定组件的坐标呢?

虽然我们通过onTouch事件可以知道用户手指的位置,那我们还有一个问题没解决,就是怎么知道各个表情包的坐标呢?

ArkTs为我们提供了一个API,根据组件ID获取组件实例对象, 通过组件实例对象将获取的坐标位置和大小同步返回给调用方,接口如下:


import { componentUtils } from '@kit.ArkUI';
// 调用方式
let modePosition:componentUtils.ComponentInfo = componentUtils.getRectangleById("id");


3、布 局

布局比较简单,在本文中,将整个表情胶囊用一个Row包裹,另外,评论图标与表情胶囊整体属于在一个Row中。示意图如下:

图片

为了方便动态插拔,我们将图标资源集合使用一个数组来动态维护,资源类型定义与数组定义如下:


interface CommentIconInfo {
  source: Resource,
  id: string;
}
const commentIcons: Array<CommentIconInfo> =
  [
    {
      id: 'page_main_icon1',
      source: $r('app.media.send_you_a_little_red_flower'),
    },
    {
      id: 'page_main_icon2',
      source: $r('app.media.powerful'),
    },
    {
      id: 'page_main_icon3',
      source: $r('app.media.send_heart'),
    }, {
    id: 'page_main_icon4',
    source: $r('app.media.surprise'),
  },
  ]


布局代码如下:


build() {
  Column() {
    Row() {
      Row() {
        ForEach(this.commentIcons, (item: CommentIconInfo) => {
          Image(item.source)
            .id(item.id)
            .width(this.selectedId === item.id ? this.selectedSize : this.normalSize)
            .height(this.selectedId === item.id ? this.selectedSize : this.normalSize)
            .padding({ left: this.iconMarginLeft })
            .animation({ duration: this.animDuration, curve: curves.springMotion() })
            .visibility(this.showCommentIconPanel ? Visibility.Visible : Visibility.None)
        }, (item: CommentIconInfo) => {
          return `${item.id}` // 复写id生成器
        })
      }
      .visibility(this.showCommentIconPanel ? Visibility.Visible : Visibility.None)
      .backgroundColor(Color.Pink)
      .width(this.showCommentIconPanel ? this.getRowWidth() : 10)
      .borderRadius(this.selectedId ? 40 : 20)
      .padding({ left: this.paddingHoriz, right: this.paddingHoriz })
      .animation({ duration: this.animDuration, curve: curves.springMotion() })

      SymbolGlyph($r('sys.symbol.ellipsis_message_fill'))
        .fontSize(60)
        .renderingStrategy(SymbolRenderingStrategy.MULTIPLE_COLOR)
        .fontColor([Color.Green])
        .margin({ left: 10 })
        .onTouch(this.onTouchEvent)

    }
    .justifyContent(FlexAlign.End)
    .width('100%')
  }
  .padding(20)
  .height('100%')
}


4、onTouch事件处理

我们在onTouch事件中需要做两个核心事情:

  1. 控制表情胶囊的显示/隐藏
  2. 控制用户手指指向的表情包,并聚焦放大显示

代码如下:


private onTouchEvent = (event: TouchEvent) => {
  if (!event) {
    return;
  }
  // 手指按下0.5s后弹出表情选择面板
  if (event.type === TouchType.Down) {
    this.lastTouchDown = Date.now();
  } else if (event.type === TouchType.Up || event.type === TouchType.Cancel) {
    this.showCommentIconPanel = false; // 松手后,取消显示表情面板(具体消失逻辑可以根据业务需求调整)
  } else if (!this.showCommentIconPanel) {
    this.showCommentIconPanel = (Date.now() - this.lastTouchDown) > 500;
  }
  this.checkCommentIcons(event);
}

// 判断用户手指是否选中了某个表情包
private checkCommentIcons = (event: TouchEvent) => {
  const touchInfo = event.touches[0];
  this.selectedId = '';
  if (!touchInfo) {
    return;
  }
  const windowX = Math.ceil(touchInfo.windowX); // 获取用户手指x坐标
  const context = this.getUIContext();
  // 检测用户手指x坐标可以命中的表情
  this.commentIcons.forEach(iconInfo => {
    if (event.type === TouchType.Up) {
      return;
    }
    const compInfo = componentUtils.getRectangleById(iconInfo.id);
    const x = Math.ceil(context.px2vp(compInfo.windowOffset.x));
    const width = Math.ceil(context.px2vp(compInfo.size.width));
    if (windowX >= x && windowX <= x + width) {
      this.selectedId = iconInfo.id; // 范围中有表情被选中
    }
  });
  this.commentIcons = [...this.commentIcons]; // 低成本刷新数组(浅拷贝)
}


5、源代码

示例效果如下:

图片

下方的源代码替换了11、15、19、22行的图片资源后,可以正常运行。代码如下:


import { componentUtils } from '@kit.ArkUI';
import { curves } from '@kit.ArkUI';
interface CommentIconInfo {
  source: Resource,
  id: string;
}
const commentIcons: Array<CommentIconInfo> =
  [
    {
      id: 'page_main_icon1',
      source: $r('app.media.send_you_a_little_red_flower'),
    },
    {
      id: 'page_main_icon2',
      source: $r('app.media.powerful'),
    },
    {
      id: 'page_main_icon3',
      source: $r('app.media.send_heart'),
    }, {
    id: 'page_main_icon4',
    source: $r('app.media.surprise'),
  },
  ]
@Entry
@Component
struct Index {
  @State selectedSize: number = 60;
  @State normalSize: number = 35;
  @State showCommentIconPanel: boolean = false;
  @State commentIcons: Array<CommentIconInfo> = commentIcons;
  @State selectedId: string = '';
  // 一些本地使用的量
  lastTouchDown: number = 0;
  iconMarginLeft: number = 5;
  paddingHoriz: number = 10; // 左右padding
  animDuration: number = 500;
  private onTouchEvent = (event: TouchEvent) => {
    if (!event) {
      return;
    }
    // 手指按下0.5s后弹出表情选择面板
    if (event.type === TouchType.Down) {
      this.lastTouchDown = Date.now();
    } else if (event.type === TouchType.Up || event.type === TouchType.Cancel) {
      this.showCommentIconPanel = false; // 松手后,取消显示表情面板(具体消失逻辑可以根据业务需求调整)
    } else if (!this.showCommentIconPanel) {
      this.showCommentIconPanel = (Date.now() - this.lastTouchDown) > 500;
    }
    this.checkCommentIcons(event);
  }
  private checkCommentIcons = (event: TouchEvent) => {
    const touchInfo = event.touches[0];
    this.selectedId = '';
    if (!touchInfo) {
      return;
    }
    const windowX = Math.ceil(touchInfo.windowX); // 获取用户手指x坐标
    const context = this.getUIContext();
    // 检测用户手指x坐标可以命中的表情
    this.commentIcons.forEach(iconInfo => {
      if (event.type === TouchType.Up) {
        return;
      }
      const compInfo = componentUtils.getRectangleById(iconInfo.id);
      const x = Math.ceil(context.px2vp(compInfo.windowOffset.x));
      const width = Math.ceil(context.px2vp(compInfo.size.width));
      if (windowX >= x && windowX <= x + width) {
        this.selectedId = iconInfo.id; // 范围中有表情被选中
      }
    });
    this.commentIcons = [...this.commentIcons]; // 低成本刷新数组(浅拷贝)
  }
  build() {
    Column() {
      Row() {
        Row() {
          ForEach(this.commentIcons, (item: CommentIconInfo) => {
            Image(item.source)
              .id(item.id)
              .width(this.selectedId === item.id ? this.selectedSize : this.normalSize)
              .height(this.selectedId === item.id ? this.selectedSize : this.normalSize)
              .padding({ left: this.iconMarginLeft })
              .animation({ duration: this.animDuration, curve: curves.springMotion() })
              .visibility(this.showCommentIconPanel ? Visibility.Visible : Visibility.None)
          }, (item: CommentIconInfo) => {
            return `${item.id}` // 复写id生成器
          })
        }
        .visibility(this.showCommentIconPanel ? Visibility.Visible : Visibility.None)
        .backgroundColor(Color.Pink)
        .width(this.showCommentIconPanel ? this.getRowWidth() : 10)
        .borderRadius(this.selectedId ? 40 : 20)
        .padding({ left: this.paddingHoriz, right: this.paddingHoriz })
        .animation({ duration: this.animDuration, curve: curves.springMotion() })
        SymbolGlyph($r('sys.symbol.ellipsis_message_fill'))
          .fontSize(60)
          .renderingStrategy(SymbolRenderingStrategy.MULTIPLE_COLOR)
          .fontColor([Color.Green])
          .margin({ left: 10 })
          .onTouch(this.onTouchEvent)
      }
      .justifyContent(FlexAlign.End)
      .width('100%')
    }
    .padding(20)
    .height('100%')
  }
  private getRowWidth() { // 防止抖动
    const baseWidth =
      this.paddingHoriz + this.paddingHoriz + this.commentIcons.length * this.normalSize +
        (this.commentIcons.length - 1) * this.iconMarginLeft;
    if (this.selectedId) {
      return baseWidth + this.selectedSize - this.normalSize;
    }
    return baseWidth;
  }
}


项目源代码地址:


https://gitee.com/lantingshuxu/harmony-class-room-demos/tree/feat%2FmagicComment/




©本站发布的所有内容,包括但不限于文字、图片、音频、视频、图表、标志、标识、广告、商标、商号、域名、软件、程序等,除特别标明外,均来源于网络或用户投稿,版权归原作者或原出处所有。我们致力于保护原作者版权,若涉及版权问题,请及时联系我们进行处理。
分类
HarmonyOS

暂无评论数据

加载中...

发布

地址:北京市朝阳区北三环东路三元桥曙光西里甲1号第三置业A座1508室 电话:13391790444或(010)62178877
版权所有:电脑商情信息服务集团 北京赢邦策略咨询有限责任公司
声明:本媒体部分图片、文章来源于网络,版权归原作者所有,我司致力于保护作者版权,如有侵权,请与我司联系删除

京ICP备:2022009079号-2

京公网安备:11010502051901号

ICP证:京B2-20230255