Encapsulating Begin Ignoring Events and End Ignoring Events

There are times when it’s really handy to stop event processing momentarily so you can perform an update to the UI (e.g. sliding a custom view into place using a timed animation) or you otherwise need to save the user from themselves by momentarily stopping touch events.

I’ve written previously on How to Suspend / Ignore Touch Events, which is as simple as:

// Begin ignoring events
[[UIApplicationsharedApplication] beginIgnoringInteractionEvents];
 
// Stop ignoring events
[[UIApplicationsharedApplication] endIgnoringInteractionEvents];


The problem with the above is that depending on when and where you make these calls, you can end up with the lines sprinkled about inside your code, which can make it troublesome to match one call with the other.

For example, if I make a call to begin ignoring events, and then move to a new view in which I then make a request to stop ignoring events, I now have the begin ignorning call in a different class from the end ignorning call. A big deal? Definitely not, however, when you see one call without the other, it can be both hard to know if you have a matching set, not to mention the questions that may arise when you revisit your code a few months down the road.

I get around this by writing a very simple set of methods for stopping and stopping the ignore events process. Here’s how this looks:

Interface Definition for SomeClass
@interface SomeClass : NSObject 
{
  ...
}
 
+ (void)enableEvents;
+ (void)startIgnoringEvents;
+ (void)stopIgnoringEventsAfter:(int)seconds;

Notice that the three methods shown are class methods. The implementation for SomeClass follows:

Implementation for SomeClass
@implementation SomeClass
 
...
 
+ (void)enableEvents
{
  [[UIApplication sharedApplication] endIgnoringInteractionEvents];
}
 
+ (void)startIgnoringEvents
{
  [[UIApplication sharedApplication] beginIgnoringInteractionEvents];
}
 
+ (void)stopIgnoringEventsAfter:(int)seconds
{
  [self performSelector:@selector(enableEvents) withObject:nil afterDelay:seconds];
}
 
@end
How to Start and Stop Ingoring Events

Now that I have these helper methods, I can keep all references to start and stop ignoring of events in one place, here’s how this might look:

// Stop events momentarily...
[SomeClass startIgnoringEvents];    
 
// This is the activity I don't want interrupted
[dropDownViewController showMsg];
 
// Stop ignoring events
[ShoppingListViewController stopIgnoringEventsAfter:2];

Again, these extra steps are not required, however, keeping related code together can be a good thing.

  1. You could also toss these into a category on UIApplication

  2. This code snippet (thank you) does not seem to work….

    I am using a segmented control, and loading the default value as design time, when i populate the form at runtime, the events for the segment changed are still being fired….

    any ideas on how to stop the events from being fired??

    thanks
    tony

Comments are closed.