首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

iOS 觸控事件處理:touchesBegan/Moved/Ended/Cancelled 四大回調完整指南

     編輯:程式設計綜合問答

需求:在 UIView 中處理使用者觸控操作,實現按下高亮、滑動追蹤、抬起確認、取消重置等效果。核心是覆寫 UIResponder 的四大觸控事件方法。

四大核心方法

方法觸發時機典型用途
touchesBegan(_:with:)手指**剛接觸**螢幕高亮按鈕、記錄起始座標、啟動計時器
touchesMoved(_:with:)手指**在螢幕上滑動**(高頻率重複呼叫)拖曳視圖、繪圖、滑動選單追蹤
touchesEnded(_:with:)手指**離開螢幕**(正常結束)確認點擊、提交表單、結束拖曳、觸發動作
touchesCancelled(_:with:)系統**中斷觸控**(來電、推播、多工切換、超出視圖範圍)重置高亮、取消進行中操作、清理暫態狀態

完整實作範例:自訂按鈕高亮效果


class CustomButton: UIView {
    private var isHighlighted = false
    
    override func touchesBegan(_ touches: Set, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        isHighlighted = true
        UIView.animate(withDuration: 0.1) {
            self.alpha = 0.6 // 按下變暗
            self.transform = CGAffineTransform(scaleX: 0.95, y: 0.95) // 微縮
        }
    }
    
    override func touchesMoved(_ touches: Set, with event: UIEvent?) {
        super.touchesMoved(touches, with: event)
        // 判斷手指是否移出按鈕範圍
        if let touch = touches.first {
            let location = touch.location(in: self)
            if !bounds.contains(location) {
                // 移出範圍 → 取消高亮
                resetHighlight()
            }
        }
    }
    
    override func touchesEnded(_ touches: Set, with event: UIEvent?) {
        super.touchesEnded(touches, with: event)
        if isHighlighted {
            // 觸發點擊動作
            sendActions(for: .touchUpInside)
        }
        resetHighlight()
    }
    
    override func touchesCancelled(_ touches: Set, with event: UIEvent?) {
        super.touchesCancelled(touches, with: event)
        resetHighlight()
    }
    
    private func resetHighlight() {
        isHighlighted = false
        UIView.animate(withDuration: 0.1) {
            self.alpha = 1.0
            self.transform = .identity
        }
    }
}

關鍵細節

  • 多點觸控:touches 是 Set,單點操作用 touches.first。
  • 座標轉換:touch.location(in: view) 取得相對於指定視圖的座標。
  • 事件傳遞鏈:View → Superview → ... → Window → Application。若不處理,呼叫 super 向上傳遞。
  • touchesCancelled 必須處理:否則高亮狀態殘留、操作邏輯未重置,造成使用者困惑。

現代替代:UIControl / UIGestureRecognizer

標準 UI 控制項請優先使用 UIControl / UIGestureRecognizer;只有極度自訂需求才需覆寫觸控事件。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved
方案適用場景優點
繼承 UIControl按鈕、滑桿、開關等標準控制項內建狀態管理、Target-Action 模式、無障礙支援
UITapGestureRecognizer點擊、雙擊、長按宣告式、可加多個手勢、自動處理取消
UIPanGestureRecognizer拖曳、滑動內建速度/位移計算、邊界限制
覆寫 touches*高度自訂繪圖、遊戲、特殊互動完全控制、最低層級、效能最高