質問

I need to go through a series of steps one by one. In all, I have three steps to go through which are inside of a while loop. Once the three tests are completed, then and only then should the user be exited from the while loop. The catch is that these steps need to be done sequentially, and require the user to do each test in order, if they pass, then move on to the next step.

Here is the relevant code:

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];

            }

        }

    }

I need the user to position the iOS device in each position, and a beep sound is played to indicate a successful test. Once ALL of the three tests have been completed in order, I want the user to be exited from the loop. I figure each time a test has been cleared, I would increment the passCount counter by 1, until we reach 3 which would exit me from the loop. My issue though is how to go through each test, and in order.

役に立ちましたか?

解決

Assuming this isn't running on the main UI thread, remove the while loop, replace each if and else if with a while condition, set the appropriate boolean flags to true when a test passes and you're done.

他のヒント

You could implement that in

deviceDidRotate()

you need a object variable for saving the current progress, or you use an enum like you have done:

int step; 

then in deviceDidRotate you check:

   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 
   }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top