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

  1. 2025.01.23 Swift Gesture(스와이프, 멀티스와이프)-Swipe 이벤트 따라하기

스위프트 Swipe제스쳐 이벤트 따라하기

코디는 이벤트를 정의하고, 이벤트 실행시 수행할 함수를 작성하면 됩니다.

1. 먼저 아래와 같이 이벤트를 정의하고 등록합니다.(addGestureRecognizer)

        let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGuesture(_:)))
        swipeUp.direction = UISwipeGestureRecognizer.Direction.up
        self.view.addGestureRecognizer(swipeUp)

2. 이벤트를 정의할 때, 명시한 #selector에 들어가는 함수를 정의합니다.(이벤트 수행할 내용 정의)

@objc func respondToSwipeGuesture(_ gesture : UIGestureRecognizer) {
        if let swipeGuesture = gesture as? UISwipeGestureRecognizer {
            imgViewUp.image = imgUp[0]
            imgViewDown.image = imgDown[0]
            imgViewRight.image = imgRight[0]
            imgViewLeft.image = imgLeft[0]
            
            switch swipeGuesture.direction {
            case UISwipeGestureRecognizer.Direction.up :
                imgViewUp.image = imgUp[1]
            case UISwipeGestureRecognizer.Direction.down :
                imgViewDown.image = imgDown[1]
            case UISwipeGestureRecognizer.Direction.right :
                imgViewRight.image = imgRight[1]
            case UISwipeGestureRecognizer.Direction.left :
                imgViewLeft.image = imgLeft[1]
            default :
                break
            }
            
            
        }
    }

3. 테스트 

멀티 터치로 변경하려면, let numOfTouches = 2 를 선언한 후, 이벤트 등록할때 numberOfTouchesRequired의 속성에 대입하면 됩니다. iOS시뮬레이터에서 테스트 할 경우, 'option' 키를 누르면 멀티 터치가 가능합니다. 다만, 두개의 터치가 기본값으로 반대 방향으로 이동하므로, 'option'키와 'shift' 키를 동시에 누르면서 이동하면 제대로 멀티 터치 - Swipe 테스트를 할 수 있습니다.

전체 소스입니다. 

// Swipe를 구현하고, 멀티터치를 인지할 수있음
// 싱글터치와 멀티터치를 구분하려면, 이소스 말고, 다음의 멀티터치 소스 확인 필요


import UIKit

class ViewController: UIViewController {
    
    /* 멀티터치를 스와이프를 하려면, numberOfTouchesRequired 속성이 필요한데,
     이속석에 numOfTouches = 2 의 값을 대입해야 합니다.
     시뮬레이터에서 'option' 키를 누르고 테스트하면, 두개의 동그라미가 반대로 향하므로
     option키와 shift 키를 동시에 누르면서 움직이면, 멀티 터치가 됩니다.*/
    
    let numOfTouches = 2

    @IBOutlet weak var imgViewUp: UIImageView!
    @IBOutlet weak var imgViewDown: UIImageView!
    @IBOutlet weak var imgViewLeft: UIImageView!
    @IBOutlet weak var imgViewRight: UIImageView!
    
    
    var imgLeft = [UIImage] ()
    var imgRight = [UIImage] ()
    var imgUp = [UIImage] ()
    var imgDown = [UIImage] ()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        imgUp.append(UIImage(named: "arrow.up.png")!)
        imgDown.append(UIImage(named: "arrow.down.png")!)
        imgRight.append(UIImage(named: "arrow.right.png")!)
        imgLeft.append(UIImage(named: "arrow.left.png")!)
        
        imgUp.append(UIImage(named: "arrow.up.black.png")!)
        imgDown.append(UIImage(named: "arrow.down.black.png")!)
        imgRight.append(UIImage(named: "arrow.right.black.png")!)
        imgLeft.append(UIImage(named: "arrow.left.black.png")!)
        
        imgViewUp.image = imgUp[0]
        imgViewDown.image = imgDown[0]
        imgViewRight.image = imgRight[0]
        imgViewLeft.image = imgLeft[0]
        
