我正在监视我的应用程序中的Pitch and Roll值,我想监视它是否在一秒钟内改变5度,然后运行一个进程。目前我的代码如下所示:
CMAttitude *attitude;
CMDeviceMotion *motion = scoringManage.deviceMotion;
attitude = motion.attitude;
basePitch = degrees(attitude.pitch);
baseRoll = degrees(attitude.roll);
if((pitchfloat >= basePitch+5) || (pitchfloat <= basePitch-5)) {
}
if((rollfloat >= baseRoll+5) || (rollfloat <= baseRoll-5)) {
}
这称为:
yprTime = [NSTimer scheduledTimerWithTimeInterval:(1.0/1.0) target:self selector:@selector(yprscore) userInfo:nil repeats:YES];
此过程每秒都会与我的计时器一起运行,但是当值更改时,它将多次运行该循环。
问题是if语句在那一秒内运行的次数太多了20次。
答案 0 :(得分:1)
如果您只想在值更改后运行它,则需要设置BOOL。在你的if pitch&gt;中5语句设置另一个如果检查BOOL
if((pitchfloat >= basePitch+5) || (pitchfloat <= basePitch-5)) {
if (firstTimeBOOLCheckisTrue == NO) {
firstTimeBOOLCheckisTrue = YES;
[self doSomething];
}
}
(void *)doSomething{
if (imReadytoCheckPitchAgain == YES){
firstTimeBOOLCheckisTrue = NO;
}