제스처를 구현하는 방법은 두 가지가 존재한다.
- Gesture Recognizer
- UIView's Touch Event Handling
UIView's Touch Event Handling
- 터치 이벤트와 관련된 함수들이다.
- touchesBegan
- 터치가 일어날 때 불려지는 함수이다.
- touchesMoved
- 터치를 움직일 때마다 불려지는 함수이다.
- touchesEnded
- 터치가 끝날 때 불려지는 함수이다.
- touchesCancelled
- 위의 3가지의 이벤트가 발생도중에 갑자기 Alert이 발생한 경우 불려지는 함수이다.
아래의 예와 같이 viewcontroller 안에 아무곳에서 override를 하면된다.
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else { return }
let point = touch.location(in: view)
let previousPoint = touch.previousLocation(in: view)
pointX.text = "x: \(point.x)"
pointY.text = "y: \(point.y)"
if point.x > previousPoint.x {
directionLabel.text = "오른쪽"
} else {
directionLabel.text = "왼쪽"
}
}
Gesture Recognizer
제스처
- tap gesture
- long-press gesture
- pan gesture → 손가락으로 쭉 끌기
- swipe gesture
- pinch gesture
- rotation gesture
- 스토리보드에서 추가를 하거나, 아래의 코드와 같이 직접 코드를 통해서 추가를 해줄 수도 있다.
class ViewController: UIViewController {
@IBOutlet weak var swipeDirectionLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
//Swipe Gesture를 감지할 수 있는 UISwipeGestureRecognizer 생성
let rightToLeftSwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(swipe(_:)))
let leftToRightSwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(swipe(_:)))
//SwipeGestureRecognizer의 default dircetion은 오른쪽이다.
rightToLeftSwipeGestureRecognizer.direction = .left
view.addGestureRecognizer(rightToLeftSwipeGestureRecognizer)
view.addGestureRecognizer(leftToRightSwipeGestureRecognizer)
}
@objc func swipe(_ sender: UISwipeGestureRecognizer) {
if (sender.direction == .left) {
swipeDirectionLabel.text = "왼쪽"
}
if (sender.direction == .right) {
swipeDirectionLabel.text = "오른쪽"
}
}
}
그렇다면, touch event와 gesture recognizer 둘이 겹치면 뭐가 더 먼저 불릴까?
- 먼저 touch event가 먼저 불리게된다.
- gesture recognizer의 경우, long-press인지 pan gesture 인지 판단하는 데 시간이 좀 걸리기에!
touchMoved
VS UIpanGestureRecognizer
- 단 아래의 식과 같이 두 개가 한번에 사용이될 경우, 첫번째 터치에서만 touchMoved가 발동이되고, 이후로는 계속 Gesture Recognizer가 발동이된다.
class TouchEventViewController: UIViewController {
@IBOutlet weak var directionLabel: UILabel!
@IBOutlet weak var pointY: UILabel!
@IBOutlet weak var pointX: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(paning(_:)))
view.addGestureRecognizer(panGestureRecognizer)
// Do any additional setup after loading the view.
}
@objc func paning(_ sender: UIPanGestureRecognizer){
print("panning중입니다~")
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else { return }
let point = touch.location(in: view)
let previousPoint = touch.previousLocation(in: view)
pointX.text = "x: \(point.x)"
pointY.text = "y: \(point.y)"
print("toucheMoved가 실행됩니다!")
if point.x > previousPoint.x {
directionLabel.text = "오른쪽"
} else {
directionLabel.text = "왼쪽"
}
}
'iOS' 카테고리의 다른 글
[iOS] FileManager (0) | 2023.06.08 |
---|---|
[iOS] NotificationCenter (0) | 2023.04.13 |
[iOS] 의존성 관리 도구(Package Manager) (0) | 2023.02.21 |
[iOS] TableView에서 Swipe해서 Delete하기 (0) | 2023.02.09 |
[iOS] sizeToFit()과 ToolBar (0) | 2023.02.09 |