        let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGuesture(_:)))
        swipeUp.direction = UISwipeGestureRecognizer.Direction.up
        // multi-touch swipe
        swipeUp.numberOfTouchesRequired = numOfTouches
        self.view.addGestureRecognizer(swipeUp)
        
        let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGuesture(_:)))
        swipeDown.direction = UISwipeGestureRecognizer.Direction.down
        // multi-touch swipe
        swipeDown.numberOfTouchesRequired = numOfTouches
        self.view.addGestureRecognizer(swipeDown)
        
        let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGuesture(_:)))
        swipeRight.direction = UISwipeGestureRecognizer.Direction.right
        // multi-touch swipe
        swipeRight.numberOfTouchesRequired = numOfTouches
        self.view.addGestureRecognizer(swipeRight)
        
        let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGuesture(_:)))
        swipeLeft.direction = UISwipeGestureRecognizer.Direction.left
        // multi-touch swipe
        swipeLeft.numberOfTouchesRequired = numOfTouches
        self.view.addGestureRecognizer(swipeLeft)
         
    }
    
    @objc func respondToSwipeGuesture(_ gesture : UIGestureRecognizer) {
        if let swipeGuesture = gesture as? UISwipeGestureRecognizer {
            imgViewUp.image = imgUp[0]
            imgViewDown.image = imgDown[0]
            imgViewRight.image = imgRight[0]
            imgViewLeft.image = imgLeft[0]
            
            switch swipeGuesture.direction {
            case UISwipeGestureRecognizer.Direction.up :
                imgViewUp.image = imgUp[1]
            case UISwipeGestureRecognizer.Direction.down :
                imgViewDown.image = imgDown[1]
            case UISwipeGestureRecognizer.Direction.right :
                imgViewRight.image = imgRight[1]
            case UISwipeGestureRecognizer.Direction.left :
                imgViewLeft.image = imgLeft[1]
            default :
                break
            }
            
            
        }
    }


}

 

5. 멀티 터치와 싱글 터치 구분

위의 예제는 numberOfTouchesRequired에 값을 2를 대입하여, 싱글 또는 멀치 터치를 인식하는 것이라면,  
아래의 소스는 멀티 터치에 대한 이벤트, 싱글터치에 대한 이벤트를 각각 등록하고 정의한 예입니다. 

// 멀티터치와 싱글터치를 구분할 수 있는 Swipe 예제.

import UIKit

class ViewController: UIViewController {
    
    /* 멀티터치를 스와이프를 하려면, numberOfTouchesRequired 속성이 필요한데,
     이속석에 numOfTouches = 2 의 값을 대입해야 합니다.
     시뮬레이터에서 'option' 키를 누르고 테스트하면, 두개의 동그라미가 반대로 향하므로
     option키와 shift 키를 동시에 누르면서 움직이면, 멀티 터치가 됩니다.*/
    
    let numOfTouches = 2

    @IBOutlet weak var imgViewUp: UIImageView!
    @IBOutlet weak var imgViewDown: UIImageView!
    @IBOutlet weak var imgViewLeft: UIImageView!
    @IBOutlet weak var imgViewRight: UIImageView!
    
    
    var imgLeft = [UIImage] ()
    var imgRight = [UIImage] ()
    var imgUp = [UIImage] ()
    var imgDown = [UIImage] ()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        imgUp.append(UIImage(named: "arrow.up.png")!)
        imgDown.append(UIImage(named: "arrow.down.png")!)
        imgRight.append(UIImage(named: "arrow.right.png")!)
        imgLeft.append(UIImage(named: "arrow.left.png")!)
        
        imgUp.append(UIImage(named: "arrow.up.black.png")!)
        imgDown.append(UIImage(named: "arrow.down.black.png")!)
        imgRight.append(UIImage(named: "arrow.right.black.png")!)
        imgLeft.append(UIImage(named: "arrow.left.black.png")!)
        
