iPhone应用程序响应设备方向更改

时间:2013-09-04 07:36:11

标签: iphone ios objective-c xcode opengl-es-1.1

设备如何知道设备方向已更改,然后开始响应它?在使用opengl的简单应用程序中,当设备方向改变时,两个函数一起工作以在渲染引擎中对此事件做出反应:UpdateAnimation,OnRotate 我的问题是,它们是如何触发的?

感谢您的帮助, 亚辛

//////////////////////////////////////示例代码

class RenderingEngine1 : public IRenderingEngine {
public:
RenderingEngine1();
void Initialize(int width, int height);
void Render() const;
void UpdateAnimation(float timeStep);
void OnRotate(DeviceOrientation newOrientation);
private:
vector<Vertex> m_cone;
vector<Vertex> m_disk;
Animation m_animation;
GLuint m_framebuffer;
GLuint m_colorRenderbuffer;
GLuint m_depthRenderbuffer;
};


void RenderingEngine1::OnRotate(DeviceOrientation orientation)
{
vec3 direction;
switch (orientation) {
case DeviceOrientationUnknown:
case DeviceOrientationPortrait:
direction = vec3(0, 1, 0);
break;
case DeviceOrientationPortraitUpsideDown:
direction = vec3(0, -1, 0);
break;
case DeviceOrientationFaceDown:
direction = vec3(0, 0, -1);
break;
case DeviceOrientationFaceUp:
direction = vec3(0, 0, 1);
break;
case DeviceOrientationLandscapeLeft:
direction = vec3(+1, 0, 0);
break;
case DeviceOrientationLandscapeRight:
direction = vec3(-1, 0, 0);
break;
}
m_animation.Elapsed = 0;
m_animation.Start = m_animation.Current = m_animation.End;
m_animation.End = Quaternion::CreateFromVectors(vec3(0, 1, 0), direction);
}


void RenderingEngine1::UpdateAnimation(float timeStep)
{
if (m_animation.Current == m_animation.End)
return;
m_animation.Elapsed += timeStep;
if (m_animation.Elapsed >= AnimationDuration) {
m_animation.Current = m_animation.End;
} else {
float mu = m_animation.Elapsed / AnimationDuration;
m_animation.Current = m_animation.Start.Slerp(mu, m_animation.End);
}
}

3 个答案:

答案 0 :(得分:1)

此处的其他答案指的是界面方向更改,而我相信您询问设备方向更改(可能会或可能不会影响界面方向)

应用中的相关类实例需要启动设备方向通知:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

然后,只要设备方向发生变化,就需要将自己设置为接收通知:

[[NSNotificationCenter defaultCenter] 
            addObserver:self 
               selector:@selector(didRotate:) 
                   name:UIDeviceOrientationDidChangeNotification 
                 object:nil];

selector指示的方法中,它可以检查当前的方向:

- (void)didRotate:(NSNotification*)notification { 
     UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
     ...

}

类实例还需要在不再需要它时停止设备方向通知,并将其自身作为观察者移除(- dealloc通常是执行此操作的好地方)

[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] removeObserver:self];

答案 1 :(得分:0)

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{

     //Do what you want here

}

当设备改变方向时会调用此方法。

答案 2 :(得分:0)

发件人: How to get default LandscapeLeft Orientation in iOS5?

调用以下方法,您要检查orientation.happy编码的状态: - )

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [self CheckForViewForOrientation:toInterfaceOrientation];
}

- (void) CheckForViewForOrientation:(UIInterfaceOrientation)orientation
{
    if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        //portrait view
    }
    else if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
    {
        //landscape view
    }
}