繁体   English   中英

如何响应iPhone的加速度计旋转图像?

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

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样本

干杯-

您也可以在视图本身上结合使用CGAffineTransforms。 这些矩阵用于通过旋转或平移来变换视图。 如果愿意,您甚至可以为该过程设置动画。 如果这是您实际上要尝试的操作,我可以提供一些示例代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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