HarmonyOS Next开发----k线图滑动惯性

2024-06-02 13:12

本文主要是介绍HarmonyOS Next开发----k线图滑动惯性,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

K线图的惯性滑动,由于官方提供的Scroller没有设置初始位置的方法,不知道后面会不会支持。由于项目急着上线,所以只有采用另一种方案,滑动结束后模拟计算惯性滑动。

思路:

手指滑动结束后,k线惯性滑动轨迹,类似于匀减速运动。所以可以用个定时器,每个100ms让k线继续平移一段距离,只需要计算出每个时刻的速度即可。对于手指离开屏幕时的速度,可以用平均速度来代替。

class SlideInertia {public callback?: (x: number | null) => void;private startX: number = 0;private lastMoveX: number = 0;private velocity: number = 0;private startTime: number = 0;private timerId: number = -1;constructor(callback?: (x: number | null) => void) {this.callback = callback}public handleTouchStart = (event: GestureEvent) => {this.startX = event.offsetX;this.startTime = new Date().getTime();};public handleTouchMove = (event: GestureEvent) => {this.lastMoveX = event.offsetX;};public handleTouchEnd = () => {const curTime = new Date().getTime();const diffTime = (curTime - this.startTime) / 10;if (diffTime <= 0) {return;}this.velocity = (this.startX - this.lastMoveX) / diffTime;// 释放触摸,开始惯性滑动this.startFling();this.startTime = 0;};private startFling() {const step = () => {this.velocity *= 0.9; // 惯性减弱if (Math.abs(this.velocity) > 1) {if (this.callback) {this.callback(this.velocity);}this.timerId = setTimeout(step, 100);} else {if (this.callback) {this.callback(null);}}};this.timerId = setTimeout(step);}public clearTimer() {if (this.timerId > 0) {clearTimeout(this.timerId);}}
}
然后在组件的TouchDown、TouchMove和TouchUP事件处调用,然后回调里面处理滑动即可。
 .onActionStart((event) => {if (!this.mIsTouchRegionStats && !this.mIsLongPressed && event.fingerList.length === 1) {this.requestParentNotHandleGesture(true);this.mGestureHelper.startScroll();this.slideInertia.handleTouchStart(event);}}).onActionEnd(() => {this.mGestureHelper.stopScroll();this.onDraw();}).onActionUpdate((event?: GestureEvent) => {if (event && event.fingerList.length === 1) {if (this.mIsLongPressed) {let reallyOffsetX: number = event.offsetX - this.mLastOffsetX;let reallyOffsetY: number = event.offsetY - this.mLastOffsetY;this.mLastOffsetX = event.offsetX;this.mLastOffsetY = event.offsetY;this.mTouchPoint.offset(reallyOffsetX, reallyOffsetY);let mainRenderTitleHeight = this.titleHeightArr[0];const padding = UPMarketUIIndexBaseRender.padding;this.mTouchPoint.y = Math.max(mainRenderTitleHeight + padding, this.mTouchPoint.y);this.mTouchPoint.y = Math.min(this.totalHeight - padding * 2, this.mTouchPoint.y);this.mGestureHelper.updateCrossPoint(this.touchX2CrossX(this.mTouchPoint.x), this.mTouchPoint.y);this.callbackLongClickData();this.onDraw();} else if (!this.mGestureHelper.isShowCross()) {//平移if (this.mGestureHelper.scroll(event.offsetX, this.totalWidth)) {this.callbackRegionStatsChanged();this.onDraw();}this.slideInertia.handleTouchMove(event);}}}})
   .onTouch((event: TouchEvent) => {switch (event.type) {case TouchType.Down:this.slideInertia.clearTimer();this.mIsTouchRegionStats = this.mGestureHelper.isTouchRegionStatsControlPoint(new UPPoint(event.touches[0].x, event.touches[0].y));if (this.mIsTouchRegionStats) {this.requestParentNotHandleGesture(true);}this.mLastTouchDownPoint.set(event.touches[0].x, event.touches[0].y);breakcase TouchType.Move:let distanceX: number = Math.abs(this.mLastTouchDownPoint.x - event.touches[0].x);let distanceY: number = Math.abs(this.mLastTouchDownPoint.y - event.touches[0].y);if ((distanceX > distanceY && (this.attrs?.supportTranslationAndZoom || this.mIsLongPressed))|| this.mIsTouchRegionStats) { // 如果触摸点在区间统计控制区域,则支持垂直和水平方向滑动,无需判断distanceXthis.mCanHorizontalScroll = true;}if (this.mIsTouchRegionStats) {let regionStatsIndexChanged = this.mGestureHelper.updateRegionStatsControlPoint(new UPPoint(event.touches[0].x, event.touches[0].y), event.touches[0].x - this.mLastTouchDownPoint.x);if (regionStatsIndexChanged) {this.callbackRegionStatsChanged();this.onDraw();}}breakcase TouchType.Up:case TouchType.Cancel:if (!this.mGestureHelper.isShowRegionStats() && !this.mIsLongPressed) {this.slideInertia.handleTouchEnd();}
由于k线图有些操作时不需要惯性滑动的(比如长按、区间统计打开时等),所以将handleTouchStart和handleTouchMove放在了平移手势回调里面。
  private slideInertia: SlideInertia = new SlideInertia((offsetX: number | null) => {if (offsetX == null) {return;}if (this.mGestureHelper.scroll(0, 0, Math.floor(offsetX))) {this.onDraw();} else {this.slideInertia.clearTimer();}});

请添加图片描述

这篇关于HarmonyOS Next开发----k线图滑动惯性的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1024060

相关文章

一文详解Python如何开发游戏

《一文详解Python如何开发游戏》Python是一种非常流行的编程语言,也可以用来开发游戏模组,:本文主要介绍Python如何开发游戏的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录一、python简介二、Python 开发 2D 游戏的优劣势优势缺点三、Python 开发 3D

基于Python开发Windows自动更新控制工具

《基于Python开发Windows自动更新控制工具》在当今数字化时代,操作系统更新已成为计算机维护的重要组成部分,本文介绍一款基于Python和PyQt5的Windows自动更新控制工具,有需要的可... 目录设计原理与技术实现系统架构概述数学建模工具界面完整代码实现技术深度分析多层级控制理论服务层控制注

Java中的分布式系统开发基于 Zookeeper 与 Dubbo 的应用案例解析

《Java中的分布式系统开发基于Zookeeper与Dubbo的应用案例解析》本文将通过实际案例,带你走进基于Zookeeper与Dubbo的分布式系统开发,本文通过实例代码给大家介绍的非常详... 目录Java 中的分布式系统开发基于 Zookeeper 与 Dubbo 的应用案例一、分布式系统中的挑战二

基于Go语言开发一个 IP 归属地查询接口工具

《基于Go语言开发一个IP归属地查询接口工具》在日常开发中,IP地址归属地查询是一个常见需求,本文将带大家使用Go语言快速开发一个IP归属地查询接口服务,有需要的小伙伴可以了解下... 目录功能目标技术栈项目结构核心代码(main.go)使用方法扩展功能总结在日常开发中,IP 地址归属地查询是一个常见需求:

基于 Cursor 开发 Spring Boot 项目详细攻略

《基于Cursor开发SpringBoot项目详细攻略》Cursor是集成GPT4、Claude3.5等LLM的VSCode类AI编程工具,支持SpringBoot项目开发全流程,涵盖环境配... 目录cursor是什么?基于 Cursor 开发 Spring Boot 项目完整指南1. 环境准备2. 创建

SpringBoot 多环境开发实战(从配置、管理与控制)

《SpringBoot多环境开发实战(从配置、管理与控制)》本文详解SpringBoot多环境配置,涵盖单文件YAML、多文件模式、MavenProfile分组及激活策略,通过优先级控制灵活切换环境... 目录一、多环境开发基础(单文件 YAML 版)(一)配置原理与优势(二)实操示例二、多环境开发多文件版

使用docker搭建嵌入式Linux开发环境

《使用docker搭建嵌入式Linux开发环境》本文主要介绍了使用docker搭建嵌入式Linux开发环境,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录1、前言2、安装docker3、编写容器管理脚本4、创建容器1、前言在日常开发全志、rk等不同

Python实战之SEO优化自动化工具开发指南

《Python实战之SEO优化自动化工具开发指南》在数字化营销时代,搜索引擎优化(SEO)已成为网站获取流量的重要手段,本文将带您使用Python开发一套完整的SEO自动化工具,需要的可以了解下... 目录前言项目概述技术栈选择核心模块实现1. 关键词研究模块2. 网站技术seo检测模块3. 内容优化分析模

基于Java开发一个极简版敏感词检测工具

《基于Java开发一个极简版敏感词检测工具》这篇文章主要为大家详细介绍了如何基于Java开发一个极简版敏感词检测工具,文中的示例代码简洁易懂,感兴趣的小伙伴可以跟随小编一起学习一下... 目录你是否还在为敏感词检测头疼一、极简版Java敏感词检测工具的3大核心优势1.1 优势1:DFA算法驱动,效率提升10

Python开发简易网络服务器的示例详解(新手入门)

《Python开发简易网络服务器的示例详解(新手入门)》网络服务器是互联网基础设施的核心组件,它本质上是一个持续运行的程序,负责监听特定端口,本文将使用Python开发一个简单的网络服务器,感兴趣的小... 目录网络服务器基础概念python内置服务器模块1. HTTP服务器模块2. Socket服务器模块