Home:ALL Converter>Responding to touch events during view controller presentation animation

Responding to touch events during view controller presentation animation

Ask Time:2019-12-11T02:22:04         Author:wstr

Json Formatter

In my iOS app, I have a Toast view that works much like toasts on Android. When a toast is presented, it is displayed in a small region of the screen for a few seconds after which it is automatically dismissed. Toasts have two main properties:

  1. While a toast is on screen, the user can navigate around the app like they are used to.
  2. A toast is sticky, meaning that it stays on screen for the specified duration no matter what (even when pushing, popping, presenting, and dismissing other view controllers).

To achieve the 'stickiness', toasts are presented in a subclass of UIWindow. More specifically, I call present(toastViewController, animated: true, completion: nil) on the custom window's root view controller, which presents the toast using a custom animation that I have implemented using UIViewPropertyAnimator in conjunction with UIViewControllerTransitioningDelegate and UIViewControllerAnimatedTransitioning. Since the window fills the entire screen, I have overriden its point(inside point: CGPoint, with event: UIEvent?) method to always return false. This propagates touch events to the key window containing the actual interface below the toast.

This works great when toasts are on screen, but (and this is the problem) the UI in the key window is frozen while the toast is animating on and off screen. This means that if the user is scrolling through a table view when the toast dismisses, the UI will hang for around 0.5 seconds.

I have verified that touch events can indeed be propagated to the key window during the animations, but they somehow hit a dead end before they reach the actual UI elements. My guess is that this is related to the animations hogging the main thread, but either way, I would love to hear suggestions on how this could be resolved.

Author:wstr,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/59273262/responding-to-touch-events-during-view-controller-presentation-animation
yy