HarmonyOS鸿蒙Next滑动解锁和倒计时按钮示例代码

HarmonyOS鸿蒙Next滑动解锁和倒计时按钮示例代码 介绍
本示例实现了简单的滑动解锁按钮和倒计时结束按钮,能够实现滑动解锁,长按2秒结束或者按原设定时间结束。

滑动解锁和倒计时结束按钮源码链接

效果预览

图片名称

使用说明

运行项目前,请执行 ohpm install @ohos/hamock,下载hamock依赖

实现思路

  1. 滑动解锁依靠手势操作以及移动过程中的坐标计算更新来完成
    SlideButton

  2. 按钮长按的加速结束时间的效果,也是依靠手势操作判断
    LongPressButton


更多关于HarmonyOS鸿蒙Next滑动解锁和倒计时按钮示例代码的实战教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于HarmonyOS鸿蒙Next滑动解锁和倒计时按钮示例代码的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,实现滑动解锁和倒计时按钮的功能可以通过以下示例代码来完成。该代码使用了ArkUI框架,主要依赖@ohos.agp.components@ohos.agp.animation模块。

import { Component, State, Builder, View, Text, Button, Slider, Stack, Column, Row } from '@ohos.agp.components';
import { Animator, AnimatorOptions } from '@ohos.agp.animation';

@Builder
function createSlider() {
  Slider({
    min: 0,
    max: 100,
    value: 0,
    style: { width: '80%', height: 20 },
    onSlideEnd: (value) => {
      if (value >= 80) {
        console.log('Unlocked');
        // 解锁后的操作
      }
    }
  });
}

@Builder
function createCountdownButton() {
  let count = 10;
  const timer = setInterval(() => {
    count--;
    if (count <= 0) {
      clearInterval(timer);
      console.log('Countdown finished');
      // 倒计时结束后的操作
    }
  }, 1000);

  Button({ type: ButtonType.Normal, stateEffect: true })
    .width(150)
    .height(50)
    .fontSize(20)
    .onClick(() => {
      console.log('Button clicked');
      // 按钮点击后的操作
    });
}

@Entry
@Component
struct Index {
  @State sliderVisible: boolean = true;
  @State buttonVisible: boolean = true;

  build() {
    Column() {
      if (this.sliderVisible) {
        createSlider();
      }
      if (this.buttonVisible) {
        createCountdownButton();
      }
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
    .alignItems(HorizontalAlign.Center)
  }
}

这段代码实现了一个简单的滑动解锁功能和一个倒计时按钮。滑动解锁通过Slider组件实现,当滑动到指定位置时触发解锁操作。倒计时按钮通过setInterval实现倒计时功能,倒计时结束后执行相应操作。

回到顶部