2012-06-21 29 views
0

我不知道爲什麼會發生這種情況..這段代碼有什麼問題..(動畫視圖)?

我想在用戶開始在文本字段中鍵入內容時激活我的視圖。但代碼僅適用於橫向左排列,但不正確的景觀...

這些方法被稱爲在兩個方向...

這裏是代碼..

- (void)keyboardWasShown:(NSNotification *)aNotification { 
    if (keyboardShown) 
     return; 

     NSTimeInterval animationDuration = 0.3; 
     CGRect frame = self.view.frame; 
     frame.size.width += 150; 
     [UIView beginAnimations:@"ResizeForKeyboard" context:nil]; 
     [UIView setAnimationDuration:animationDuration]; 
     self.view.frame = frame; 
     [UIView commitAnimations]; 



    keyboardShown = YES; 
} 

- (void)keyboardWasHidden:(NSNotification *)aNotification { 

     NSTimeInterval animationDuration = 0.3; 
     CGRect frame = self.view.frame; 
     frame.size.width -= 150; 
     [UIView beginAnimations:@"ResizeForKeyboard" context:nil]; 
     [UIView setAnimationDuration:animationDuration]; 
     self.view.frame = frame; 
     [UIView commitAnimations]; 


    keyboardShown = NO; 
} 

截至目前我剛硬編碼的值..

+0

有你回來是 - 「(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation」?或者這是你添加到窗口的根視圖控制器? –

+1

你爲什麼要調整寬度而不是高度? – gschandler

+0

是的,你應該調整高度...... –

回答

0

OK ..想通了一些幫助......

當您設置視圖的框架及其控制器添加到窗口的根視圖控制器那麼你應該使用邊界....

因爲窗戶框架保持不變...

所以這裏是新的代碼看起來..

- (void)keyboardShown:(NSNotification *) aNotification { 
    if (keyboardShown) 
     return; 
    CGRect frame = self.view.bounds; 
    frame.origin.y += 150; 
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil]; 
    [UIView setAnimationDuration:0.3]; 
    self.view.bounds = frame; 
    [UIView commitAnimations]; 
    keyboardShown = NO; 
} 

- (void)keyboardHidden:(NSNotification *) aNotification { 
    CGRect frame = self.view.bounds; 
    frame.origin.y -= 150; 
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil]; 
    [UIView setAnimationDuration:0.3]; 
    self.view.bounds = frame; 
    [UIView commitAnimations]; 
    keyboardShown = NO; 
} 
0

爲什麼不動畫的視圖的起源而不是大小?

frame.origin.y -= 150; 

frame.origin.y = 0; 
+0

這即使是有道理的,但使我的看法轉移左左風景左方向右右風景右方向......如果我改變原點的x值,那麼它向上移動一個,向下移動另一個..:/ –

+0

你可以做view.center.y - = 150; –

+0

對不起,即使這不起作用...與上面相同的問題...還有一些錯誤,設置根視圖控制器,感謝您的努力 –