免責事項: 使用しようとしている方法は非推奨です。CMMotionManager
代わりに使用してください。でも:
デリゲートを 1 つ設定し (別のクラスにする必要があります)、NSNotificationCenter を使用して情報を他のリスナーに配布します。例:
@interface SharedAccelerometerListener: NSObject <UIAccelerometerDelegate>
@end
@implementation SharedAccelerometerListener
- (id)init
{
if ((self = [super init]))
{
[UIAccelerometer sharedAccelerometer].delegate = self;
[UIAccelerometer sharedAccelerometer].updateInterval = 0.05f;
}
}
- (void)accelerometer:(UIAccelerometer *)acc didAccelerate:(UIAcceleration *)acceleration
{
NSDictionary *info = [NSDictionary dictionaryWithObject:acceleration forKey:@"acceleration"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"AccelerometerDidAccelerate" sender:nil userInfo:info];
}
@end
次に、リスナークラスで:
id accelerationListener = [[SharedAccelerometerListener alloc] init];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didAccelerate:) name:@"AccelerometerDidAccelerate" object:nil];
- (void)didAccelerate:(NSNotification *)notif
{
UIAcceleration *acc = [[notif userInfo] objectForKey:@"acceleration"];
// do whatever you want with the acceleration
}