iPhone app response to device orientation change

527 views Asked by At

How does the device know that the device orientation has changed, then start to respond to it? In a simple app using opengl, when the device orientation changes, two functions work together to react to this event in the rendering engine: UpdateAnimation, OnRotate My question is, how were they triggered??

Thanks for your help, Yassin

////////////////////////////////////// Sample code

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

There are 3 answers

0
Jitendra On
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{

     //Do what you want here

}

this method is called when device changes his orientation..

0
Master Stroke On

From: How to get default LandscapeLeft Orientation in iOS5?

call the below method where you want to check the status of orientation.happy coding :-)

- (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
    }
}
0
foundry On

The other answers here refer to interface orientation changes, whereas I believe you are asking about device orientation changes (which may or may not have an affect on interface orientation)

A relevant class instance in the app would need to start device orientation notifications:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

Then it would need to set itself up to receive notifications whenever the device orientation changes:

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

in the method indicated by selector, it can check the current orientation thus:

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

}

The class instance also needs to stop device orientation notifications when it no longer needs them, and remove itself as an observer (- dealloc is often a good place to do this)

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