728x90
iOS는 사용자의 터치로 대부분의 동작을 수행합니다.
터치 이벤트를 필두로 드래그, 탭 등 다양한 동작을 수행하는데요.
이러한 이벤트를 사용하는 방법에 대해 알아보도록 하겠습니다.
탭은 모바일 화면을 손가락으로 순간적으로 누르는 것을 의미합니다.
이것은 마치 마우스로 클릭하는 것처럼 느껴집니다.
탭, 터치 이벤트 메서드 작성
touchesBegan, touchesMoved, touchesEnded는 터치 이벤트가 발생했을 때 호출되는 메서드입니다.
해당 메서드들을 override 해서 원하는 동작을 수행하도록 할 수 있습니다.
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first! as UITouch
txtMessage.text = "Touches Began"
txtTapCount.text = String(touch.tapCount)
txtTouchCount.text = String(touches.count)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first! as UITouch
txtMessage.text = "Touches Moved"
txtTapCount.text = String(touch.tapCount)
txtTouchCount.text = String(touches.count)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first! as UITouch
txtMessage.text = "Touches Ended"
txtTapCount.text = String(touch.tapCount)
txtTouchCount.text = String(touches.count)
}
기기를 흔드는 모션
motionEnded 메소드를 이용하면 특정한 모션이 끝났을 때의 동작을 지정할 수 있습니다.
override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
if motion == .motionShake {
imgView.image = nil
}
}
'iOS > 스위프트로 아이폰 앱 만들기' 카테고리의 다른 글
[19장] pinch gesture (0) | 2021.10.20 |
---|---|
[18장] Swipe Gesture (0) | 2021.10.20 |
[16장] Core graphics (0) | 2021.10.19 |
[15장] Camera & Photo Library (0) | 2021.10.19 |
[14장] Video (0) | 2021.10.19 |