设备如何知道设备方向已更改,然后开始响应?在一个使用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);
}
}
此处的其他答案指的是接口方向的变化,而我相信您询问的是设备方向的变化(可能会也可能不会影响接口方向)
应用程序中的相关类实例需要启动设备定向通知:
[[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];
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
//Do what you want here
}
当设备改变方向时调用此方法。。
来源:如何在iOS5中获得默认的LandscapeLeft Orientation
调用下面的方法来检查方向的状态。快乐编码:-)
- (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
}
}