-1

下面是代码:

if ([motionManager isAccelerometerAvailable] == YES) {
    motionManager.deviceMotionUpdateInterval = 1.0 / 100.0;
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];

    [motionManager startAccelerometerUpdatesToQueue:queue withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
                    [self performSelector:@selector(exchangeCard)
                               withObject:nil
                               afterDelay:0];

    }];

我发现没有在块上调用选择器。所以我的问题是如何让 performSelector 在块上调用函数

4

1 回答 1

2

通常不需要使用startAccelerometerUpdatesToQueue:,除非你知道你在用线程做什么,否则你当然不应该使用它。在我看来你不像!使用运动管理器的方法是启动它,然后反复询问它的更新(您可以使用重复的 NSTimer 进行设置)。

self.motman = [CMMotionManager new];
if (!self.motman.accelerometerAvailable) {
    NSLog(@"oh well");
    return;
}
self.motman.accelerometerUpdateInterval = // whatever
[self.motman startAccelerometerUpdates];
NSTimeInterval t = self.motman.accelerometerUpdateInterval * 10;
self.timer =
    [NSTimer
        scheduledTimerWithTimeInterval:t
        target:self selector:@selector(poll:) userInfo:nil repeats:YES];

所以 nowpoll:会被重复调用,你可以做任何你想做的事情:

- (void) poll: (id) dummy {
    // ask self.motman for current values here; for example:
    CMAccelerometerData* dat = self.motman.accelerometerData;
    // now do something with that info
}

有关如何使用运动管理器获取加速度值的实际代码和完整说明,请参阅我的书:http ://www.aeth.com/iOSBook/ch35.html#_raw_acceleration

于 2013-04-14T02:48:19.920 回答