2013-03-09 45 views
1

我試圖創建一個簡單的模態視圖控制器,讓您使用文本視圖編輯文本。但是,當我以模態方式呈現視圖控制器時,它從左下方滑入,而不是從底部滑入。目前的ViewController模態顯示奇怪的動畫

這裏的怪異效果的視頻:http://youtu.be/9M_MHA5mt1M

我控制器只需手錶鍵盤顯示,然後適當地調整其大小使用自動佈局文本視圖。下面的代碼:

#import "TextPicker.h" 

@interface TextPicker() 

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *keyboardHeight; 

@end 

@implementation TextPicker 

- (id)initWithText:(NSString *)text 
{ 
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil]; 
    self = [storyboard instantiateViewControllerWithIdentifier:@"textPicker"]; 
    if (self) { 
     self.text = text; 

    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    [self observeKeyboard]; 

    //self.textView.text = self.text; 
    [self.textView becomeFirstResponder]; 
} 


- (void) viewWillDisappear:(BOOL)animated { 
    [self.textView resignFirstResponder]; 
} 

- (IBAction)savePressed:(id)sender { 
    [self dismissViewControllerAnimated:YES completion:nil];  
} 

- (IBAction)cancelPressed:(id)sender { 
    [self dismissViewControllerAnimated:YES completion:nil]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
} 

- (void) dealloc { 
    [self stopObervingKeyboard]; 
} 

- (void)observeKeyboard { 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillChangeFrameNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; 
} 

- (void)stopObervingKeyboard { 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; 
} 

- (void)keyboardWillShow:(NSNotification *)notification { 
    NSDictionary *info = [notification userInfo]; 

    NSValue *kbFrame = [info objectForKey:UIKeyboardFrameEndUserInfoKey]; 
    CGRect keyboardFrame = [kbFrame CGRectValue]; 
    self.keyboardHeight.constant = -keyboardFrame.size.height; 

    NSTimeInterval animationDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; 
    [UIView animateWithDuration:animationDuration animations:^{ 
     [self.view layoutIfNeeded]; 
    }]; 
} 

- (void)keyboardWillHide:(NSNotification *)notification { 
    NSDictionary *info = [notification userInfo]; 
    NSTimeInterval animationDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; 

    self.keyboardHeight.constant = 0; 
    [UIView animateWithDuration:animationDuration animations:^{ 
     [self.view layoutIfNeeded]; 
    }]; 
} 

- (IBAction)dismissKeyboard:(id)sender { 
    [self.textView resignFirstResponder]; 
} 


@end 
+0

確保您的應用程序中沒有缺少「commitAnimation」調用(將遵循「beginAnimation」)。 – Till 2013-03-09 03:10:05

+1

你爲什麼使用自己的動畫?你是否試圖與普通的模態轉換有不同的外觀? – rdelmar 2013-03-09 03:25:46

回答

1

你的觀點是動畫,你問它通過包裝動畫塊內的[self.view layoutIfNeeded]電話。

viewDidLoad中,您開始觀察鍵盤更改,當您檢測到它們時,您將對調整設置動畫,這通常是正確的。但是,在視圖完成其第一個佈局之前,您將顯示鍵盤;這會產生一個從CGRectZero到適當尺寸的所有視圖的動畫。這就是你看到的效果。

所以基本上你需要給視圖一個機會,在之前你的動畫layoutIfNeeded調用。可能最簡單的方法就是將[self.textView becomeFirstResponder];移動到viewWillAppear:viewDidAppear:

*作爲一個方面說明,請記住在外觀調用超級電話。我注意到你沒有撥打[super viewWillDisappear];

+0

完全正確,謝謝! – aloo 2013-03-10 02:57:38