以模态方式呈现视图控制器显示奇怪的动画

3
我试图创建一个简单的模态视图控制器,让您使用文本视图编辑文本。然而,当我以模态方式呈现视图控制器时,它从左下方滑入,而不是仅从底部滑入。这里有一个奇怪效果的视频: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

确保您的应用程序中没有缺少的“commitAnimation”调用(紧接着“beginAnimation”的内容)。 - Till
1
你为什么要使用自己的动画?你是想让模态转换看起来与普通的不同吗? - rdelmar
1个回答

6

通过在动画块中包装[self.view layoutIfNeeded]调用,您的视图正在按照您的要求进行动画。

viewDidLoad中,您开始观察键盘的变化,当您检测到它们时,您会对调整进行动画处理,这通常是正确的。但是,在视图进行第一次布局之前,您显示了键盘;这导致所有视图从CGRectZero到其正确大小的动画效果。这就是您看到的效果。

因此,基本上您需要在动画的layoutIfNeeded调用之前给视图一个布局的机会。可能最简单的方法是将[self.textView becomeFirstResponder];移动到viewWillAppear:viewDidAppear:中。

*作为附注,请记住在外观调用中调用super。我注意到您没有调用[super viewWillDisappear];


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接