ios - iPhone app response to device orientation change -
how device know device orientation has changed, start respond it? in simple app using opengl, when device orientation changes, 2 functions work react event in rendering engine: updateanimation, onrotate question is, how triggered??
thanks 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); } }
the other answers here refer interface orientation changes, whereas believe asking device orientation changes (which may or may not have affect on interface orientation)
a relevant class instance in app need start device orientation notifications:
[[uidevice currentdevice] begingeneratingdeviceorientationnotifications];
then need set receive notifications whenever device orientation changes:
[[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(didrotate:) name:uideviceorientationdidchangenotification object:nil];
in method indicated selector
, can check current orientation thus:
- (void)didrotate:(nsnotification*)notification { uideviceorientation orientation = [uidevice currentdevice].orientation; ... }
the class instance needs stop device orientation notifications when no longer needs them, , remove observer (- dealloc
place this)
[[uidevice currentdevice] endgeneratingdeviceorientationnotifications]; [[nsnotificationcenter defaultcenter] removeobserver:self];
Comments
Post a Comment