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