스위프트 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
}
}
}
}
'개발자 넋두리 > 아이폰개발(Swift)' 카테고리의 다른 글
Swift 사진찍기, 동영상 촬영, 라이브러리 가져오기 등.. (0) | 2025.01.24 |
---|---|
Swift 개발하면서.. 자질구레 하게 꿀팁들... (1) | 2025.01.24 |
Swift 탭터치(tap-touch) 카운트 예제 (0) | 2025.01.22 |
Swift 동영상 플레이 예제. (0) | 2025.01.21 |
Swift 오디오, 동영상 플레이 예제 소스 (0) | 2025.01.20 |