2

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

ところで、私が計画しているアニメーションの種類は次のとおりです。

a)デバイスが横向きになったら、落下しているかのように新しいビューを上からスライドさせます。

b)デバイスが縦向きに戻ると、そのビューは下にスライドして消えます。

4

1 に答える 1

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

ここで行ったことはUIView、に横向きで追加され、名前付き.xibに接続されています。サブビューとして追加し、事前に回転させて、画面外に移動します。また、デバイスローテーション通知のオブザーバーとして自分自身を追加します(後でその通知のために自分自身を削除することを忘れないでください)。そして、私はこのVCがポートレートのみを処理することを示しています。IBOutletsidewaysviewDidLoadshouldAutoRo..

デバイスが回転すると、NSNotificationCenter呼び出しが行われますorientationChange:。その時点で、デバイスを左に回転させると、sidewaysビューが右からスライドインします(スライドダウンしているように見えます)。

明らかに、両方の横向きの場合、コードはより複雑になります。また、2番目のビューが「落ちている」ように感じさせるには、アニメーションのタイミングをいじる必要があります。

于 2012-02-07T21:16:37.467 に答える