Sobes.tech
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:

  1. Event reception: Hardware (touch controller) registers the touch and generates an event.
  2. Event processing in IOKit: The device driver in IOKit receives information about the touch (coordinates, pressure, etc.).
  3. Sending event to SpringBoard: IOKit passes the event to the system process SpringBoard.
  4. Event routing: SpringBoard determines the active application and sends the event via IPC (Inter-Process Communication) to that application.
  5. Event reception in UIKit/SwiftUI: The event is received by the framework (UIKit or SwiftUI) within the application.
  6. Adding event to queue: The event is added to the application's event processing queue.
  7. Application receives the event: The main application loop (Run Loop) retrieves the event from the queue.
  8. Event processing through UIResponder: The event is passed to a UIEvent object. UIKit begins the event delivery process, usually starting with the UIWindow object.
  9. hitTest check: The system determines the topmost interactive UIView (or its SwiftUI equivalent) under the touch point, using the hitTest(_:with:) method (in UIKit). This method recursively traverses the view hierarchy, checking if the point is within bounds and if the view is userInteractionEnabled.
  10. 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 if hitTest returns nil), the event is passed to the next responder in the chain, usually the superview or view controller.
  11. 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:)).
  12. Executing logic: The gesture handler or touch method executes user-defined logic (e.g., UI state change, navigation, action execution).
  13. 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)
}
What happens in the system when we press the screen… - sobes.tech