HarmonyOS 鸿蒙Next 有没有支持可以左右上下都可以滚动的组件
HarmonyOS 鸿蒙Next 有没有支持可以左右上下都可以滚动的组件
有没有支持可以左右上下都可以滚动的组件 , 要做查看大图的效果
暂时没有同时支持上下左右滚动的组件,查看大图的demo您可以参考
// xxx.ets import router from ‘@ohos.router’
@Entry @Component struct SharedTransitionExample { @State active: boolean = false @State imageNames: Resource[] = [$r(‘app.media.image1’), $r(‘app.media.image2’), $r(‘app.media.image3’)] @StorageLink(‘currentIndex’) currentIndex: number = 0
build() { Row() { ForEach(this.imageNames, (res: Resource, index: number) => { Column() { Image(res) .width(‘100%’) .height(‘100%’) .objectFit(ImageFit.Contain) } .width(100) .height(100) .clip(true) .sharedTransition(‘sharedImage’ + res.id, { duration: 200, curve: Curve.Linear, zIndex: this.currentIndex === index ? 10 : -10 }) .onClick(() => { this.currentIndex = index router.pushUrl({ url: ‘pages/Index’, params: { data: this.imageNames, } }) }) }) } .width(‘100%’) .height(‘100%’) }
pageTransition() { PageTransitionEnter({ duration: 0, curve: Curve.Linear }) .onEnter((type?: RouteType, progress?: number) => { }) // 进场过程中会逐帧触发onEnter回调,入参为动效的归一化进度(0% – 100%) PageTransitionExit({ duration: 0, curve: Curve.Ease }) .onExit((type?: RouteType, progress?: number) => { }) // 退场过程中会逐帧触发onExit回调,入参为动效的归一化进度(0% – 100%) } } 2.Index页面 import window from ‘@ohos.window’; import router from ‘@ohos.router’;
interface Data { data: Resource[]; }
enum Direction { None, Left, Right, }
@Entry @Component struct Index { private swiperController: SwiperController = new SwiperController(); @State imageNames: Resource[] = [] @StorageLink(‘currentIndex’) currentIndex: number = 0 @State screenWidth: number = 0; @State op: number = 0
aboutToAppear() { const data = (router.getParams() as Data) this.imageNames = data.data window.getLastWindow(getContext(this)).then(currentWindow => { let property = currentWindow.getWindowProperties(); this.screenWidth = property.windowRect.width; }) }
build() {
Stack({ alignContent: Alignment.Center }) {
Swiper(this.swiperController) {
ForEach(this.imageNames, (name: Resource, index: number) => {
Column() {
ImageComponent({
image: name,
viewWidth: this.screenWidth,
isCurrent: this.currentIndex === index,
onNeedGoNext: (dire: Direction) => {
if (dire === Direction.Right) {
this.swiperController.showNext()
} else if (dire === Direction.Left) {
this.swiperController.showPrevious()
}
}
}).zIndex(index == this.currentIndex ? 2 : 1)
}
.width(‘100%’)
.height(‘100%’)
.justifyContent(FlexAlign.Center)
})
}
.index(this.currentIndex)
.indicator(false)
.disableSwipe(true)
.itemSpace(10)
.onChange((index: number) => {
this.currentIndex = index
})
}
.width(‘100%’).height(‘100%’)
.backgroundColor(rgba(0,0,0,${this.op})
)
}
pageTransition() { PageTransitionEnter({ duration: 200, curve: Curve.Linear }) .onEnter((type?: RouteType, progress?: number) => { if (progress) { this.op = progress } }) // 进场过程中会逐帧触发onEnter回调,入参为动效的归一化进度(0% – 100 @Component struct ImageComponent { private image: Resource = $r(‘app.media.icon’) private preGeometryScale: number = 1 @State geometryScale: number = 1 private preOffsetX: number = 0 private preOffsetY: number = 0 @State geometryOffsetX: number = 0 @State geometryOffsetY: number = 0 @State imageWidth: number = 0 @State imageHeight: number = 0 @Prop viewWidth: number = 0 @Prop isCurrent: boolean = false private dire: Direction = Direction.None private goNext: boolean = true private pinching: boolean = false private onNeedGoNext: (dire: Direction) => void = () => { }
reset(): Promise<void> | undefined { this.preGeometryScale = 1 this.preOffsetX = 0 this.preOffsetY = 0 this.dire = Direction.None this.goNext = true if (this.geometryScale === 1) return return new Promise<void>(res => { animateTo({ duration: 200, onFinish: res }, () => { this.geometryScale = 1 this.geometryOffsetX = 0 this.geometryOffsetY = 0 }) }) }
build() { Column() { Image(this.image) .onComplete((e) => { this.imageWidth = (e?.width || 0) this.imageHeight = (e?.height || 0) }) .objectFit(ImageFit.Cover) .width(this.imageWidth + ‘px’) .height(this.imageHeight + ‘px’) .scale({ x: this.geometryScale, y: this.geometryScale }) .offset({ x: this.geometryOffsetX, y: this.geometryOffsetY }) .focusable(true) .objectFit(ImageFit.Cover) .autoResize(false) .sharedTransition(‘sharedImage’ + this.image.id, { duration: 200, curve: Curve.Linear, zIndex: this.isCurrent ? 10 : -10 }) } .clip(true) .width(‘100%’) .height(‘100%’) .justifyContent(FlexAlign.Center) .hitTestBehavior(HitTestMode.Default) .parallelGesture GestureGroup(GestureMode.Parallel, PinchGesture({ fingers: 2 }) .onActionStart((event: GestureEvent) => { this.pinching = true this.goNext = false })// 当捏合手势触发时,可以通过回调函数获取缩放比例,从而修改组件的缩放比例 .onActionUpdate((event: GestureEvent) => { const s = this.preGeometryScale * event.scale; this.geometryScale = Math.max(0.6, Math.min(2, s)) }) .onActionEnd(async () => { this.preGeometryScale = this.geometryScale if (this.preGeometryScale < 1) { await this.reset() } this.pinching = false }), PanGesture() .onActionStart((event?: GestureEvent) => { }) .onActionUpdate((event?: GestureEvent) => { let offsetX = this.preOffsetX + (event?.offsetX || 0) let offsetY = this.preOffsetY + (event?.offsetY || 0) if (((this.imageWidth * this.geometryScale - this.viewWidth) / 2 - Math.abs(vp2px(offsetX))) <= 0) { if (!this.pinching) { this.dire = offsetX < 0 ? Direction.Right : Direction.Left } return; } this.goNext = false this.geometryOffsetX = offsetX this.geometryOffsetY = offsetY }) .onActionEnd((event?: GestureEvent) => { if ((this.dire !== Direction.None)) { if (this.goNext) { this.onNeedGoNext(this.dire) this.reset() } this.goNext = true } this.preOffsetX = this.geometryOffsetX this.preOffsetY = this.geometryOffsetY }), ) } }
更多关于HarmonyOS 鸿蒙Next 有没有支持可以左右上下都可以滚动的组件的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
鸿蒙Next系统中,确实提供了支持多方向滚动的组件,但具体组件名称和用法可能因版本和API更新而有所变化。鸿蒙系统在设计上注重灵活性和用户体验,因此其UI框架通常包含丰富的组件库,以满足不同场景下的需求。
对于左右上下均可滚动的组件,鸿蒙系统可能通过组合使用现有的滚动组件来实现,如ScrollView
(支持单一方向滚动)的嵌套使用,或者通过自定义组件来实现更复杂的滚动行为。然而,鸿蒙系统也可能已经内置了支持多方向滚动的组件,具体需参考最新的鸿蒙开发文档或API指南。
在鸿蒙的ArkUI框架中,开发者可以通过声明式或编程式的方式使用这些组件,并根据需要调整滚动行为、滚动速度、边界效果等参数。如果组件库中未直接提供所需的多方向滚动组件,开发者还可以通过继承和扩展现有组件来创建自定义组件,以满足特定需求。
如果你正在开发鸿蒙应用并需要实现多方向滚动功能,建议查阅最新的鸿蒙开发文档,了解组件库中的相关组件及其用法。如果问题依旧没法解决请联系官网客服,官网地址是: