• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Touchscreen Event
2
3
4A touchscreen event refer to a callback event triggered when a finger or stylus is pressed, slides, or is lifted from a component. Touchscreen events include the [click event](#click-event), [drag event](#drag-event), and [touch event](#touch-event).
5
6
7**Figure 1** Touchscreen event principles
8
9
10![en-us_image_0000001562700461](figures/en-us_image_0000001562700461.png)
11
12
13## Click Event
14
15A click event refers to a complete press and lift action performed by using a finger or a stylus. When a click event occurs, the following callback is triggered:
16
17
18
19```ts
20onClick(event: (event?: ClickEvent) => void)
21```
22
23
24The **event** parameter provides the coordinates of the click event relative to the window or component and the event source where the click occurs.
25
26
27  For example, the click event of a button is used to control the display and hiding of an image.
28
29```ts
30@Entry
31@Component
32struct IfElseTransition {
33  @State flag: boolean = true;
34  @State btnMsg: string = 'show';
35
36  build() {
37    Column() {
38      Button(this.btnMsg).width(80).height(30).margin(30)
39        .onClick(() => {
40          if (this.flag) {
41            this.btnMsg = 'hide';
42          } else {
43            this.btnMsg = 'show';
44          }
45          // Click the button to control the display and hiding of the image.
46          this.flag = !this.flag;
47        })
48      if (this.flag) {
49        Image($r('app.media.icon')).width(200).height(200)
50      }
51    }.height('100%').width('100%')
52  }
53}
54```
55
56
57## Drag Event
58
59A drag event is triggered when a user presses and holds a component (>=500 ms) using a finger or stylus and drags the component to the drop target. The process of triggering a drag event is as follows:
60
61
62![en-us_image_0000001562820825](figures/en-us_image_0000001562820825.png)
63
64
65Whether a drag event can be triggered depends on the distance of long-pressing and dragging with the finger or stylus on the screen. The drag event is triggered when this distance reaches 5 vp. ArkUI supports intra-application and cross-application drag events.
66
67
68The drag event provides the following [APIs](../reference/arkui-ts/ts-universal-events-drag-drop.md).
69
70
71| API                                    | Description                                      |
72| ---------------------------------------- | ---------------------------------------- |
73| onDragStart(event: (event?: DragEvent, extraParams?: string) => CustomBuilder \| DragItemInfo) | Triggered when dragging starts. Currently, only custom **pixelmap** objects and custom components are supported.          |
74| onDragEnter(event: (event?: DragEvent, extraParams?: string) => void) | Triggered when the dragged item enters a valid drop target. **DragEvent** indicates the position where the drag occurs. **extraParmas** indicates the additional information about the drag event.|
75| onDragLeave(event: (event?: DragEvent, extraParams?: string) => void) | Triggered when the dragged item leaves a valid drop target. **DragEvent** indicates the position where the drag occurs. **extraParmas** indicates the additional information about the drag event.|
76| onDragMove(event: (event?: DragEvent, extraParams?: string) => void) | Triggered when the dragged item moves in a valid drop target. **DragEvent** indicates the position where the drag occurs. **extraParmas** indicates the additional information about the drag event.|
77| onDrop(event: (event?: DragEvent, extraParams?: string) => void) | Triggered when the dragged item is dropped on a valid drop target. **DragEvent** indicates the position where the drag occurs. **extraParmas** indicates the additional information about the drag event.|
78
79
80The following is an example of dragging a component out of a window in cross-window dragging:
81
82
83
84```ts
85import image from '@ohos.multimedia.image';
86
87@Entry
88@Component
89struct Index {
90  @State text: string = ''
91  @State bool1: boolean = false
92  @State bool2: boolean = false
93  @State visible: Visibility = Visibility.Visible
94  @State pixelMap: PixelMap = undefined
95  private pixelMapReader = undefined
96
97  aboutToAppear() {
98    console.info('begin to create pixmap has info message: ')
99    this.createPixelMap()
100  }
101
102  createPixelMap() {
103    let color = new ArrayBuffer(4 * 96 * 96);
104    var buffer = new Uint8Array(color);
105    for (var i = 0; i < buffer.length; i++) {
106      buffer[i] = (i + 1) % 255;
107    }
108    let opts = {
109      alphaType: 0,
110      editable: true,
111      pixelFormat: 4,
112      scaleMode: 1,
113      size: { height: 96, width: 96 }
114    }
115    const promise = image.createPixelMap(color, opts);
116    promise.then((data) => {
117      console.info('create pixmap has info message: ' + JSON.stringify(data))
118      this.pixelMap = data;
119      this.pixelMapReader = data;
120    })
121  }
122
123  @Builder pixelMapBuilder() {
124    Text('drag item')
125      .width('100%')
126      .height(100)
127      .fontSize(16)
128      .textAlign(TextAlign.Center)
129      .borderRadius(10)
130      .backgroundColor(0xFFFFFF)
131  }
132
133  build() {
134    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
135      Text('App1')
136        .width('40%')
137        .height(80)
138        .fontSize(20)
139        .margin(30)
140        .textAlign(TextAlign.Center)
141        .backgroundColor(Color.Pink)
142        .visibility(Visibility.Visible)
143
144      Text('Across Window Drag This')
145        .width('80%')
146        .height(80)
147        .fontSize(16)
148        .margin(30)
149        .textAlign(TextAlign.Center)
150        .backgroundColor(Color.Pink)
151        .visibility(this.visible)
152        .onDragStart(() => {                    // Triggered when cross-window dragging starts.
153          console.info('Text onDrag start')
154          this.bool1 = true
155          this.text = 'TextDrag'
156          return { pixelMap: this.pixelMapReader, extraInfo: 'custom extra info.' }
157        })
158        .onDrop((event: DragEvent, extraParams: string) => {
159          console.info('Text onDragDrop,  ')
160          this.visible = Visibility.None                    // Make the source invisible after the dragging is complete.
161        })
162    }
163
164    .width('100%')
165    .height('100%')
166  }
167}
168```
169
170
171The following is an example of dragging a component into a window in cross-window dragging:
172
173
174
175```ts
176
177@Entry
178@Component
179struct Index {
180  @State number: string[] = ['drag here']
181  @State text: string = ''
182  @State bool1: boolean = false
183  @State bool2: boolean = false
184  @State visible: Visibility = Visibility.Visible
185  @State visible2: Visibility = Visibility.None
186  scroller: Scroller = new Scroller()
187
188  build() {
189    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
190      Text('App2')
191        .width('40%')
192        .height(80)
193        .fontSize(20)
194        .margin(30)
195        .textAlign(TextAlign.Center)
196        .backgroundColor(Color.Pink)
197        .visibility(Visibility.Visible)
198
199      List({ space: 20, initialIndex: 0 }) {
200        ForEach(this.number, (item) => {
201          ListItem() {
202            Text('' + item)
203              .width('100%')
204              .height(80)
205              .fontSize(16)
206              .borderRadius(10)
207              .textAlign(TextAlign.Center)
208              .backgroundColor(0xFFFFFF)
209          }
210        }, item => item)
211
212        ListItem() {
213          Text('Across Window Drag This')
214            .width('80%')
215            .height(80)
216            .fontSize(16)
217            .margin(30)
218            .textAlign(TextAlign.Center)
219            .backgroundColor(Color.Pink)
220            .visibility(this.visible2)
221        }
222      }
223      .height('50%')
224      .width('90%')
225      .border({ width: 1 })
226      .divider({ strokeWidth: 2, color: 0xFFFFFF, startMargin: 20, endMargin: 20 })
227      .onDragEnter((event: DragEvent, extraParams: string) => {                         // Drag the component into the window.
228        console.info('List onDragEnter, ' + extraParams)
229      })
230      .onDragMove((event: DragEvent, extraParams: string) => {                          // Move the component during dragging.
231        console.info('List onDragMove, ' + extraParams)
232      })
233      .onDragLeave((event: DragEvent, extraParams: string) => {                         // Drag the component out of the window.
234        console.info('List onDragLeave, ' + extraParams)
235      })
236      .onDrop((event: DragEvent, extraParams: string) => {                              // Release the component.
237        console.info('List onDragDrop, ' + extraParams)
238        this.visible2 = Visibility.Visible                                              // Make the dragged object visible.
239      })
240    }
241    .width('100%')
242    .height('100%')
243  }
244}
245```
246
247
248## Touch Event
249
250When a finger or stylus touches a component, a touch event corresponding to the action is triggered, such as a press (Down), slide (Move), or lift (Up) event.
251
252
253```ts
254onTouch(event: (event?: TouchEvent) => void)
255```
256
257- If **event.type** is **TouchType.Down**, the finger or stylus is pressed.
258
259- If** event.type** is **TouchType.Up**, the finger or stylus is lifted.
260
261- If **event.type** is **TouchType.Move**, the finger or stylus is pressed and moved.
262
263The touch event may be triggered by multiple fingers at the same time. Information such as the location of the finger that triggers the event, unique identifier of the finger, finger information changed, and an input device source may be obtained by using the **event** parameter.
264
265
266```ts
267// xxx.ets
268@Entry
269@Component
270struct TouchExample {
271  @State text: string = '';
272  @State eventType: string = '';
273
274  build() {
275    Column() {
276      Button('Touch').height(40).width(100)
277        .onTouch((event: TouchEvent) => {
278          if (event.type === TouchType.Down) {
279            this.eventType = 'Down';
280          }
281          if (event.type === TouchType.Up) {
282            this.eventType = 'Up';
283          }
284          if (event.type === TouchType.Move) {
285            this.eventType = 'Move';
286          }
287          this.text = 'TouchType:' + this.eventType + '\nDistance between touch point and touch element:\nx: '
288          + event.touches[0].x + '\n' + 'y: ' + event.touches[0].y + '\nComponent globalPos:('
289          + event.target.area.globalPosition.x + ',' + event.target.area.globalPosition.y + ')\nwidth:'
290          + event.target.area.width + '\nheight:' + event.target.area.height
291        })
292      Button('Touch').height(50).width(200).margin(20)
293        .onTouch((event: TouchEvent) => {
294          if (event.type === TouchType.Down) {
295            this.eventType = 'Down';
296          }
297          if (event.type === TouchType.Up) {
298            this.eventType = 'Up';
299          }
300          if (event.type === TouchType.Move) {
301            this.eventType = 'Move';
302          }
303          this.text = 'TouchType:' + this.eventType + '\nDistance between touch point and touch element:\nx: '
304          + event.touches[0].x + '\n' + 'y: ' + event.touches[0].y + '\nComponent globalPos:('
305          + event.target.area.globalPosition.x + ',' + event.target.area.globalPosition.y + ')\nwidth:'
306          + event.target.area.width + '\nheight:' + event.target.area.height
307        })
308      Text(this.text)
309    }.width('100%').padding(30)
310  }
311}
312```
313
314
315![en-us_image_0000001511900468](figures/en-us_image_0000001511900468.gif)
316