简体   繁体   English

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

[英]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. hai,我使用加速度计通过UIInterfaceOrientationLandscapeRight等设备方向来旋转uiimageview。 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? 如果我不使用设备方向,(如果我将iphone放在桌子上,加速度计必须以特定的角度工作。设备位于UIInterfaceOrientationPortrait中)我该怎么办? 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. 在Apple提供的此代码示例中,他们使用加速度计来调整图像在屏幕中央的方向。 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) (您需要访问Apple开发人员网站才能下载此代码示例)

oalTouch Sample oalTouch样本

Cheers- 干杯-

You could also use a combination of CGAffineTransforms on the view itself. 您也可以在视图本身上结合使用CGAffineTransforms。 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. 如果这是您实际上要尝试的操作,我可以提供一些示例代码。

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

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