开发者

Apply loop on accelerometer reading?

开发者 https://www.devze.com 2023-01-22 18:20 出处:网络
Actually i want to apply a loop on accelerometer, means i want to start a accelerometer on behalf of loop. if i want to start loop one then want to perform accelerometer reading one time. if loop will

Actually i want to apply a loop on accelerometer, means i want to start a accelerometer on behalf of loop. if i want to start loop one then want to perform accelerometer reading one time. if loop will run twice then want to run accelerometer twice. But it not happens. What should i do to control accelerometer.

Have a quick look on code

in viewdidload

for (int i=0; i<3; i++) 

    {
        NSLog(@"Hellooooooooo",i);
        [[UIAccelerometer sharedAcceleromet开发者_Python百科er] setDelegate:self];
    }

and in accelerometer didAccelerate

{
        float xx = -[acceleration x]; 
    float yy = [acceleration y]; 

} 

I don't know what is going wrong ?

help me if u have some idea about it.

thanks in advance for any help.


set the update interval to something not 0.
And if you don't need the accelerometer anymore set it back to 0 and remove your delegate. If you want 3 measurements you have to change your code, setting the delegate 3 times has no effect.

You could use something like this:

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
    static int count = 0;
    count++;
    float xx = -[acceleration x]; 
    float yy = [acceleration y]; 
    if (count >= 3) {
        [accelerometer setUpdateInterval:0];
        [accelerometer setDelegate:nil];
        count = 0;
    }
    NSLog(@"%f %f", xx, yy);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [[UIAccelerometer sharedAccelerometer] setDelegate:self];
    [[UIAccelerometer sharedAccelerometer] setUpdateInterval:1.0/measurementsPerSecond];
}


I have solved this issue . Just apply a int count variable and assign the count value initialize. Take the value from the user, according to your needs and check out the condition in the accelerometer delegate. Increase the count variable every time and stop the accelerometer at specific count which u want.

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:
                     (UIAcceleration *)acceleration 
{ 
    // Get the current device angle

        float xx = -[acceleration x];

       float yy = [acceleration y]; 

       if(count <= userChoice)
       {
           if(deviceOrientation != UIInterfaceOrientationPortraitUpsideDown)
           {
               // here we will start NStimer to calcualte actual time

                  NSLog(@"Count Before = %d",count);

                  alert.hidden=TRUE;

                  deviceOrientation = UIInterfaceOrientationPortraitUpsideDown;

                  [self startTimer];

                  count++;

                  NSLog(@"Count after = %d",count);
          }
      }

 if (count <= 1) 
 {
     count++;

 }

 else 
 {
      UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"Stop" message:@"Your rounds are ended." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];

      [alert show];

      [alert release];

   }

 }
0

精彩评论

暂无评论...
验证码 换一张
取 消