2013-07-06 60 views
0

旋轉後我的視圖的大小重置有一些小問題,我無法修復它。旋轉後重新初始化UIView視圖和子視圖

我在我的視圖「bottomContainerView」上放了一個「滑動」動作,大小根據方向而不同!

這裏是我的新代碼:

-(void)swipeToggle:(UISwipeGestureRecognizer *)sender { 
if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft || [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight) { 
    if (sender.direction == UISwipeGestureRecognizerDirectionUp){ 

     NSLog(@"if gesture up - LS"); 

     [UIView animateWithDuration:0.5 
           delay:0.1 
          options:UIViewAnimationOptionCurveEaseInOut 
         animations:^{ 
          topContainerView.frame = CGRectMake(0.0, -160.0, 480.0, 244.0); 
          bottomContainerView.frame = CGRectMake(0.0, 0.0, 480.0, 300.0);} 
         completion:^(BOOL finished){ 
          NSLog(@"%f,%f",bottomContainerView.frame.size.width,bottomContainerView.frame.size.height);         
         }]; 

    } else if (sender.direction == UISwipeGestureRecognizerDirectionDown) { 

     NSLog(@"else if gesture down - LS"); 

     [UIView animateWithDuration:0.5 
           delay:0.1 
          options:UIViewAnimationOptionCurveEaseInOut 
         animations:^{ 
          topContainerView.frame = CGRectMake(0.0, -160.0, 480.0, 244.0); 
          bottomContainerView.frame = CGRectMake(0.0, 84.0, 480.0, 216.0);} 
         completion:^(BOOL finished){ 
          NSLog(@"%f,%f",bottomContainerView.frame.size.width,bottomContainerView.frame.size.height); 
         }]; 
    } 
} 
else if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait) { 
    if (sender.direction == UISwipeGestureRecognizerDirectionUp) { 

     NSLog(@"if gesture down - PT"); 

     [UIView animateWithDuration:0.5 
          delay:0.1 
         options:UIViewAnimationOptionCurveEaseInOut 
        animations:^{ 
         topContainerView.frame = CGRectMake(0.0, 0.0, 320.0, 244.0); 
         bottomContainerView.frame = CGRectMake(0.0, 0.0, self.view.frame.size.width, 640.0); 
        } 
        completion:^(BOOL finished){ 
         NSLog(@"%f,%f",bottomContainerView.frame.size.width,bottomContainerView.frame.size.height); 
        }]; 
    } 
    else if (sender.direction == UISwipeGestureRecognizerDirectionDown) { 

     NSLog(@"else if gesture up - PT"); 

     [UIView animateWithDuration:0.5 
           delay:0.1 
          options:UIViewAnimationOptionCurveEaseInOut 
         animations:^{ 
          topContainerView.frame = CGRectMake(0.0, 0.0, 320.0, 244.0); 
          bottomContainerView.frame = CGRectMake(0.0, 244.0, self.view.frame.size.width, 216.0); 
         } 
         completion:^(BOOL finished){ 
          NSLog(@"%f,%f",bottomContainerView.frame.size.width,bottomContainerView.frame.size.height); 
         }]; 

    } 
}   

}

更新的Post - 這是我的新代碼正確與你教會了我什麼,並親切地解釋;-)

+0

你的動畫代碼亂七八糟。爲什麼在基於塊的動畫中使用commitAnimations/beginAnimations? – Abizern

+0

只是因爲我是新手!但也許你可以向我解釋爲什麼我做錯了,如何做得更好而不是批評? – GilbertOOl

+0

特別是因爲你似乎是一個不錯的偉大的ios開發者! – GilbertOOl

回答

0

我的眼睛受到傷害當看這個代碼。

這裏您試圖結合2種聲明動畫的方式。 如果您正在使用beginAnimations:你不必使用[UIView animateWithDuration:]的方法,但必須與[UIView commitAnimations];

最清晰的方式完成您的動畫代碼是用[UIView animateWithDuration:]並把動畫代碼裏面animations:^{}塊。

P.S:我強烈建議不要在代碼中使用幻數,而是使用常量。 SwitchToFullNormalScreen和SwitchToNormalScreen動畫只是拷貝過來的。我的意思是來吧,那不好。

+0

閱讀您的評論後,我意識到自己的錯誤,所以我看Apple的文檔以瞭解更多信息!所以我嘗試,現在我明白了更好! 非常感謝你,我學到了一些東西,我相信它會爲我的未來服務! 我不明白的是最後的「BOOL完成」是什麼,我覺得這是應該把代碼重置我的意見到正確的大小,對嗎? – GilbertOOl

+0

您應該學習如何使用Xcode內聯文檔。 Quote:「在動畫序列結束時要執行的塊對象,該塊沒有返回值,並且接受一個布爾參數,該參數指示在完成處理程序被調用之前是否實際完成動畫」 –

+0

因此,您可以標記此帖子請回答? – Lonkly