如何在全局变量的值发生变化时启动方法?

时间:2012-06-14 14:54:03

标签: ios xcode methods

很抱歉我的问题中没有任何代码! 我想实现一个程序,我想在加速度计x轴读数超过某个阈值时启动陀螺仪更新。当陀螺仪被激活时,我也需要停止加速度计更新。我计划的方法是将加速度计数据转换为全局变量,然后将其与if条件进行比较。如果它满足条件我需要发送信号以启动陀螺更新并停止加速更新?

我可以实现关于加速度和陀螺仪更新的代码,但问题是我不知道当全局变量的值超过阈值时如何启动陀螺仪数据n停止加速度数据?请帮帮我!

2 个答案:

答案 0 :(得分:1)

你可以使用getter和setter来改变变量。在setter中,您可以检查您想要的任何条件。那么你就可以发布关于达到所需阈值的通知。

- (void) setNeededValue:(valueType)newValue;
{
    neededValue = newValue;

    if( neededValue > threshold )
    {
        [[NSNotificationCenter defaultCenter] postNotificationName: someConstStringAsNotificationName object: self];
    }
}

接收通知通过调用

在您想要接收它的任何类中使用此代码
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationHandler) name:someConstStringAsNotificationName object:nil];

答案 1 :(得分:0)

听起来不错,你只需要尝试一下。伪:

if ( globalVariable > thresholdValue ) {
    stopAccelData();
    startGyroData();
}