HarmonyOS 能否提供频道选择UI和长按拖动嵌套滚动这俩个demo。

HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
aquaa

频道选择UI长按拖动:

import curves from '@ohos.curves';

@Entry
@Component
struct Index {
  @State numbers: String[] = ["娱乐", "关注", "热榜", "航天", "动漫", "宠物", "保险", "xx", "汽车", "财经", "体育", "女性", "搞笑", "军事", "有料", "北京", "深度", "xx"]
  scroller: Scroller = new Scroller()
  @State text: string = 'drag'
  @State isShowDelete: boolean = false
  @State isEdit: boolean = false
  @State rotateZ: number = 0;

  private stopJump() {
    animateTo({
      delay: 0,
      tempo: 5,
      duration: 0,
      curve: Curve.Smooth,
      playMode: PlayMode.Normal,
      iterations: 1
    },
      () => {
        this.rotateZ = 0;
      })
  }

  //抖动的动画
  private jumpWithSpeed(speed: number) {
    if (this.isEdit) {
      this.rotateZ = -1;
      animateTo({
        delay: 0,
        tempo: speed,
        duration: 1000,
        curve: Curve.Smooth,
        playMode: PlayMode.Normal,
        iterations: -1
      },
        () => {
          this.rotateZ = 1;
        })
    } else {
      this.stopJump()
    }
  }

  //拖拽过程中展示的样式
  @Builder
  pixelMapBuilder() {
    Column() {
      Text(this.text)
        .fontSize(16)
        .backgroundColor(0xF9CF93)
        .width(80)
        .height(40)
        .textAlign(TextAlign.Center)
        .borderRadius(20)
        .borderWidth(2)
        .borderColor(Color.Orange)
    }
  }

  //改变数组中元素位置
  changeIndex(index1: number, index2: number) {
    this.numbers.splice(index2, 0, this.numbers.splice(index1, 1)[0])
  }

  build() {
    Column({ space: 5 }) {
      Row({ space: 30 }) {
        Text("我的频道")
          .fontSize(20)
        Text(this.isEdit ? "长按拖动调整顺序" : "点击编辑按钮进入编辑")
          .fontColor(Color.Gray)
        Button() {
          Text(this.isEdit ? "完成" : "编辑")
            .fontColor(this.isEdit ? Color.Black : 0xF9CF93)
        }.width(80)
        .height(30)
        .backgroundColor(Color.White)
        .onClick(() => {
          this.isEdit = !this.isEdit
          this.jumpWithSpeed(5)
        })
      }.padding({ left: 15 })

      Grid(this.scroller) {
        ForEach(this.numbers, (day: string, index: number) => {
          GridItem() {
            Stack({ alignContent: Alignment.TopEnd }) {
              Text(day)
                .borderRadius(20)
                .borderWidth(2)
                .borderColor(Color.Orange)
                .fontSize(16)
                .backgroundColor(0xF9CF93)
                .width(80)
                .height(40)
                .textAlign(TextAlign.Center)
                .onTouch((event: TouchEvent) => {
                  if (event.type === TouchType.Down) {
                    this.text = day
                  }
                })
              if (this.isEdit) {
                Image($r('app.media.close'))
                  .width(20)
                  .height(20)
                  .onClick(() => {
                    animateTo({ duration: 300 }, () => {
                      this.numbers.splice(index, 1)
                    })
                    this.stopJump()
                    this.jumpWithSpeed(5)
                  })
              }
            }
            .rotate({
              z: this.rotateZ,
              angle: 0.4,
              centerX: 0.5,
              centerY: 0.5
            })
          }
          .transition({ type: TransitionType.All, translate: { x: 100 }, scale: { x: 1, y: 1 } })
          .padding({ top: 15 })
        })
      }
      .columnsTemplate('1fr 1fr 1fr')
      .columnsGap(10)
      .rowsGap(10)
      .onScrollIndex((first: number) => {
        console.info(first.toString())
      })
      .margin({ top: 5 })
      .width('100%')
      .backgroundColor(0xFAEEE0)
      .height('100%')
      .supportAnimation(true)
      //设置Grid是否进入编辑模式,进入编辑模式可以拖拽Grid组件内部GridItem
      .editMode(this.isEdit)
      //第一次拖拽此事件绑定的组件时,触发回调
      .onItemDragStart((event: ItemDragInfo, itemIndex: number) => {
        //设置拖拽过程中显示的图片
        return this.pixelMapBuilder()
      })
      //绑定此事件的组件可作为拖拽释放目标,当在本组件范围内停止拖拽行为时,触发回调
      //itemIndex为拖拽起始位置,insertIndex为拖拽插入位置
      .onItemDrop((event: ItemDragInfo, itemIndex: number, insertIndex: number, isSuccess: boolean) => {
        //不支持拖拽到已有内容以外的位置
        if (insertIndex < this.numbers.length) {
          this.changeIndex(itemIndex, insertIndex)
          this.stopJump()
          this.jumpWithSpeed(5)
        }
      })
    }.width('100%')
    .margin({ top: 5 })
    .alignItems(HorizontalAlign.End)
  }
}
分享
微博
QQ
微信
回复
2天前
相关问题
HarmonyOS Grid拖动排序长按冲突
551浏览 • 2回复 待解决
HarmonyOS能否提供NFC识别的demo
430浏览 • 1回复 待解决
能否提供关于SM3加密的demo
697浏览 • 1回复 待解决
HarmonyOS 能否提供扫码的demo代码
21浏览 • 1回复 待解决
HarmonyOS 能否提供登录界面样例demo
250浏览 • 1回复 待解决
Image长按拖动如何关闭
2340浏览 • 1回复 待解决
HarmonyOS提供路由跳转Demo
407浏览 • 1回复 待解决
HarmonyOS 嵌套滚动冲突
45浏览 • 1回复 待解决
页面列表嵌套滚动,实现列表吸顶
1302浏览 • 1回复 待解决