Middle
What happens in the system when we press the screen?
sobes.tech AI
Answer from AI
When a user taps on an iOS device screen, the following occurs:
- Event reception: Hardware (touch controller) registers the touch and generates an event.
- Event processing in IOKit: The device driver in IOKit receives information about the touch (coordinates, pressure, etc.).
- Sending event to SpringBoard: IOKit passes the event to the system process SpringBoard.
- Event routing: SpringBoard determines the active application and sends the event via IPC (Inter-Process Communication) to that application.
- Event reception in UIKit/SwiftUI: The event is received by the framework (UIKit or SwiftUI) within the application.
- Adding event to queue: The event is added to the application's event processing queue.
- Application receives the event: The main application loop (Run Loop) retrieves the event from the queue.
- Event processing through UIResponder: The event is passed to a
UIEventobject. UIKit begins the event delivery process, usually starting with theUIWindowobject. - hitTest check: The system determines the topmost interactive
UIView(or its SwiftUI equivalent) under the touch point, using thehitTest(_:with:)method (in UIKit). This method recursively traverses the view hierarchy, checking if the point is within bounds and if the view is userInteractionEnabled. - Event delivery: The event is delivered to the found view. If the view does not handle the event (e.g., lacks the appropriate gesture or implements
touchesBegan:withEvent:but does not handle it, or ifhitTestreturns nil), the event is passed to the next responder in the chain, usually the superview or view controller. - Handling gestures or touch methods: If the view has attached gesture recognizers (
UIGestureRecognizer), they receive the event first. If a gesture is recognized, it is handled by the corresponding Objective-C action or Swift function. If gestures do not handle the event or are absent, the event is passed to the view's touch methods (touchesBegan(_:with:),touchesMoved(_:with:),touchesEnded(_:with:),touchesCancelled(_:with:)). - Executing logic: The gesture handler or touch method executes user-defined logic (e.g., UI state change, navigation, action execution).
- UI update: If the logic results in UI changes, the framework requests a redraw (if necessary).
Here is a simplified example of touch handling logic in a button action:
// Inside a ViewController class or another UIResponder
@IBAction func buttonTapped(_ sender: Any) {
// Logic executed when the button is tapped
print("Button tapped!")
}
Example of using touchesBegan (less common for simple taps):
// In a custom UIView subclass
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
// Get the first touch
guard let touch = touches.first else { return }
// Get the touch point in view coordinates
let location = touch.location(in: self)
print("Screen touched at CGPoint: \(location)")
// Calling super is necessary to continue the responder chain
// super.touchesBegan(touches, with: event)
}