[IOS]TouchEvent터치 이벤트

이정찬·2022년 5월 31일
0

Storyboard개발일지

목록 보기
17/20

터치 이벤트를 가져오는 간단한 예제를 만들어 보았다.

스토리보드 구성

추가 옵션


동시에 터치되는 기능을 확인하기 위해 동시 터치 기능을 활성화 하였다.

코드

import UIKit

class ViewController: UIViewController {

    @IBOutlet var txtMessage: UILabel!
    @IBOutlet var txtTapCount: UILabel!
    @IBOutlet var txtTouchCount: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    
    //터치가 시작될 때 호출
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        touchEvent(touches, messageText: "터치가 되었습니다.")
    }
    
    //터치된 부분이 움직일 때 호출
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        touchEvent(touches, messageText: "터치가 이동중입니다.")
    }
    
    //터치된 부분을 뗴었을 때 호출
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        touchEvent(touches, messageText: "터치가 끝났습니다.")
    }
    
    //터치 이밴트를 처리할 함수
    func touchEvent(_ touches: Set<UITouch>, messageText: String?){
        let touch = touches.first! as UITouch //터치 이벤트 가져옴
        
        txtMessage.text = messageText //현재 발생한 이벤트 종류 출력
        txtTapCount.text = String(touch.tapCount) //터치된 횟수 출력
        txtTouchCount.text = String(touches.count) //터치된 갯수 출력
    }
}

실행 결과

profile
오늘도 조금씩 성장하자

0개의 댓글