[UIKit] UIResponder
UIResponder
이벤트에 응답하고 처리하기 위한 추상적인 인터페이스 Class
- UIResponder는 UIKit의 핵심 클래스로, 사용자 이벤트(터치, 모션, 리모트 컨트롤 등) 처리하는 객체들의 Base Class
- UIView, UIViewController, UIApplication 등 대부분의 UI 컴포넌트가 UIResponder를 상속 받음
- Responder Chain을 통해 이벤트가 전파되는 구조를 관리
이벤트 처리
UIResponder에서는 4가지 유형의 이벤트를 처리할 수 있습니다.
- Touch
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
// 터치 시작 시 동작
}
- Motion
override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
if motion == .motionShake {
// 흔들기 감지
}
}
- Remote Control
- Presses Events(ex. Keybord input)
Responder Chain
앱은 Responder Object를 통해 이벤트를 수신하고 처리합니다. Responder Object는 UIResponder Class의 인스턴스이며, 주로 UIView, UIViewController, UIApplication과 같은 서브클래스가 일반적으로 사용됩니다. Responder는 Event를 직접 받아서 처리하거나 다른 Responder에게 전달할 책임이 있습니다. 앱이 이벤트를 수신하면 UIKit은 해당 이벤트를 첫 번째 Responder(First Responder)로 지정된 가장 적합한 객체로 자동 전달합니다.
처리되지 않은 이벤트는 활성된 Responder Chain을 따라 Responder간에 전달됩니다. Responder Chain은 앱의 Responder Object들이 동적으로 구성하는 경로로, 이벤트가 처리될 때까지 계층 구조를 따라 이동합니다.
이벤트의 First responder 결정
UIKit은 해당 이벤트 유형에 따라 Object를 이벤트에 대한 First Responder로 지정합니다.
Event type | First responder |
Touch events | 터치가 발생한 시점 |
Press evnets | 포커스가 있는 객체 |
Shake-motion events | 사용자(또는 UIKit)가 지정하는 객체 |
Remote-control events | 사용자(또는 UIKit)가 지정하는 객체 |
Editing menu messages | 사용자(또는 UIKit)가 지정하는 객체 |
Touch event가 포함된 responder 결정
UIKit은 View 기반 hit-testing을 사용하여 터치 이벤트가 발생하는 위치를 확인합니다. 구체적으로 UIKit은 터치 위치를 View 계층 구조의 View 객체 경계와 비교합니다. UIView의 hitTest(_: with:) 메서드는 지정된 터치를 포함하는 가장 깊은 하위 View를 찾아 View 계층 구조를 탐색하며, 결과가 Touch Evnet의 First responder가 됩니다.
터치 위치가 View 경계 밖에 있는 경우, hitTest(_:with:) 메서드는 해당 View와 모든 하위 View에서 무시됩니다.
결과적으로 view의 clipsToBounds 프로퍼티가 true면, 해당 View의 경계 밖에 있는 하위 View는 터치를 포함하고 있더라도 return 되지 않습니다.
Touch가 발생하면 UIKit은 UITouch 객체를 생성하여 View와 연결합니다. Touch 위치 또는 다른 매개변수가 변경되면 UIKit은 동일한 UITouch 객체를 새 정보로 업데이트합니다. 변경되지 않는 유일한 속성은 View입니다.(터치 위치가 기존 View 밖으로 이동하더라도 터치 View 값은 변경되지 않습니다.)
Responder Chain 변경(Next Responder)
Responder Chain은 Responder Object의 next 속성을 override하여 수정할 수 있습니다. 이 속성에서 return 되는 Object가 next responder가 됩니다. 많은 UIKit Class가 이미 이 속성을 재정의하고 특정 객체를 return 하도록 구현되어 있습니다.
1. UIView
- View가 ViewController의 rootView인 경우 : next Responder는 해당 ViewController
- 그렇지 않은 경우 : next Responder는 View의 SuperView
2. UIViewController
- ViewController의 View가 Window의 rootView인 경우 : next Responder는 UIWindow 객체
- ViewController가 다른 ViewController에 의해 present된 경우 : next Responder는 present를 시작한 ViewController(presenting view controller)
3. UIWindow
- Next Responder는 UIApplication 객체
4. UIApplication
- Next Responder는 AppDelegate
※ 단, Delegate는 UIResponder의 인스턴스여야 하고, View, ViewController, 앱 객체 자체가 아니여야함
출처
UIResponder | Apple Developer Documentation
An abstract interface for responding to and handling events.
developer.apple.com
Using responders and the responder chain to handle events | Apple Developer Documentation
Learn how to handle events that propagate through your app.
developer.apple.com
'iOS > UIKit' 카테고리의 다른 글
UIModalPresentationStyle (0) | 2024.11.17 |
---|---|
[iOS] UITextField PlaceHolder (0) | 2024.03.24 |
[iOS] UICollectionViewCompositionalLayout (0) | 2024.03.09 |
[iOS] UIScrollView (1) | 2024.03.09 |
[iOS] @IBInspectable (0) | 2024.01.06 |