可参考以下代码限制设备左右滑动:@Entry @Component struct secondPage { @State rotateAngle: number = 0; @State speed: number = 0; @State message: string = 'Hello World secondPage'; onBackPress(): boolean | void { return true } build() { Column(){ Button('点击返回上一页') .onClick(()=>{ router.back() }) RelativeContainer() { Text(this.message) .id('HelloWorld') .fontSize(50) .fontWeight(FontWeight.Bold) .alignRules({ center: { anchor: '__container__', align: VerticalAlign.Center }, middle: { anchor: '__container__', align: HorizontalAlign.Center } }) } .gesture( // 绑定滑动手势且限制仅在水平方向滑动时触发 SwipeGesture({ direction: SwipeDirection.Horizontal,fingers:0 }) // 当滑动手势触发时,获取滑动的速度和角度,实现对组件的布局参数的修改 .onAction((event: GestureEvent|undefined) => { if(event){ this.speed = event.speed; this.rotateAngle = event.angle; } }) ) .height('100%') .width('100%') .backgroundColor(Color.Green) } } }
可参考以下代码限制设备左右滑动: