W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
觸屏事件指當手指/手寫筆在組件上按下、滑動、抬起時觸發(fā)的回調(diào)事件。包括點擊事件、拖拽事件和觸摸事件。
圖1 觸摸事件原理
點擊事件是指通過手指或手寫筆做出一次完整的按下和抬起動作。當發(fā)生點擊事件時,會觸發(fā)以下回調(diào)函數(shù):
- onClick(event: (event?: ClickEvent) => void)
event參數(shù)提供點擊事件相對于窗口或組件的坐標位置,以及發(fā)生點擊的事件源。
- @Entry
- @Component
- struct IfElseTransition {
- @State flag: boolean = true;
- @State btnMsg: string = 'show';
- build() {
- Column() {
- Button(this.btnMsg).width(80).height(30).margin(30)
- .onClick(() => {
- if (this.flag) {
- this.btnMsg = 'hide';
- } else {
- this.btnMsg = 'show';
- }
- // 點擊Button控制Image的顯示和消失
- this.flag = !this.flag;
- })
- if (this.flag) {
- Image($r('app.media.icon')).width(200).height(200)
- }
- }.height('100%').width('100%')
- }
- }
拖拽事件指手指/手寫筆長按組件(>=500ms),并拖拽到接收區(qū)域釋放的事件。拖拽事件觸發(fā)流程:
拖拽事件的觸發(fā)通過長按、拖動平移判定,手指平移的距離達到5vp即可觸發(fā)拖拽事件。ArkUI支持應(yīng)用內(nèi)、跨應(yīng)用的拖拽事件。
拖拽事件提供以下接口:
接口名稱 | 描述 |
---|---|
onDragStart(event: (event?: DragEvent, extraParams?: string) => CustomBuilder | DragItemInfo) | 拖拽啟動接口。當前僅支持自定義pixelmap和自定義組件。 |
onDragEnter(event: (event?: DragEvent, extraParams?: string) => void) | 拖拽進入組件接口。DragEvent定義拖拽發(fā)生位置,extraParmas表示用戶自定義信息 |
onDragLeave(event: (event?: DragEvent, extraParams?: string) => void) | 拖拽離開組件接口。DragEvent定義拖拽發(fā)生位置,extraParmas表示拖拽事件額外信息。 |
onDragMove(event: (event?: DragEvent, extraParams?: string) => void) | 拖拽移動接口。DragEvent定義拖拽發(fā)生位置,extraParmas表示拖拽事件額外信息。 |
onDrop(event: (event?: DragEvent, extraParams?: string) => void) | 拖拽釋放組件接口。DragEvent定義拖拽發(fā)生位置,extraParmas表示拖拽事件額外信息。 |
如下是跨窗口拖拽,拖出窗口示例:
- import image from '@ohos.multimedia.image';
- @Entry
- @Component
- struct Index {
- @State visible: Visibility = Visibility.Visible
- private pixelMapReader = undefined
- aboutToAppear() {
- console.info('begin to create pixmap has info message: ')
- this.createPixelMap()
- }
- createPixelMap() {
- let color = new ArrayBuffer(4 * 96 * 96);
- var buffer = new Uint8Array(color);
- for (var i = 0; i < buffer.length; i++) {
- buffer[i] = (i + 1) % 255;
- }
- let opts = {
- alphaType: 0,
- editable: true,
- pixelFormat: 4,
- scaleMode: 1,
- size: { height: 96, width: 96 }
- }
- const promise = image.createPixelMap(color, opts);
- promise.then((data) => {
- console.info('create pixmap has info message: ' + JSON.stringify(data))
- this.pixelMapReader = data;
- })
- }
- @Builder pixelMapBuilder() {
- Text('drag item')
- .width('100%')
- .height(100)
- .fontSize(16)
- .textAlign(TextAlign.Center)
- .borderRadius(10)
- .backgroundColor(0xFFFFFF)
- }
- build() {
- Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
- Text('App1')
- .width('40%')
- .height(80)
- .fontSize(20)
- .margin(30)
- .textAlign(TextAlign.Center)
- .backgroundColor(Color.Pink)
- .visibility(Visibility.Visible)
- Text('Across Window Drag This')
- .width('80%')
- .height(80)
- .fontSize(16)
- .margin(30)
- .textAlign(TextAlign.Center)
- .backgroundColor(Color.Pink)
- .visibility(this.visible)
- .onDragStart(() => { //啟動跨窗口拖拽
- console.info('Text onDrag start')
- return { pixelMap: this.pixelMapReader, extraInfo: 'custom extra info.' }
- })
- .onDrop((event: DragEvent, extraParams: string) => {
- console.info('Text onDragDrop, ')
- this.visible = Visibility.None //拖動結(jié)束后,使源不可見
- })
- }
- .width('100%')
- .height('100%')
- }
- }
跨窗口拖拽,拖入示例:
- @Entry
- @Component
- struct Index {
- @State number: string[] = ['drag here']
- @State text: string = ''
- @State bool1: boolean = false
- @State bool2: boolean = false
- @State visible: Visibility = Visibility.Visible
- @State visible2: Visibility = Visibility.None
- scroller: Scroller = new Scroller()
- build() {
- Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
- Text('App2')
- .width('40%')
- .height(80)
- .fontSize(20)
- .margin(30)
- .textAlign(TextAlign.Center)
- .backgroundColor(Color.Pink)
- .visibility(Visibility.Visible)
- List({ space: 20, initialIndex: 0 }) {
- ForEach(this.number, (item) => {
- ListItem() {
- Text('' + item)
- .width('100%')
- .height(80)
- .fontSize(16)
- .borderRadius(10)
- .textAlign(TextAlign.Center)
- .backgroundColor(0xFFFFFF)
- }
- }, item => item)
- ListItem() {
- Text('Across Window Drag This')
- .width('80%')
- .height(80)
- .fontSize(16)
- .margin(30)
- .textAlign(TextAlign.Center)
- .backgroundColor(Color.Pink)
- .visibility(this.visible2)
- }
- }
- .height('50%')
- .width('90%')
- .border({ width: 1 })
- .divider({ strokeWidth: 2, color: 0xFFFFFF, startMargin: 20, endMargin: 20 })
- .onDragEnter((event: DragEvent, extraParams: string) => { //拖拽進去組件
- console.info('List onDragEnter, ' + extraParams)
- })
- .onDragMove((event: DragEvent, extraParams: string) => { //拖拽時移動
- console.info('List onDragMove, ' + extraParams)
- })
- .onDragLeave((event: DragEvent, extraParams: string) => { //拖拽離開組件
- console.info('List onDragLeave, ' + extraParams)
- })
- .onDrop((event: DragEvent, extraParams: string) => { //釋放組件
- console.info('List onDragDrop, ' + extraParams)
- this.visible2 = Visibility.Visible //拖拽完成使拖入目標可見
- })
- }
- .width('100%')
- .height('100%')
- }
- }
當手指或手寫筆在組件上觸碰時,會觸發(fā)不同動作所對應(yīng)的事件響應(yīng),包括按下(Down)、滑動(Move)、抬起(Up)事件:
- onTouch(event: (event?: TouchEvent) => void)
觸摸事件可以同時多指觸發(fā),通過event參數(shù)可獲取觸發(fā)的手指位置、手指唯一標志、當前發(fā)生變化的手指和輸入的設(shè)備源等信息。
- // xxx.ets
- @Entry
- @Component
- struct TouchExample {
- @State text: string = '';
- @State eventType: string = '';
- build() {
- Column() {
- Button('Touch').height(40).width(100)
- .onTouch((event: TouchEvent) => {
- if (event.type === TouchType.Down) {
- this.eventType = 'Down';
- }
- if (event.type === TouchType.Up) {
- this.eventType = 'Up';
- }
- if (event.type === TouchType.Move) {
- this.eventType = 'Move';
- }
- this.text = 'TouchType:' + this.eventType + '\nDistance between touch point and touch element:\nx: '
- + event.touches[0].x + '\n' + 'y: ' + event.touches[0].y + '\nComponent globalPos:('
- + event.target.area.globalPosition.x + ',' + event.target.area.globalPosition.y + ')\nwidth:'
- + event.target.area.width + '\nheight:' + event.target.area.height
- })
- Button('Touch').height(50).width(200).margin(20)
- .onTouch((event: TouchEvent) => {
- if (event.type === TouchType.Down) {
- this.eventType = 'Down';
- }
- if (event.type === TouchType.Up) {
- this.eventType = 'Up';
- }
- if (event.type === TouchType.Move) {
- this.eventType = 'Move';
- }
- this.text = 'TouchType:' + this.eventType + '\nDistance between touch point and touch element:\nx: '
- + event.touches[0].x + '\n' + 'y: ' + event.touches[0].y + '\nComponent globalPos:('
- + event.target.area.globalPosition.x + ',' + event.target.area.globalPosition.y + ')\nwidth:'
- + event.target.area.width + '\nheight:' + event.target.area.height
- })
- Text(this.text)
- }.width('100%').padding(30)
- }
- }
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: