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

3 回答 3

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];
于 2013-09-04T08:07:08.883 回答
0
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{

     //Do what you want here

}

当设备改变他的方向时调用这个方法..

于 2013-09-04T07:56:52.427 回答
0

来自:如何在 iOS5 中获取默认的 LandscapeLeft 方向?

调用下面的方法来检查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
    }
}
于 2013-09-04T07:59:20.453 回答