如何根据iPhone的加速度计旋转图像?

hai,我使用加速度计通过UIInterfaceOrientationLandscapeRight等设备方向旋转uiimageview.如果我不使用设备方向,(如果我把
iphone放在桌子上,加速计必须在特定角度工作.设备在UIInterfaceOrientationPortrait)我该怎么办?我通过该图像视图的中心旋转图像,但我无法理解它旋转的角度,它是否基于中心(作为原点)旋转? (我想要原点的图形表示)如果我想在特定的角度停止旋转,我该怎么办?代码:任何人都可以帮助我……?

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

        }
    }
}

最佳答案 在Apple提供的代码示例中,他们使用加速计来调整屏幕中心的图像方向.希望这个示例代码可以帮助您找到所需内容. (您需要访问Apple开发人员站点才能下载此代码示例)

oalTouch Sample

干杯-

点赞