2

我想在設備旋轉時提供自定義動畫,完全覆蓋默認動畫。達到此目的的最佳方式是什麼?在設備旋轉時重寫UIViewController的默認動畫

順便說一句,那種動畫我計劃的是:

a)當設備進入的景觀,有一個新的視圖滑塊從頂部就像它在下降。

b)當設備返回肖像時,該視圖應滑下並消失。

回答

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風景方向,並將其連接到一個名爲sidewaysIBOutlet。在viewDidLoad我添加它作爲子視圖,預旋轉它,並將其移出屏幕。我還將自己添加爲設備輪播通知的觀察者(請記住,稍後要爲該通知刪除自己)。並在shouldAutoRo..我表明,這個VC只處理肖像。當設備旋轉NSNotificationCenter調用orientationChange:。此時,如果設備向左旋轉,我的sideways視圖將從右側滑入(這看起來像是向下滑動)。

很明顯,對於兩種風景方向,代碼都會更復雜。另外,您可能不得不攪亂動畫時機,以使其感覺彷彿第二個視圖是「下降」