通常、ユーザーがデバイスを回転させたときに発生するアニメーション(主にビューのフレームを操作)をwillAnimateToInterfaceOrientationメソッドに配置します。スケルトン形式では、次のようになります。
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
{
//NSLog(@"willAnimateRotationToInterfaceOrientation: %d", toInterfaceOrientation);
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation))
{
// portrait
}
else
{
// landscape
}
}
編集:将来の使用のためにデバイスの回転を覚えておく必要がある状況では、currentOrientation(タイプint)と呼ばれるビューコントローラークラスにivarを設定し、次のようにします。
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
//NSLog(@"shouldAutorotateToInterfaceOrientation: %d", toInterfaceOrientation);
if (toInterfaceOrientation == UIDeviceOrientationPortrait || toInterfaceOrientation == UIDeviceOrientationPortraitUpsideDown ||
toInterfaceOrientation == UIDeviceOrientationLandscapeLeft || toInterfaceOrientation == UIDeviceOrientationLandscapeRight)
{
currentOrientation = toInterfaceOrientation;
}
return YES;
}
次に、View Controllerでメソッドを実行しているときに、デバイスがどの方向にあるかがわかります。