HarmonyOS 换成model后动画放大实现不了

HarmonyOS
8h前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

listitem内容为object,不能直接用 ==判断当前item,可以改用index确定当前点击的item,修改如下:(其他案例参考:https://gitee.com/harmonyos-cases/cases/tree/master/CommonAppDevelopment/feature/listexchange

import curves from '@ohos.curves';
@Entry
@Component
struct model {
  @State private arr: titleStrModel[] = []
  @State dragItem: titleStrModel = new titleStrModel()
  @State scaleItem: titleStrModel = new titleStrModel()
  @State neighborItem: titleStrModel = new titleStrModel()
  @State selectedIndex:number = -1
  @State neighborScale: number = -1
  private dragRefOffset: number = 0
  @State offsetX: number = 0
  @State offsetY: number = 0
  private ITEM_INTV: number = 120
  aboutToAppear(): void {
    const d = new titleStrModel()
    d.name = 'wewewe'
    const b = new titleStrModel()
    b.name = 'adadad'
    const h = new titleStrModel()
    h.name = 'dfdfdf'
    this.arr.push(d)
    this.arr.push(b)
    this.arr.push(h)
  }

  //根据当前index确定item是否缩放
  scaleSelect(item: titleStrModel):void {
    this.selectedIndex = this.arr.indexOf(item)
    this.arr[this.selectedIndex].scale = 1.05
  }
  itemMove(index: number, newIndex: number): void {
    let tmp = this.arr.splice(index, 1)
    this.arr.splice(newIndex, 0, tmp[0])
  }
  build() {
    Stack() {
      List({ space: 20, initialIndex: 0 }) {
        ForEach(this.arr, (item: titleStrModel) => {
          ListItem() {
            Row() {
              Text('' + item.name)
                .width('100%')
                .height(100)
                .fontSize(16)
                .textAlign(TextAlign.Center)
                .borderRadius(10)
                .backgroundColor(0xFFFFFF)
              Image($r('app.media.app_icon'))
                .width(100)
                .height(100)
            }
            .shadow(this.scaleItem.name == item.name ? { radius: 70, color:Color.Blue, offsetX: 0, offsetY: 0 } :
              { radius: 0, color: Color.Blue, offsetX: 0, offsetY: 0 })
            .animation({ curve: Curve.Sharp, duration: 300 })
          }
          .margin({ left: 12, right: 12 })
          .scale({ x: item.scale, y: item.scale })
          .zIndex(this.dragItem.name == item.name ? 1 : 0)
          .translate(this.dragItem.name == item.name ? { y: this.offsetY } : { y: 0 })
          .gesture(

            //以下组合手势为顺序识别,当长按手势事件未正常触发时则不会触发拖动手势事件
            GestureGroup(GestureMode.Sequence,
              LongPressGesture({ repeat: true })
                .onAction((event?: GestureEvent) => {
                  animateTo({ curve: Curve.Friction, duration: 300 }, () => {
                    this.scaleItem = item
                    this.scaleSelect(item)
                  })
                })
                .onActionEnd(() => {
                  animateTo({ curve: Curve.Friction, duration: 300 }, () => {
                    this.scaleItem = new titleStrModel()
                  })
                }),
              PanGesture({ fingers: 1, direction: null, distance: 0 })
                .onActionStart(() => {
                  this.dragItem = item
                  this.dragRefOffset = 0
                })
                .onActionUpdate((event: GestureEvent) => {
                  this.offsetY = event.offsetY - this.dragRefOffset
                  // console.log('Y:' + this.offsetY.toString())
                  this.neighborItem = new titleStrModel()
                  let index = this.arr.indexOf(item)
                  let curveValue = curves.initCurve(Curve.Sharp)
                  let value: number = 0
                  //根据位移计算相邻项的缩放
                  if (this.offsetY < 0) {
                    value = curveValue.interpolate(-this.offsetY / this.ITEM_INTV)
                    this.neighborItem = this.arr[index-1]
                    this.neighborScale = 1 - value / 20;
                    console.log('neighborScale:' + this.neighborScale.toString())
                  } else if (this.offsetY > 0) {
                    value = curveValue.interpolate(this.offsetY / this.ITEM_INTV)
                    this.neighborItem = this.arr[index+1]
                    this.neighborScale = 1 - value / 20;
                  }
                  //根据位移交换排序
                  if (this.offsetY > this.ITEM_INTV / 2) {
                    animateTo({ curve: curves.interpolatingSpring(0, 1, 400, 38) }, () => {
                      this.offsetY -= this.ITEM_INTV
                      this.dragRefOffset += this.ITEM_INTV
                      this.itemMove(index, index + 1)
                    })
                  } else if (this.offsetY < -this.ITEM_INTV / 2) {
                    animateTo({ curve: curves.interpolatingSpring(0, 1, 400, 38) }, () => {
                      this.offsetY += this.ITEM_INTV
                      this.dragRefOffset -= this.ITEM_INTV
                      this.itemMove(index, index - 1)
                    })
                  }
                })
                .onActionEnd((event: GestureEvent) => {
                  animateTo({ curve: curves.interpolatingSpring(0, 1, 400, 38) }, () => {
                    this.dragItem = new titleStrModel()
                    this.neighborItem = new titleStrModel()
                    //动画结束将缩放还原
                    this.arr[this.arr.indexOf(this.scaleItem)].scale = 1
                  })
                  animateTo({
                    curve: curves.interpolatingSpring(14, 1, 170, 17), delay: 150
                  }, () => {
                    this.scaleItem = new titleStrModel()
                  })
                })
            )
              .onCancel(() => {
                animateTo({ curve: curves.interpolatingSpring(0, 1, 400, 38) }, () => {
                  this.dragItem = new titleStrModel()
                  this.neighborItem = new titleStrModel()
                })
                animateTo({
                  curve: curves.interpolatingSpring(14, 1, 170, 17), delay: 150
                }, () => {
                  this.scaleItem = new titleStrModel()
                })
              })
          )
        })
      }
    }.width('100%').height('100%').backgroundColor(0xDCDCDC).padding({ top: 5 })
  }
}
class titleStrModel {
  name:string = ''
  scale:number = 1 //增加scale属性, 每个item都有对应的scale值
}
分享
微博
QQ
微信
回复
7h前
相关问题
HarmonyOS 如何实现放大缩小的动画
377浏览 • 1回复 待解决
Image组件如何实现双指手势放大
2395浏览 • 1回复 待解决
HarmonyOS 如何实现RippleView动画
311浏览 • 1回复 待解决
HarmonyOS 如何实现WaveView动画
342浏览 • 1回复 待解决
HarmonyOS 如何实现旋转动画
446浏览 • 1回复 待解决
HarmonyOS 如何实现音频声浪动画
665浏览 • 1回复 待解决
HarmonyOS如何实现动态缩放动画
538浏览 • 1回复 待解决
HarmonyOS 点赞动画实现方案
0浏览 • 0回复 待解决
HarmonyOS 怎样实现开屏动画或图片?
369浏览 • 1回复 待解决
API version 8 does not support the Stage model
8026浏览 • 1回复 待解决
属性动画如何实现宽高动画效果
2032浏览 • 1回复 待解决
安装不了HarmonyOs Legacy SDK
8258浏览 • 1回复 待解决
如何应用属性动画实现宽高的动画
403浏览 • 1回复 待解决
如何实现动画转场效果
836浏览 • 1回复 待解决