如何在while循环中执行连续步骤?

时间:2012-12-05 20:33:08

标签: ios if-statement while-loop uideviceorientation

我需要逐一完成一系列步骤。总而言之,我有三个步骤可以通过while循环。一旦三个测试完成,那么只有那时用户才能退出while循环。问题是这些步骤需要按顺序完成,并且要求用户按顺序进行每个测试,如果它们通过,则继续进行下一步。

以下是相关代码:

int passCount = 0;
    BOOL flatPass = FALSE;
    BOOL landscapePass = FALSE;
    BOOL portraitPass = FALSE;

while (passCount < 3) {


        if (flatPass == FALSE) {

            if (device.orientation == UIDeviceOrientationFaceUp || device.orientation == UIDeviceOrientationFaceDown) {

                [self pushSound];

            }

        }


        else if (landscapePass == FALSE) {

            if (device.orientation == UIDeviceOrientationLandscapeLeft || device.orientation == UIDeviceOrientationLandscapeRight) {

                [self pushSound];

            }

        }


        else if (portraitPass == FALSE) {

            if (device.orientation == UIDeviceOrientationPortrait || device.orientation == UIDeviceOrientationPortraitUpsideDown) {

                [self pushSound];

            }

        }

    }

我需要用户将iOS设备放在每个位置,并播放一声嘟嘟声以表示测试成功。一旦所有三个测试按顺序完成,我希望用户退出循环。我想每次测试都被清除,我会将passCount计数器递增1,直到我们达到3,这将使我退出循环。我的问题是如何按顺序完成每个测试。

2 个答案:

答案 0 :(得分:0)

假设这没有在主UI线程上运行,请删除while循环,用if条件替换每个else ifwhile,设置相应的布尔值当测试通过并且你已经完成时,标志为true。

答案 1 :(得分:0)

你可以在

中实现它
  

deviceDidRotate()

你需要一个对象变量来保存当前的进度,或者像你一样使用枚举:

int step; 

然后在deviceDidRotate中检查:

   if (step == 0 && device.orientation == UIDeviceOrientationFaceUp ) {
     step = 1;
   } else if (step == 1 && device.orientation == UIDeviceOrientationLandscapeLeft) {
     step = 2;
   } else if (step == 2 && device.orientation == UIDeviceOrientationPortrait ) {
     // successful!
     // now do action, reset step? call method 
   }