        imgUp.append(UIImage(named: "arrow.up.green.png")!)
        imgDown.append(UIImage(named: "arrow.down.green.png")!)
        imgRight.append(UIImage(named: "arrow.right.green.png")!)
        imgLeft.append(UIImage(named: "arrow.left.green.png")!)
        
        imgViewUp.image = imgUp[0]
        imgViewDown.image = imgDown[0]
        imgViewRight.image = imgRight[0]
        imgViewLeft.image = imgLeft[0]
        
        let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGuesture(_:)))
        swipeUp.direction = UISwipeGestureRecognizer.Direction.up
        self.view.addGestureRecognizer(swipeUp)
        
        let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGuesture(_:)))
        swipeDown.direction = UISwipeGestureRecognizer.Direction.down
        self.view.addGestureRecognizer(swipeDown)
        
        let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGuesture(_:)))
        swipeRight.direction = UISwipeGestureRecognizer.Direction.right
        self.view.addGestureRecognizer(swipeRight)
        
        let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGuesture(_:)))
        swipeLeft.direction = UISwipeGestureRecognizer.Direction.left
        self.view.addGestureRecognizer(swipeLeft)
        
        
        
        let swipeUpMulti = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGuestureMulti(_:)))
        swipeUpMulti.direction = UISwipeGestureRecognizer.Direction.up
        swipeUpMulti.numberOfTouchesRequired = numOfTouches
        self.view.addGestureRecognizer(swipeUpMulti)
        
        let swipeDownMulti = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGuestureMulti(_:)))
        swipeDownMulti.direction = UISwipeGestureRecognizer.Direction.down
        swipeDownMulti.numberOfTouchesRequired = numOfTouches
        self.view.addGestureRecognizer(swipeDownMulti)
        
        let swipeRightMulti = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGuestureMulti(_:)))
        swipeRightMulti.direction = UISwipeGestureRecognizer.Direction.right
        swipeRightMulti.numberOfTouchesRequired = numOfTouches
        self.view.addGestureRecognizer(swipeRightMulti)
        
        let swipeLeftMulti = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGuestureMulti(_:)))
        swipeLeftMulti.direction = UISwipeGestureRecognizer.Direction.left
        swipeLeftMulti.numberOfTouchesRequired = numOfTouches
        self.view.addGestureRecognizer(swipeLeftMulti)
    }
    
    @objc func respondToSwipeGuesture(_ gesture : UIGestureRecognizer) {
        if let swipeGuesture = gesture as? UISwipeGestureRecognizer {
            imgViewUp.image = imgUp[0]
            imgViewDown.image = imgDown[0]
            imgViewRight.image = imgRight[0]
            imgViewLeft.image = imgLeft[0]
            
            switch swipeGuesture.direction {
            case UISwipeGestureRecognizer.Direction.up :
                imgViewUp.image = imgUp[1]
            case UISwipeGestureRecognizer.Direction.down :
                imgViewDown.image = imgDown[1]
            case UISwipeGestureRecognizer.Direction.right :
                imgViewRight.image = imgRight[1]
            case UISwipeGestureRecognizer.Direction.left :
                imgViewLeft.image = imgLeft[1]
            default :
                break
            }
            
            
        }
    }
    
    @objc func respondToSwipeGuestureMulti(_ gesture : UIGestureRecognizer) {
        if let swipeGuesture = gesture as? UISwipeGestureRecognizer {
            imgViewUp.image = imgUp[0]
            imgViewDown.image = imgDown[0]
            imgViewRight.image = imgRight[0]
            imgViewLeft.image = imgLeft[0]
            
            switch swipeGuesture.direction {
            case UISwipeGestureRecognizer.Direction.up :
                imgViewUp.image = imgUp[2]
            case UISwipeGestureRecognizer.Direction.down :
                imgViewDown.image = imgDown[2]
            case UISwipeGestureRecognizer.Direction.right :
                imgViewRight.image = imgRight[2]
            case UISwipeGestureRecognizer.Direction.left :
                imgViewLeft.image = imgLeft[2]
            default :
                break
            }
            
            
        }
    }


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