tags:

views:

1494

answers:

3

hai , i use accelerometer to rotate uiimageview through device orientation like UIInterfaceOrientationLandscapeRight,etc. if i dont use device orientation,(if i put iphone on the table , accelerometer must work in particular angle.The device is in UIInterfaceOrientationPortrait)how can i do it? i rotate the image through center of that imageview ,but i could not understand in which angle it is rotated ,is it rotating based on the center(as origin) ? (i want graphical representation with origin)if i want to stop rotation in particular angle,how can i do it?the code : anyone can help me...?

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration 
{
    rollingX =  (acceleration.x * 0.1) + (rollingX * (1.0 - 0.1));
    rollingY =  (acceleration.y * 0.1) + (rollingY * (1.0 - 0.1));

    float xx = -rollingX; 
    float yy = rollingY;
    float angle = atan2(yy, xx); 
        self.angle += M_PI / 2.0;

    if(self.angle >= -2.25 && self.angle <= -0.25)
    {
     if(deviceOrientation != UIInterfaceOrientationPortrait)
     {
      deviceOrientation = UIInterfaceOrientationPortrait;
      self.updatingIsEnabled =YES;
      self.foamView.center = CGPointMake(center.x,center.y);
     }

    }
    else if(self.angle >= -1.75  && self.angle <= 0.75)
    {

     if(deviceOrientation != UIInterfaceOrientationLandscapeRight)
     {
      deviceOrientation = UIInterfaceOrientationLandscapeRight;
      self.updatingIsEnabled =YES;
     }

    }
    else if(self.angle >= 0.75 && self.angle <= 2.25)
    {
     if(deviceOrientation != UIInterfaceOrientationPortraitUpsideDown)
     {
      deviceOrientation = UIInterfaceOrientationPortraitUpsideDown;
      self.updatingIsEnabled =YES;
      self.foamView.center = CGPointMake(center.x,center.y);
     }
    }
    else if(self.angle <= -2.25 || self.angle >= 2.25)
    {
     if(deviceOrientation != UIInterfaceOrientationLandscapeLeft)
     {
         deviceOrientation = UIInterfaceOrientationLandscapeLeft;
      self.updatingIsEnabled =YES;
      self.foamView.center = CGPointMake(center.x,center.y);

     }
    }
}
+2  A: 

In this code sample provided by Apple they use the accelerometer to adjust the orientation of an image in the center of the screen. Hopefully this sample code will help you find what you need. (You will need access to the Apple developer site to download this code sample)

oalTouch Sample

Cheers-

J.Biard
A: 

You could also use a combination of CGAffineTransforms on the view itself. These matrices act to transform a view by rotation or translations. You can even animate the process if you like. If this is what you're actually trying to do, I can provide some sample code.

Dan Lorenc
A: 

Are you talking about doing this with the iPhone sitting flat on a table? The iPhone sadly won't be able to read "spinning" movements while sitting flat on a tabe.

Maniacdev