HarmonyOS 鸿蒙Next Swiper滑动时,对内部子组件的动画影响
HarmonyOS 鸿蒙Next Swiper滑动时,对内部子组件的动画影响
Swiper(this.swiperContentController) {
LazyForEach(this.pageTitleSource, (item: PregnancyTitleData) => {
…
Image($r(‘app.media.detail_pregnant_icon_schedule’))
.width(30)
.height(30)
.position(this.getFirstPosition())
.translate({ x: this.getThumbXPosition(), y: 0 })
.transition(TransitionEffect.SLIDE.animation({
duration: this.getDuration(),
curve: Curve.EaseOut,
iterations: 1,
playMode: PlayMode.Normal,
})) …
}, (item: PregnancyTitleData) => JSON.stringify(item))
}
上述harmonyOs代码,在Swiper中,懒加载有一组数据,其中子视图Image有滑动效果,为什么在Swiper滑动时,会影响到Image内部的 translate效果?要怎么屏蔽掉外部Swiper滑动对Image动画的影响?
更多关于HarmonyOS 鸿蒙Next Swiper滑动时,对内部子组件的动画影响的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
swiper的滑动不影响图片自身的平移效果,demo请参考:
// xxx.ets
class CommonDataSource<T> implements IDataSource {
private dataArray: T[] = [];
private listeners: DataChangeListener[] = [];
constructor(element: T[]) {
this.dataArray = element;
}
public getData(index: number) {
return this.dataArray[index]
}
public totalCount(): number {
return this.dataArray.length;
}
public addData(index: number, data: T[]): void {
this.dataArray = this.dataArray.concat(data);
this.notifyDataAdd(index);
}
public pushData(data: T): void {
this.dataArray.push(data);
this.notifyDataAdd(this.dataArray.length - 1);
}
unregisterDataChangeListener(listener: DataChangeListener): void {
const pos = this.listeners.indexOf(listener);
if (pos >= 0) {
this.listeners.splice(pos, 1);
}
}
registerDataChangeListener(listener: DataChangeListener): void {
if (this.listeners.indexOf(listener) < 0) {
this.listeners.push(listener);
}
}
notifyDataReload(): void {
this.listeners.forEach((listener: DataChangeListener) => {
listener.onDataReloaded();
})
}
notifyDataAdd(index: number): void {
this.listeners.forEach((listener: DataChangeListener) => {
listener.onDataAdd(index);
})
}
notifyDataChange(index: number): void {
this.listeners.forEach((listener: DataChangeListener) => {
listener.onDataChange(index);
})
}
notifyDataDelete(index: number): void {
this.listeners.forEach((listener: DataChangeListener) => {
listener.onDataDelete(index);
})
}
notifyDataMove(from: number, to: number): void {
this.listeners.forEach((listener: DataChangeListener) => {
listener.onDataMove(from, to);
})
}
}
class ProductDataModel {
img: string = ‘’;
comment: string = ‘’;
}
const productData: ProductDataModel[] = [
{
img: ‘common/ic_banner01.png’,
comment: ‘1’,
},
{
img: ‘common/ic_banner02.png’,
comment: ‘1’,
},
{
img: ‘common/ic_banner03.png’,
comment: ‘1’,
},
{
img: ‘common/ic_banner04.png’,
comment: ‘1’,
}
]
@Entry
@Component
struct SwiperExample {
private swiperController: SwiperController = new SwiperController()
build() {
Column({ space: 5 }) {
Swiper(this.swiperController) {
LazyForEach(new CommonDataSource<ProductDataModel>(productData), (item: ProductDataModel) => {
Image($rawfile(item.img))
.width(‘100%’)
.objectFit(ImageFit.Fill)
.translate({ x: 40, y:0 })
.transition(TransitionEffect.OPACITY.animation({ duration: 2000, curve: Curve.Ease }).combine(
TransitionEffect.translate({ x: 40, y: 0 })))
// .transition(TransitionEffect.SLIDE.animation({
// //duration: this.getDuration(),
// curve: Curve.EaseOut,
// iterations: 1,
// playMode: PlayMode.Normal,
// }))
// .position(this.getFirstPosition())
// .translate({ x: this.getThumbXPosition(), y: 0 })
}, (item: ProductDataModel) => JSON.stringify(item))
}
.cachedCount(2)
.index(1)
.autoPlay(true)
.interval(4000)
.loop(true)
.duration(1000)
.itemSpace(0)
.curve(Curve.Linear)
.onChange((index: number) => {
console.info(index.toString())
})
.onGestureSwipe((index: number, extraInfo: SwiperAnimationEvent) => {
console.info("index: " + index)
console.info("current offset: " + extraInfo.currentOffset)
})
.onAnimationStart((index: number, targetIndex: number, extraInfo: SwiperAnimationEvent) => {
console.info("index: " + index)
console.info("targetIndex: " + targetIndex)
console.info("current offset: " + extraInfo.currentOffset)
console.info("target offset: " + extraInfo.targetOffset)
console.info("velocity: " + extraInfo.velocity)
})
.onAnimationEnd((index: number, extraInfo: SwiperAnimationEvent) => {
console.info("index: " + index)
console.info("current offset: " + extraInfo.currentOffset)
})
}.width(‘100%’)
.margin({ top: 5 })
}
}
更多关于HarmonyOS 鸿蒙Next Swiper滑动时,对内部子组件的动画影响的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙系统中,Next Swiper组件滑动时,对内部子组件的动画影响主要通过动画插值器和滑动事件回调来实现。
HarmonyOS提供了丰富的动画框架,允许开发者自定义动画效果。对于Next Swiper组件,其滑动动作可以触发一系列动画,这些动画应用于内部子组件上。开发者可以通过设置动画插值器(Interpolator)来控制动画的速度变化,比如匀速、加速、减速等。
在滑动事件回调中,可以获取滑动进度、滑动方向等参数,基于这些参数动态调整子组件的属性,如透明度、缩放比例、旋转角度等,从而实现丰富的动画效果。例如,可以使用这些参数来控制子组件的透明度从0到1变化,模拟淡入效果;或者根据滑动进度调整子组件的位置,实现抛物线运动等。
此外,HarmonyOS还支持动画组合和动画序列,允许开发者将多个动画效果组合在一起,按照特定的顺序和时间执行,从而创建复杂的动画场景。
总之,通过合理设置动画插值器、利用滑动事件回调以及动画组合和序列,开发者可以在Next Swiper组件滑动时,对内部子组件实现丰富多样的动画效果。
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html