Multiple UIAccelerometer delegates

Sometimes there is a need to listen to the acceleration events of your device and sometimes there is need to listen multiple times to this events. The only trade-off is, that you can set only one delegate to the shared instance of UIAccelerometer.

For rare situations, multiple objects or controllers need to listen to acceleration events in parallel we need to provide a simple wrapper implementation which acts as an acceleration delegate and notifies subscribed observers. Such a wrapper I implemented on my own and you can find it at my github repository.

To use it just import Accelerometer.h and use the sharedInstance to add and remove new delegates. The following examples shows you that:

//Create observers which implement UIAccelerometerDelegate protocol
ObserverOne *one = [[[ObserverOne alloc] init] autorelease];
ObserverTwo *two = [[[ObserverTwo alloc] init] autorelease];
 
//Subscribe observers to acceleration events
[[Accelerometer sharedInstance] addDelegate:one];
[[Accelerometer sharedInstance] addDelegate:two];
 
... // some code goes here
 
//Unsubscribe observer from acceleration events
[[Accelerometer sharedInstance] removeDelegate:one];

The drawback of the current implementation is, that you can’t subscribe to different update intervals. For the moment each observer will be notified at the same interval.

Cheers,
Andreas

0 Responses to “Multiple UIAccelerometer delegates”


  • No Comments

Leave a Reply