代码拉取完成,页面将自动刷新
本示例介绍使用ArkUIstack 组件实现多层级轮播图。该场景多用于购物、资讯类应用。
使用说明
1.通过stack和offsetx实现多层级堆叠。源码参考SwiperComponent.ets
Stack() {
LazyForEach(this.swiperDataSource, (item: SwiperData, index: number) => {
Stack({ alignContent: Alignment.BottomStart }) {
Image(item.imageSrc)
.objectFit(ImageFit.Auto)
.width('100%')
.height('100%')
.borderRadius($r('app.string.main_page_top_borderRadius'))
...
3.通过手势控制调用显式动画同时修改数据中间值currentIndex来修改组件zIndex提示组件层级实现动画切换效果。源码参考SwiperComponent.ets
Stack() {
ForEach(this.swiperDataSource, (item: SwiperData, index: number) => {
Stack({ alignContent: Alignment.BottomStart }) {
Image(item.imageSrc)
.objectFit(ImageFit.Auto)
.width('100%')
.height('100%')
.borderRadius($r('app.string.swipercomponent_main_page_top_borderRadius'))
// 轮播图底部蒙层
Stack() {
Column() {
}
.width('100%')
.height('100%')
.backgroundColor(Color.Black)
.opacity(0.3)
.borderRadius({
topLeft: 0,
topRight: 0,
bottomLeft: $r('app.string.swipercomponent_main_page_top_borderRadius'),
bottomRight: $r('app.string.swipercomponent_main_page_top_borderRadius')
})
Text(item.name)
.width('100%')
.height('100%')
.fontSize(16)
.fontColor(Color.White)
.textAlign(TextAlign.Start)
.padding($r('app.string.swipercomponent_main_page_padding5'))
}
.height('17%')
}
.gesture(
PanGesture({ direction: PanDirection.Horizontal })
.onActionStart((event: GestureEvent) => {
this.startAnimation(event.offsetX < 0);
})
)
startAnimation(isLeft: boolean): void {
animateTo({
duration: 300,
}, () => {
let dataLength: number = this.swiperData.length;
let tempIndex: number = isLeft ? this.currentIndex + 1 : this.currentIndex - 1 + dataLength;
this.currentIndex = tempIndex % dataLength;
})
}
不涉及
functionalscenes // har类型
|---model
| |---SwiperData.ets // 轮播数据模型和数据控制器
|---mainpage
| |---FunctionalScenes.ets // 轮播页面
不涉及
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。