ベストは主観的であり、アプリケーション全体に依存します。
ローテーションイベントを処理するかなり単純な方法の1つは、システムに自分自身ではなく、自分で処理することです。基本的には、デバイスが側面に回転しているときに、側面から(プリ回転された)ビューをスライドさせることになります。
このような効果を得るための基本的なサンプルは次のとおりです。
@implementation B_VCRot_ViewController // defined in .h as @interface B_VCRot_ViewController : UIViewController
@synthesize sideways; // defined in .h as @property (strong, nonatomic) IBOutlet UIView *sideways;
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(void)orientationChange:(NSNotification *)note{
UIDeviceOrientation newOrientation = [[UIDevice currentDevice] orientation];
CGSize sidewaysSize = self.sideways.frame.size;
if (newOrientation == UIDeviceOrientationLandscapeLeft){
[UIView animateWithDuration:1.0 animations:^{
self.sideways.frame = CGRectMake(0, 0, sidewaysSize.width, sidewaysSize.height);
}];
}
else {
[UIView animateWithDuration:1.0 animations:^{
self.sideways.frame = CGRectMake(self.view.bounds.size.width, 0, sidewaysSize.width, sidewaysSize.height);
}];
}
}
- (void)viewDidLoad{
[super viewDidLoad];
[self.view addSubview:sideways];
self.sideways.transform = CGAffineTransformMakeRotation(M_PI_2); // Rotates the 'sideways' view 90deg to the right.
CGSize sidewaysSize = self.sideways.frame.size;
// Move 'sideways' offscreen to the right to be animated in on rotation.
self.sideways.frame = CGRectMake(self.view.bounds.size.width, 0, sidewaysSize.width, sidewaysSize.height);
// register for rotation notifications
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
// Do any additional setup after loading the view, typically from a nib.
}
@end
私はここに行っていることは.xib
に横向きでUIView
を追加し、sideways
という名前IBOutlet
にそれを接続されています。 viewDidLoad
私はそれをサブビューとして追加し、それをあらかじめ回転させ、画面外に移動させます。私はまた、デバイスの回転通知のオブザーバーとして自分自身を追加します(その通知のために後で自分自身を削除することを忘れないでください)。 shouldAutoRo..
では、このVCはポートレイトのみを処理することを示しています。
NSNotificationCenter
をデバイスがローテーションすると、orientationChange:
がコールされます。その時点で、デバイスを左に回転させると、私のsideways
ビューが右からスライドします(それは滑っているようです)。明らかに両方のランドスケープ方向について、コードはより複雑になるであろう。あたかも2番目のビューが「落ちている」ように感じさせるためにアニメーションのタイミングを混乱させる必要があります