2013-05-03 46 views
1

我有一個視圖,其子視圖根據約束進行調整(iOS 6之前,沒有自動佈局)。我按照預期將設備和視圖旋轉到新的位置和尺寸。手動覆蓋單個視圖的旋轉動畫?

我添加了一個新的視圖到我的XIB。這個視圖將需要以一種無法用設備旋轉時的約束來描述的方式來改變位置。是否有可能允許除新視圖之外的所有默認旋轉邏輯?

如果不是(並且this question暗示沒有)應如何處理此案?我已經嘗試在旋轉的同時添加自己的動畫,但這幾乎肯定是錯誤的(框架並不總是在完全相同的地方結束,可能是因爲這兩個動畫是同時發生的)。

// Called on "viewWillAppear" 
- (void)adjustLayout 
{ 
    if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) 
    { 
     self.myView.frame = CGRectMake(150, 128, 181, 39); 
    } 
    else 
    { 
     self.myView.frame = CGRectMake(119, 148, 181, 39); 
    } 
} 

// Called on willRotateToInterfaceOrientation 
- (void)adjustLayoutToOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration 
{ 
    // This is not really a good way to override the default animations, but it gets the job done. 
    [UIView animateWithDuration:duration animations:^{ 
     if (UIInterfaceOrientationIsLandscape(orientation)) 
     { 
      NSLog(@"frame: %@", NSStringFromCGRect(self.myView.frame)); 
      self.myView.frame = CGRectMake(69, 201, 181, 39); 
     } 
     else 
     { 
      NSLog(@"frame: %@", NSStringFromCGRect(self.myView.frame)); 
      self.myView.frame = CGRectMake(258, 94, 181, 39); 
     } 
    }]; 
} 

回答

1

解決的辦法是佈局自定義視圖元素的方法:

-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:

此塊期間調用所有佈局的變化會隨着標準旋轉的動畫是動畫。

例子:

#import "OSViewController.h" 

@implementation OSViewController 

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{ 
    return YES; 
} 

-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{ 
    [self layout]; 
} 

-(void)viewWillAppear:(BOOL)animated{ 
    [super viewWillAppear:animated]; 
    [self update]; 
    [self layout]; 
} 

-(void)update{ 

} 

-(void)layout{ 

} 

@end 
+0

是您的 「佈局」 相當於我的 「adjustLayout」?因爲如果我在didRotateFromInterfaceOrientation中調用了「adjustLayout」,我的視圖就會在正確的位置結束,但是如果我從willRotateToInterfaceOrientation ::中調用它,它不會... – 2013-05-14 20:29:08

+0

是的,「佈局」相當於「adjustLayout」,但是重點是它在willAnimateRotationToInterfaceOrientation中調用:持續時間:NOT willRotateToInterfaceOrientation;) – 2013-05-14 22:07:07

+0

是的,這就是我的意見在我的評論。沒有持續時間的willAnimateRotationToInterfaceOrientation。這是爲你工作嗎?它不適合我... – 2013-05-14 22:59:21