'2025/01/22'에 해당되는 글 1건

  1. 06:56:24 Swift 탭터치(tap-touch) 카운트 예제

탭-터치 이벤트에 대응하는 카운트 예제 코드

 

아래와 같이 3개의 함수를 재정의 하면 됩니다. 탭과 터치의 차이는 연속해서 클릭(터치)하냐?의 차이입니다.

  1. override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)  
  2. override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) 
  3. override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?)  

전체소스는 아래와 같습니다.

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var lblMessage: UILabel!
    @IBOutlet weak var lblTapCount: UILabel!
    @IBOutlet weak var lblTouchCount: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = touches.first! as UITouch
        lblMessage.text = "Touch Began"
        lblTapCount.text = String(touch.tapCount)
        lblTouchCount.text = String(touches.count)
    }
    
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = touches.first! as UITouch
        lblMessage.text = "Touch Moved"
        lblTapCount.text = String(touch.tapCount)
        lblTouchCount.text = String(touches.count)
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = touches.first! as UITouch
        lblMessage.text = "Touch Ended"
        lblTapCount.text = String(touch.tapCount)
        lblTouchCount.text = String(touches.count)
    }


}
Posted by 목표를 가지고 달린다
,