210607 Mon

Sunny·2021년 7월 3일
0

Today I Learned

목록 보기
71/88

1. 첫 번째 학습 내용: Responders, Responder Chain

**Article

Using Responders and the Responder Chain to Handle Events

Learn how to handle events that propagate through your app.

Overview

Apps receive and handle events using responder objects. A responder object is any instance of the UIResponder class, and common subclasses include UIView, UIViewController, and UIApplication.

Responder의 역할?

일단 raw event data를 받은 다음에

  1. 직접 event를 handle 하든지
  2. 다르 responder object한테 forward 하든지

Responders receive the raw event data and must either handle the event or forward it to another responder object. When your app receives an event, UIKit automatically directs that event to the most appropriate responder object, known as the first responder.

Q. 핸들되지 않은 events들은 어떻게 되냐?

A. active responder chain 안에 있는 responder에서 다른 responder로 패스됨

Q. active responder chain이란 무엇인가?

the dynamic configuration of your app’s responder objects.

Unhandled events are passed from responder to responder in the active responder chain, which is the dynamic configuration of your app’s responder objects. Figure 1 shows the responders in an app whose interface contains a label, a text field, a button, and two background views. The diagram also shows how events move from one responder to the next, following the responder chain.

Responder chains in an app

Q. Responder Chain은 왜 필요할까?

사용자가 어떤 이벤트를 줬을때

그 이벤트를 처리할 수 없는 상황이면

다음의 이벤트를 가져갈 친구는 누구냐?

그 규칙을 세워둔 것!

여기서 처리 못하면

누가 이 이벤트를 수신할거냐?

(공무원 - 책임 소재 돌리기 느낌)

말단이 못해주면

다음 위로 보내고

처리할 사람?
처리할 사람?
폭탄 돌리기 하다

시스템까지 올라감

체인 하면 쇠사슬 느낌인데

그냥 연결고리, 연결 이렇게 받아들일 수 있음

Class
UIResponder

An abstract interface for responding to and handling events.

**Managing the Responder Chain**

var next: UIResponder?

Returns the next responder in the responder chain, or nil if there is no next responder.

Q. iOS 환경에서 사용자의 터치 이벤트를 알아채거나 제어할 수 있는 방법의 종류는?

A. 터치 이벤트를 받았을 때

뷰에서 터치 이벤트를 직접 수신을 하는 메소드를 override 할 것이냐?

혹은 Tap Gesture Recognizer 같은 것으로 따로 또 인식을 할 것이냐?

크게 2가지를 볼 수 있음.

Q. Responder Chain 이라는게 추상적인 개념인지? 명시해줄 수 있는 개념인지?

A. UIResponder라는 클래스를 상속받아서

나 다음의 응답을 할 next responder를 지정할 수 있음.

어느정도 명시화 됐다라고 볼 수 있음

Responder chain 이라는 것 자체는

Gesture recognizer가 있다, touch event를 override 한다 이런거랑은 별개의 느낌

특정 이벤트가 발생했을 때

Responder Chain를 쭉 올라오긴 할텐데

그 이벤트를 수신할 때

여러가지 방법이 있을거임

자주 사용되는 이벤트가 터치이지만

더 많은 종류의 이벤트가 있음

2. 두 번째 학습 내용: touchesBegan(_:with:)

Instance Method
touchesBegan(_:with:)

Tells this object that one or more new touches occurred in a view or window.

Discussion

UIKit calls this method when a new touch is detected in a view or window. Many UIKit classes override this method and use it to handle the corresponding touch events. The default implementation of this method forwards the message up the responder chain.

When creating your own subclasses, call super to forward any events that you do not handle yourself.

For example,

[super touchesBegan:touches withEvent:event];

If you override this method without calling super (a common use pattern), you must also override the other methods for handling touch events, even if your implementations do nothing.

만약에 이 메소드를 오버라이드할 때

super 메서드를 호출하지 않으면

이 메서드 외에 터치를 핸들링하지 않는다 하더라도

터치와 관련된 모든 이벤트를 전부 오버라이드 해주라 라고 써있음

이 의미는 뭐일까?

왜 그렇게 얘기 했을까?

일단은 touchesBegan에서

super.touchesBegan

슈퍼를 찍어줘야 한다는 걸 알고 넘어가기!

First responder = text field 일 때

응답해서 뭔가를 처리할 친구가 생기면

더 이상 체인에 올려보내지 않는다

고것만 일단 알고 있으면 됨

결론) Gesture Recognizer 같은 경우엔

자기가 올려져있는 뷰가

퍼스트 리스폰더의 역할을 할 수 있을때 동작을 하는구나!

다른 친구가 먼저 리스폰더 체인을 끊어버리지 않은 경우에만

Gesture Recognizer가 반응할 수 있다!

3. 세 번째 학습 내용: UIEvent

Class
UIEvent

An object that describes a single user interaction with your app.

  • touch events
  • motion events
  • remote-control events (playing a video or skipping to the next audio track)
  • press events (interactions with a game controller or other device that has physical buttons)

Enumeration
UIEvent.EventType

Specifies the general type of an event

[case touches](https://developer.apple.com/documentation/uikit/uievent/eventtype/touches)

The event is related to touches on the screen.

[case motion](https://developer.apple.com/documentation/uikit/uievent/eventtype/motion)

The event is related to motion of the device, such as when the user shakes it.

[case remoteControl](https://developer.apple.com/documentation/uikit/uievent/eventtype/remotecontrol)

The event is a remote-control event. Remote-control events originate as commands received from a headset or external accessory for the purposes of controlling multimedia on the device.

[case presses](https://developer.apple.com/documentation/uikit/uievent/eventtype/presses)

The event is related to the press of a physical button.

Apple Developer Documentation

Apple Developer Documentation

Apple Developer Documentation

profile
iOS Developer

0개의 댓글