如上面GIF所示,自定义动画在Swiper切换到第一个子组件时表现异常。切换到其余子组件时效果符合预期。
目前定位是滑动第一个子组件到过半时,偏移量offset失效,缩放scale异常。
下面为Swiper主要代码
Swiper(this.swiperController){
ForEach(new Array(5).fill(0).map((item: number, i: number) => i), (item: number) => {
SwiperItem({
index: item,
curIndex: this.curIndex,
total: this.data.length,
windowWidth: this.windowWidth,
customScale: this.scaleList[item],
customOffset: this.translateList[item],
// visible: item === 0 ? Visibility.None : Visibility.Visible
})
})
}
.displayCount(1, false)
.loop(true)
.index(this.curIndex)
.cachedCount(5)
// .itemSpace(16)
.indicator(false)
.prevMargin(32)
.nextMargin(32)
.onChange((val: number) => {
this.curIndex = val
})
.onAnimationEnd(() => {
this.marginRight = 0
})
.onContentDidScroll((selectedIndex: number, index: number, position: number, mainAxisLength: number) => {
hilog.info(0x0000, TAG, 'selectedIdx: %{public}d, index: %{public}d, position: %{public}d, mainAxisLength: %{public}d',
selectedIndex, index, position, mainAxisLength)
let pre = this.getPre(selectedIndex)
let nxt = this.getNxt(selectedIndex)
// hilog.info(0x0000, TAG, 'pre: %{public}d nxt: %{public}d', pre, nxt)
if(index === selectedIndex){
this.scaleList[index] = this.MIN_SCALE + (1 - this.MIN_SCALE) * (1 - Math.abs(position / 1))
}else if(index === pre){
this.scaleList[index] = this.MIN_SCALE + (1 - this.MIN_SCALE) * Math.max(0, 1 - Math.abs(position / 1))
this.translateList[selectedIndex] = -this.offsetX * Math.max(0, 1 - Math.abs(position / 1))
this.translateList[index] = (this.offsetX) * Math.max(0, Math.abs((position / 1)))
if(position > -0.5 && index === 0){
// this.translateList[index] = - this.offsetX
// this.marginRight = this.offsetX
hilog.info(0x0000, TAG, 'change margin right')
}
// 更新上上一个卡片的偏移量
if(position > -0.5){
this.translateList[this.getPre(index)] = this.offsetX
}else {
this.reset(this.getPre(index))
}
}else if(index === nxt){
this.scaleList[index] = this.MIN_SCALE + (1 - this.MIN_SCALE) * Math.max(0, 1 - Math.abs(position / 1))
this.translateList[selectedIndex] = this.offsetX * Math.max(0, 1 - Math.abs(position / 1))
this.translateList[index] = (-this.offsetX) * Math.max(0, Math.abs((position / 1)))
// 更新下下一个卡片的偏移量
if(position < 0.5){
this.translateList[this.getNxt(index)] = -this.offsetX
}else {
this.reset(this.getNxt(index))
}
}
})
SwiperItem子组件
@Component
export struct SwiperItem{
@Require @Prop index: number = 0
@Prop curIndex: number = 0
@Require @Prop total: number = 5
@Prop windowWidth: number = 375
@Prop customScale: number = 1
@Prop customOffset: number = 24
@Prop marginRight: number = 0
@Prop visible: Visibility = Visibility.Visible
private offsetX: number = 24
getOffsetX(): number{
if(this.index === this.curIndex) return 0
if((this.index + 1) % this.total === this.curIndex) return this.offsetX
return -this.offsetX
}
build() {
Column(){
Image($r('app.media.privilege_swiper_item_test'))
.width('100%')
.height(174)
.objectFit(ImageFit.Cover)
Text(`权益组合包${this.index}`)
.fontColor(Color.Black)
.fontSize(16)
.fontFamily($r('app.string.base_font_bold'))
.width('100%')
.height(98)
}
.visibility(this.visible)
.width(this.windowWidth)
.height(272)
.scale({
x: this.customScale,
y: this.customScale
})
.offset({
x: this.customOffset,
y: 0
})
}
}