- ReactJS 教程
- ReactJS - 首頁
- ReactJS - 簡介
- ReactJS - 路線圖
- ReactJS - 安裝
- ReactJS - 功能
- ReactJS - 優勢與劣勢
- ReactJS - 架構
- ReactJS - 建立 React 應用
- ReactJS - JSX
- ReactJS - 元件
- ReactJS - 巢狀元件
- ReactJS - 使用新建立的元件
- ReactJS - 元件集合
- ReactJS - 樣式
- ReactJS - 屬性 (props)
- ReactJS - 使用屬性建立元件
- ReactJS - props 驗證
- ReactJS - 建構函式
- ReactJS - 元件生命週期
- ReactJS - 事件管理
- ReactJS - 建立一個事件感知元件
- ReactJS - 在 Expense Manager 應用中引入事件
- ReactJS - 狀態管理
- ReactJS - 狀態管理 API
- ReactJS - 無狀態元件
- ReactJS - 使用 React Hooks 進行狀態管理
- ReactJS - 使用 React Hooks 的元件生命週期
- ReactJS - 佈局元件
- ReactJS - 分頁
- ReactJS - Material UI
- ReactJS - Http 客戶端程式設計
- ReactJS - 表單程式設計
- ReactJS - 受控元件
- ReactJS - 非受控元件
- ReactJS - Formik
- ReactJS - 條件渲染
- ReactJS - 列表
- ReactJS - Keys
- ReactJS - 路由
- ReactJS - Redux
- ReactJS - 動畫
- ReactJS - Bootstrap
- ReactJS - Map
- ReactJS - 表格
- ReactJS - 使用 Flux 管理狀態
- ReactJS - 測試
- ReactJS - CLI 命令
- ReactJS - 構建和部署
- ReactJS - 示例
- Hooks
- ReactJS - Hooks 簡介
- ReactJS - 使用 useState
- ReactJS - 使用 useEffect
- ReactJS - 使用 useContext
- ReactJS - 使用 useRef
- ReactJS - 使用 useReducer
- ReactJS - 使用 useCallback
- ReactJS - 使用 useMemo
- ReactJS - 自定義 Hooks
- ReactJS 高階
- ReactJS - 可訪問性
- ReactJS - 程式碼分割
- ReactJS - Context
- ReactJS - 錯誤邊界
- ReactJS - 轉發 Refs
- ReactJS - 片段
- ReactJS - 高階元件
- ReactJS - 與其他庫整合
- ReactJS - 效能最佳化
- ReactJS - Profiler API
- ReactJS - Portals
- ReactJS - 無 ES6 ECMAScript 的 React
- ReactJS - 無 JSX 的 React
- ReactJS - 調和
- ReactJS - Refs 和 DOM
- ReactJS - 渲染 Props
- ReactJS - 靜態型別檢查
- ReactJS - 嚴格模式
- ReactJS - Web Components
- 其他概念
- ReactJS - 日期選擇器
- ReactJS - Helmet
- ReactJS - 內聯樣式
- ReactJS - PropTypes
- ReactJS - BrowserRouter
- ReactJS - DOM
- ReactJS - 走馬燈
- ReactJS - 圖示
- ReactJS - 表單元件
- ReactJS - 參考 API
- ReactJS 有用資源
- ReactJS - 快速指南
- ReactJS - 有用資源
- ReactJS - 討論
ReactJS - 觸控事件處理程式
觸控事件對於增強使用者體驗非常重要,尤其是在智慧手機和平板電腦等觸控裝置上。我們將瞭解幾種觸控事件、它們的型別以及如何在 React 應用中處理它們。
觸控事件是更大的 Web 開發 UI 事件家族的一部分。這些事件允許我們記錄和響應使用者的活動,例如點選、滑動和捏合。
語法
<div
onTouchStart={e => console.log('onTouchStart')}
onTouchMove={e => console.log('onTouchMove')}
onTouchEnd={e => console.log('onTouchEnd')}
onTouchCancel={e => console.log('onTouchCancel')}
/>
引數
e − 這是一個 React 事件物件,我們可以將其與 TouchEvent 屬性一起使用。
TouchEvent 型別
需要注意四種主要的觸控事件型別:
| 序號 | 型別與描述 |
|---|---|
| 1 | touchstart 當用戶將手指放在觸控表面上時,會觸發此事件。 |
| 2 | touchend 當用戶將手指從表面移開或觸控點移出表面邊緣時,會發生 touchend 事件。 |
| 3 | touchmove 當用戶在表面上移動手指時發生此事件。它會持續監控接觸點的移動。 |
| 4 | touchcancel 當觸控點以某種方式被打擾時,會發送此訊息。 |
TouchEvent 屬性
需要注意一些觸控事件屬性:
| 序號 | 屬性與描述 |
|---|---|
| 1 | altKey 顯示在事件發生時是否按下了 Alt 鍵。 |
| 2 | ctrlKey 指示在事件期間是否點選了 Ctrl 鍵。 |
| 3 | changedTouches 顯示已更改觸控的 Touch 物件列表。 |
| 4 | getModifierState(key) 一個返回布林值的函式,顯示是否按下了修飾鍵(例如,Shift、Alt 或 Ctrl)。 |
| 5 | metaKey 顯示在事件期間是否按下了 Meta 鍵。 |
| 6 | shiftKey 指示在事件期間是否點選了 Shift 鍵。 |
| 7 | touches 代表觸控表面上所有當前觸控的 Touch 物件列表。 |
| 8 | targetTouches 顯示目標元素上觸控的 Touch 物件列表。 |
| 9 | detail 顯示用於其他事件特定資訊的整數值。 |
| 10 | view 引用生成事件的 Window 物件。 |
示例
示例 - 簡單觸控應用
我們將建立一個簡單的 React 應用來顯示 TouchEvent 處理程式以及下面的程式碼。這是一個小型 React 應用,它在控制檯中顯示觸控事件:
import React from "react";
class App extends React.Component {
render() {
return (
<div
onTouchStart={(e) => console.log("onTouchStart")}
onTouchMove={(e) => console.log("onTouchMove")}
onTouchEnd={(e) => console.log("onTouchEnd")}
onTouchCancel={(e) => console.log("onTouchCancel")}
style={{
width: "200px",
height: "200px",
backgroundColor: "lightblue",
textAlign: "center",
lineHeight: "200px"
}}
>
TouchEvent
</div>
);
}
}
export default App;
輸出
This code generates the App React component. When one of the four touch events (touchstart, touchmove, touchend, and touchcancel) happens, the message is logged in the console. The component also provides a clickable <div> element with which we can interact on a touch-enabled device.
示例 - 長按應用
給定的 React 應用名為“LongPressApp”,旨在檢測觸控裝置上的長按操作。該應用有一個具有淺藍色背景的 div 元素,其寬度和高度均為 200 畫素。當我們觸控並按住應用至少 1000 毫秒時,它會在控制檯中記錄一條訊息,指出“檢測到長按”。
import React from 'react';
class App extends React.Component {
handleTouchStart = () => {
this.longPressTimer = setTimeout(() => {
console.log('Long press detected');
}, 1000); // Adjust the duration for long press threshold
};
handleTouchEnd = () => {
clearTimeout(this.longPressTimer);
};
render() {
return (
<div
onTouchStart={this.handleTouchStart}
onTouchEnd={this.handleTouchEnd}
style={{
width: '200px',
height: '200px',
backgroundColor: 'lightblue',
textAlign: 'center',
lineHeight: '200px',
}}
>
LongPressApp
</div>
);
}
}
export default App;
輸出
Open the app in a browser. Touch the app and keep your touch held for at least 1000 milliseconds. After the specified duration, we will be able to see the message "Long press detected" logged to the browser's console.
示例 - 捏合縮放應用
Pinch Zoom React 應用旨在檢測捏合手勢的開始和結束,通常用於觸控裝置上的縮放操作。該應用有一個具有淺藍色背景的 div 元素,其寬度和高度均為 200 畫素。當用戶開始捏合手勢時,它會在控制檯中記錄訊息“捏合開始”。類似地,當用戶結束捏合手勢時,它會記錄訊息“捏合結束”。
import React from 'react';
class App extends React.Component {
handleTouchStart = (e) => {
if (e.touches.length === 2) {
console.log('Pinch started');
}
};
handleTouchEnd = (e) => {
if (e.touches.length < 2) {
console.log('Pinch ended');
}
};
render() {
return (
<div
onTouchStart={this.handleTouchStart}
onTouchEnd={this.handleTouchEnd}
style={{
width: '200px',
height: '200px',
backgroundColor: 'lightblue',
textAlign: 'center',
lineHeight: '200px',
}}
>
PinchZoomApp
</div>
);
}
}
export default App;
輸出
Open the app in a browser. Use a touch-enabled device (like smartphone or tablet). Touch the app with two fingers simultaneously to trigger the "Pinch started" message. Release one or both fingers to trigger the "Pinch ended" message.
總結
觸控事件在 React 應用中扮演著重要的角色,用於建立引人入勝和互動式使用者介面,尤其是在觸控裝置上。瞭解各種觸控事件以及如何管理它們,使我們能夠為網站或應用程式使用者提供輕鬆的使用者體驗。