2

デバイスが回転するときにカスタムアニメーションを提供して、デフォルトのアニメーションを完全にオーバーライドしたいとします。これを達成する最良の方法は何ですか?デバイス回転時にUIViewControllerのデフォルトアニメーションをオーバーライドします。

ところで、私は計画していますアニメーションの種類は次のとおりです。デバイスは、風景の中に入ると、それが落下しているかのよう

a)は、上から新しいビューのスライドを持っています。

b)デバイスがポートレートに戻ったとき、そのビューはスライドして消えます。

答えて

1

ベストは主観的であり、アプリケーション全体に依存します。

ローテーションイベントを処理するかなり単純な方法の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番目のビューが「落ちている」ように感じさせるためにアニメーションのタイミングを混乱させる必要があります

関連する問題