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

4

你好,我正在使用加速度计来通过设备方向旋转UIImageView,例如UIInterfaceOrientationLandscapeRight等。如果我不使用设备方向(如果我把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);

        }
    }
}
2个回答

2
在苹果提供的这个代码示例中,他们使用加速度计来调整屏幕中央图像的方向。希望这个示例代码能帮助你找到所需内容。(您需要访问苹果开发者网站以下载此代码示例)
链接:oalTouch 示例 祝好!

0

你也可以在视图本身上使用CGAffineTransforms的组合。这些矩阵通过旋转或移动来对视图进行变换。如果你喜欢,甚至可以动画化此过程。如果这正是你想要做的,我可以提供一些示例代码。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接