-1

デバイスの向きが変わったことをデバイスがどのように認識し、それに応答し始めるのでしょうか? OpenGL を使用する単純なアプリでは、デバイスの向きが変わると、2 つの関数が連携してレンダリング エンジンのこのイベントに反応します: 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 に答える