简体   繁体   中英

How can I rotate an image in response to the iPhone's accelerometer?

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

        }
    }
}

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-

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM