W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
- TapGesture(value?:{count?:number; fingers?:number})
點(diǎn)擊手勢(shì)支持單次點(diǎn)擊和多次點(diǎn)擊,擁有兩個(gè)可選參數(shù):
- // xxx.ets
- @Entry
- @Component
- struct Index {
- @State value: string = "";
- build() {
- Column() {
- Text('Click twice').fontSize(28)
- .gesture(
- // 綁定count為2的TapGesture
- TapGesture({ count: 2 })
- .onAction((event: GestureEvent) => {
- this.value = JSON.stringify(event.fingerList[0]);
- }))
- Text(this.value)
- }
- .height(200)
- .width(250)
- .padding(20)
- .border({ width: 3 })
- .margin(30)
- }
- }
- LongPressGesture(value?:{fingers?:number; repeat?:boolean; duration?:number})
長(zhǎng)按手勢(shì)用于觸發(fā)長(zhǎng)按手勢(shì)事件,觸發(fā)長(zhǎng)按手勢(shì)的最少手指數(shù)量為1,最短長(zhǎng)按事件為500毫秒,擁有三個(gè)可選參數(shù):
以在Text組件上綁定可以重復(fù)觸發(fā)的長(zhǎng)按手勢(shì)為例:
- // xxx.ets
- @Entry
- @Component
- struct Index {
- @State count: number = 0;
- build() {
- Column() {
- Text('LongPress OnAction:' + this.count).fontSize(28)
- .gesture(
- // 綁定可以重復(fù)觸發(fā)的LongPressGesture
- LongPressGesture({ repeat: true })
- .onAction((event: GestureEvent) => {
- if (event.repeat) {
- this.count++;
- }
- })
- .onActionEnd(() => {
- this.count = 0;
- })
- )
- }
- .height(200)
- .width(250)
- .padding(20)
- .border({ width: 3 })
- .margin(30)
- }
- }
- PanGesture(value?:{ fingers?:number; direction?:PanDirection; distance?:number})
拖動(dòng)手勢(shì)用于觸發(fā)拖動(dòng)手勢(shì)事件,滑動(dòng)達(dá)到最小滑動(dòng)距離(默認(rèn)值為5vp)時(shí)拖動(dòng)手勢(shì)識(shí)別成功,擁有三個(gè)可選參數(shù):
以在Text組件上綁定拖動(dòng)手勢(shì)為例,可以通過(guò)在拖動(dòng)手勢(shì)的回調(diào)函數(shù)中修改組件的布局位置信息來(lái)實(shí)現(xiàn)組件的拖動(dòng):
- // xxx.ets
- @Entry
- @Component
- struct Index {
- @State offsetX: number = 0;
- @State offsetY: number = 0;
- @State positionX: number = 0;
- @State positionY: number = 0;
- build() {
- Column() {
- Text('PanGesture Offset:\nX: ' + this.offsetX + '\n' + 'Y: ' + this.offsetY)
- .fontSize(28)
- .height(200)
- .width(300)
- .padding(20)
- .border({ width: 3 })
- // 在組件上綁定布局位置信息
- .translate({ x: this.offsetX, y: this.offsetY, z: 0 })
- .gesture(
- // 綁定拖動(dòng)手勢(shì)
- PanGesture()
- .onActionStart((event: GestureEvent) => {
- console.info('Pan start');
- })
- // 當(dāng)觸發(fā)拖動(dòng)手勢(shì)時(shí),根據(jù)回調(diào)函數(shù)修改組件的布局位置信息
- .onActionUpdate((event: GestureEvent) => {
- this.offsetX = this.positionX + event.offsetX;
- this.offsetY = this.positionY + event.offsetY;
- })
- .onActionEnd(() => {
- this.positionX = this.offsetX;
- this.positionY = this.offsetY;
- })
- )
- }
- .height(200)
- .width(250)
- }
- }
大部分可滑動(dòng)組件,如List、Grid、Scroll、Tab等組件是通過(guò)PanGesture實(shí)現(xiàn)滑動(dòng),在組件內(nèi)部的子組件綁定拖動(dòng)手勢(shì)(PanGesture)或者滑動(dòng)手勢(shì)(SwipeGesture)會(huì)導(dǎo)致手勢(shì)競(jìng)爭(zhēng)。
當(dāng)在子組件綁定PanGesture時(shí),在子組件區(qū)域進(jìn)行滑動(dòng)僅觸發(fā)子組件的PanGesture。如果需要父組件響應(yīng),需要通過(guò)修改手勢(shì)綁定方法或者子組件向父組件傳遞消息進(jìn)行實(shí)現(xiàn),或者通過(guò)修改父子組件的PanGesture參數(shù)distance使得拖動(dòng)更靈敏。當(dāng)子組件綁定SwipeGesture時(shí),由于PanGesture和SwipeGesture觸發(fā)條件不同,需要修改PanGesture和SwipeGesture的參數(shù)以達(dá)到所需效果。
- PinchGesture(value?:{fingers?:number; distance?:number})
捏合手勢(shì)用于觸發(fā)捏合手勢(shì)事件,觸發(fā)捏合手勢(shì)的最少手指數(shù)量為2指,最大為5指,最小識(shí)別距離為5vp,擁有兩個(gè)可選參數(shù):
以在Column組件上綁定三指捏合手勢(shì)為例,可以通過(guò)在捏合手勢(shì)的函數(shù)回調(diào)中獲取縮放比例,實(shí)現(xiàn)對(duì)組件的縮小或放大:
- // xxx.ets
- @Entry
- @Component
- struct Index {
- @State scaleValue: number = 1;
- @State pinchValue: number = 1;
- @State pinchX: number = 0;
- @State pinchY: number = 0;
- build() {
- Column() {
- Column() {
- Text('PinchGesture scale:\n' + this.scaleValue)
- Text('PinchGesture center:\n(' + this.pinchX + ',' + this.pinchY + ')')
- }
- .height(200)
- .width(300)
- .border({ width: 3 })
- .margin({ top: 100 })
- // 在組件上綁定縮放比例,可以通過(guò)修改縮放比例來(lái)實(shí)現(xiàn)組件的縮小或者放大
- .scale({ x: this.scaleValue, y: this.scaleValue, z: 1 })
- .gesture(
- // 在組件上綁定三指觸發(fā)的捏合手勢(shì)
- PinchGesture({ fingers: 3 })
- .onActionStart((event: GestureEvent) => {
- console.info('Pinch start');
- })
- // 當(dāng)捏合手勢(shì)觸發(fā)時(shí),可以通過(guò)回調(diào)函數(shù)獲取縮放比例,從而修改組件的縮放比例
- .onActionUpdate((event: GestureEvent) => {
- this.scaleValue = this.pinchValue * event.scale;
- this.pinchX = event.pinchCenterX;
- this.pinchY = event.pinchCenterY;
- })
- .onActionEnd(() => {
- this.pinchValue = this.scaleValue;
- console.info('Pinch end');
- })
- )
- }
- }
- }
- RotationGesture(value?:{fingers?:number; angle?:number})
旋轉(zhuǎn)手勢(shì)用于觸發(fā)旋轉(zhuǎn)手勢(shì)事件,觸發(fā)旋轉(zhuǎn)手勢(shì)的最少手指數(shù)量為2指,最大為5指,最小改變度數(shù)為1度,擁有兩個(gè)可選參數(shù):
以在Text組件上綁定旋轉(zhuǎn)手勢(shì)實(shí)現(xiàn)組件的旋轉(zhuǎn)為例,可以通過(guò)在旋轉(zhuǎn)手勢(shì)的回調(diào)函數(shù)中獲取旋轉(zhuǎn)角度,從而實(shí)現(xiàn)組件的旋轉(zhuǎn):
- // xxx.ets
- @Entry
- @Component
- struct Index {
- @State angle: number = 0;
- @State rotateValue: number = 0;
- build() {
- Column() {
- Text('RotationGesture angle:' + this.angle).fontSize(28)
- // 在組件上綁定旋轉(zhuǎn)布局,可以通過(guò)修改旋轉(zhuǎn)角度來(lái)實(shí)現(xiàn)組件的旋轉(zhuǎn)
- .rotate({ angle: this.angle })
- .gesture(
- RotationGesture()
- .onActionStart((event: GestureEvent) => {
- console.info('RotationGesture is onActionStart');
- })
- // 當(dāng)旋轉(zhuǎn)手勢(shì)生效時(shí),通過(guò)旋轉(zhuǎn)手勢(shì)的回調(diào)函數(shù)獲取旋轉(zhuǎn)角度,從而修改組件的旋轉(zhuǎn)角度
- .onActionUpdate((event: GestureEvent) => {
- this.angle = this.rotateValue + event.angle;
- console.info('RotationGesture is onActionEnd');
- })
- // 當(dāng)旋轉(zhuǎn)結(jié)束抬手時(shí),固定組件在旋轉(zhuǎn)結(jié)束時(shí)的角度
- .onActionEnd(() => {
- this.rotateValue = this.angle;
- console.info('RotationGesture is onActionEnd');
- })
- .onActionCancel(() => {
- console.info('RotationGesture is onActionCancel');
- })
- )
- }
- .height(200)
- .width(250)
- }
- }
- SwipeGesture(value?:{fingers?:number; direction?:SwipeDirection; speed?:number})
滑動(dòng)手勢(shì)用于觸發(fā)滑動(dòng)事件,當(dāng)滑動(dòng)速度大于100vp/s時(shí)可以識(shí)別成功,擁有三個(gè)可選參數(shù):
以在Column組件上綁定滑動(dòng)手勢(shì)實(shí)現(xiàn)組件的旋轉(zhuǎn)為例:
- // xxx.ets
- @Entry
- @Component
- struct Index {
- @State rotateAngle: number = 0;
- @State speed: number = 1;
- build() {
- Column() {
- Column() {
- Text("SwipeGesture speed\n" + this.speed)
- Text("SwipeGesture angle\n" + this.rotateAngle)
- }
- .border({ width: 3 })
- .width(300)
- .height(200)
- .margin(100)
- // 在Column組件上綁定旋轉(zhuǎn),通過(guò)滑動(dòng)手勢(shì)的滑動(dòng)速度和角度修改旋轉(zhuǎn)的角度
- .rotate({ angle: this.rotateAngle })
- .gesture(
- // 綁定滑動(dòng)手勢(shì)且限制僅在豎直方向滑動(dòng)時(shí)觸發(fā)
- SwipeGesture({ direction: SwipeDirection.Vertical })
- // 當(dāng)滑動(dòng)手勢(shì)觸發(fā)時(shí),獲取滑動(dòng)的速度和角度,實(shí)現(xiàn)對(duì)組件的布局參數(shù)的修改
- .onAction((event: GestureEvent) => {
- this.speed = event.speed;
- this.rotateAngle = event.angle;
- })
- )
- }
- }
- }
當(dāng)SwipeGesture和PanGesture同時(shí)綁定時(shí),若二者是以默認(rèn)方式或者互斥方式進(jìn)行綁定時(shí),會(huì)發(fā)生競(jìng)爭(zhēng)。SwipeGesture的觸發(fā)條件為滑動(dòng)速度達(dá)到100vp/s,PanGesture的觸發(fā)條件為滑動(dòng)距離達(dá)到5vp,先達(dá)到觸發(fā)條件的手勢(shì)觸發(fā)??梢酝ㄟ^(guò)修改SwipeGesture和PanGesture的參數(shù)以達(dá)到不同的效果。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: