【问题标题】:How can I rotate an image in response to the iPhone's accelerometer?如何旋转图像以响应 iPhone 的加速度计?
【发布时间】:2009-06-05 07:49:35
【问题描述】:

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

        }
    }
}

【问题讨论】:

    标签: iphone


    【解决方案1】:

    在 Apple 提供的此代码示例中,他们使用加速度计来调整屏幕中心图像的方向。希望此示例代码将帮助您找到所需的内容。 (您需要访问 Apple 开发者网站才能下载此代码示例)

    oalTouch Sample

    干杯-

    【讨论】:

      【解决方案2】:

      您还可以在视图本身上使用 CGAffineTransforms 的组合。这些矩阵通过旋转或平移来变换视图。如果您愿意,您甚至可以为该过程设置动画。如果这是你真正想要做的,我可以提供一些示例代码。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-08
        • 2018-08-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多