본문 바로가기

Responder Chain

Gesture Recognizer 예시 코드

> 관련글 https://serena-diary.tistory.com/entry/Responder-Chain

 

> 관련글 https://serena-diary.tistory.com/entry/Touch-Event

import UIKit

class ViewController: UIViewController {
    //MARK: - part1
    @IBOutlet private var directionLabel: UILabel!
    
    //MARK: - part2
    @IBOutlet weak var positionLabel: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()

    //MARK: - Gesture Recognizer
        // 왼쪽 드래그 제스처 추가
        let swipeLeftGesture = UISwipeGestureRecognizer(target: self, action: #selector(leftGestureRecognized))
        swipeLeftGesture.direction = .left
        self.view.addGestureRecognizer(swipeLeftGesture)

        // 오른쪽 드래그 제스처 추가
        let swipeRightGesture = UISwipeGestureRecognizer(target: self, action: #selector(rightGestureRecognized))
        self.view.addGestureRecognizer(swipeRightGesture)

        // 위로 드래그 제스처 추가
        let swipeUpGesture = UISwipeGestureRecognizer(target: self, action: #selector(upGestureRecognized))
        swipeUpGesture.direction = .up
        self.view.addGestureRecognizer(swipeUpGesture)

        // 아래로 드래그 제스처 추가
        let swipeDownGesture = UISwipeGestureRecognizer(target: self, action: #selector(downGestureRecognized))
        swipeDownGesture.direction = .down
        self.view.addGestureRecognizer(swipeDownGesture)
                
    }
    
    @objc private func upGestureRecognized(_ gesture: UISwipeGestureRecognizer) {
        if gesture.state == .ended {
            directionLabel.text = "민트업"
        }
    }

    @objc private func downGestureRecognized(_ gesture: UISwipeGestureRecognizer) {
        if gesture.state == .ended {
            directionLabel.text = "세레나다운"
        }
    }

    @objc private func leftGestureRecognized(_ gesture: UISwipeGestureRecognizer) {
        if gesture.state == .ended {
            directionLabel.text = "이디나로크왼쪽"
        }
    }

    @objc private func rightGestureRecognized(_ gesture: UISwipeGestureRecognizer) {
        if gesture.state == .ended {
            directionLabel.text = "경민오른쪽"
        }
    }

    //MARK: - Touch Event Handling
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let firstTouch = touches.first {
            let touchPoint = firstTouch.location(in: self.view)
            positionLabel.text = String(format: "x: %.2f, y: %.2f", touchPoint.x, touchPoint.y)
        }
        print("touchesBegan")
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let firstTouch = touches.first {
            let touchPoint = firstTouch.location(in: self.view)
            positionLabel.text = String(format: "x: %.2f, y: %.2f", touchPoint.x, touchPoint.y)
        }
        print("touchesMoved")
        
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let firstTouch = touches.first {
            let touchPoint = firstTouch.location(in: self.view)
            positionLabel.text = String(format: "x: %.2f, y: %.2f", touchPoint.x, touchPoint.y)
        }
        print("touchesEnded")
    }

    override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let firstTouch = touches.first {
            let touchPoint = firstTouch.location(in: self.view)
            positionLabel.text = String(format: "x: %.2f, y: %.2f", touchPoint.x, touchPoint.y)
        }
        print("touchesCancelled")
    }
}
728x90

'Responder Chain' 카테고리의 다른 글

UIResponder  (0) 2024.01.26
UIEvent  (0) 2023.11.23
Responder Chain  (0) 2023.11.23
Touch Event  (0) 2023.08.19