2013-03-20 98 views
1

我試圖在我的應用處於橫向模式或縱向模式時在不同位置顯示一組圖像。我已經搜查,發現參考willAnimateRotationToInterfaceOrientation:duration方法,在這裏我補充這樣的代碼:ios:基於方向的移動圖像

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || 
     toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) 
    { 
     self.getStartedImage1.frame = CGRectMake(5, 100, 155, 115); 
     self.getStartedImage2.frame = CGRectMake(160, 100, 155, 115); 
     self.getStartedImage3.frame = CGRectMake(315, 100, 155, 115); 
    } 
    else 
    { 
     self.getStartedImage1.frame = CGRectMake(238, 96, 155, 115); 
     self.getStartedImage2.frame = CGRectMake(238, 219, 155, 115); 
     self.getStartedImage3.frame = CGRectMake(238, 342, 155, 115); 
    } 
} 

這很好工作,直到我意識到,如果你去到風景的同時已經在橫向模式下,該方法不跑。這是有道理的,因爲我在不旋轉任何東西的情況下打開視圖。但是,這並沒有幫助我的困境。所以我繼續我的搜索,並採取了方法viewWillLayoutSubviews。我爲這個方法添加了相同的主體,並且它被調用,但可能已經太晚了,因爲圖像框架的更改沒有出現在模擬器中。如果我嘗試將代碼移動到viewWillAppear,我也遇到同樣的問題。我看到了一些涉及多個視圖的複雜解決方案,我寧願避免。有什麼建議麼?

回答

1

這聽起來像你在與autolayout戰鬥。我能想到的有三種解決方案。

  1. 關閉autolayout。如果您已經自己管理所有視圖框架,那麼這可能是正確的路徑。

  2. 將您的佈局代碼移動到-viewDidLayoutSubviews。這很快但很快。它也允許autolayout做它的東西主要是

  3. 將適用的旋轉約束換掉。這將是最多的工作,但從長遠來看,可以說這是國際化等最靈活的工作。

關於三:

約束(NSLayoutConstraint S)是可被檢查和IB兩者和代碼編輯的真實對象。由於佈局非常複雜,下面是一個非常簡單的例子。假設我在視圖控制器的視圖中有兩個按鈕(連接到兩個出口buttonOnebuttonTwo),並且我想要以橫向方向反轉它們的位置。像這樣的代碼(雖然這種形式不適用)確實有效,並提供了一個如何交換約束的例子。

@implementation MyAutoViewController { 
    NSArray *_portraitConstraints; 
    NSArray *_landscapeConstraints; 
} 
-(void)viewDidLoad{ 
    [super viewDidLoad]; 

    // Clear constraints from Nib. 
    // PLEASE DO NOT DO THIS PART IN REAL LIFE 
    NSArray *current = self.view.constraints; 
    for (NSLayoutConstraint *contsra in current) { 
     [self.view removeConstraint:contsra]; 
    } 

    // Set up views dictionary 
    UIButton *buttonOne = self.buttonOne; 
    UIButton *buttonTwo = self.buttonTwo; 
    NSDictionary *viewsDict = NSDictionaryOfVariableBindings(buttonOne, buttonTwo); 

    // Set distance from left to both buttons to standard space 
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[buttonOne]" options:0 metrics:nil views:viewsDict]]; 
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[buttonTwo]" options:0 metrics:nil views:viewsDict]]; 

    // HERE IS WHERE IT GETS INTERESTING 
    // ### Create portrait constraints ### 
    _portraitConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[buttonOne]-[buttonTwo]" options:0 metrics:nil views:viewsDict]; 

    // ### Create landscape constraints ### 
    _landscapeConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[buttonTwo]-[buttonOne]" options:0 metrics:nil views:viewsDict]; 

    // Add the appropriate constraints 
    [self.view addConstraints:(UIInterfaceOrientationIsPortrait(self.interfaceOrientation))?_portraitConstraints:_landscapeConstraints]; 

} 
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{ 
    if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)){ 
     [self.view removeConstraints:_landscapeConstraints]; 
     [self.view addConstraints:_portraitConstraints]; 
    } else { 
     [self.view removeConstraints:_portraitConstraints]; 
     [self.view addConstraints:_landscapeConstraints]; 
    } 
} 

我真的建議觀看WWDC2012上的autolayout三個視頻。 Autolayout是很多東西。

+0

謝謝。我現在用#2去了。我無法關閉自動佈局,因爲它對我的其他視圖至關重要,我不確定我完全理解#3。你的意思是刪除有關圖像的限制嗎? – 2013-03-20 21:38:18

+0

@CherylYaeger我添加了一些關於選項3的更多信息。 – NJones 2013-03-21 02:13:20

+0

感謝您添加這些額外的信息! – 2013-04-02 18:33:41