gpt4 book ai didi

ios - Present ViewController 模态显示奇怪的动画

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:15:24 25 4
gpt4 key购买 nike

我正在尝试创建一个简单的模态视图 Controller ,让您可以使用 TextView 编辑文本。但是,当我以模态方式呈现 View Controller 时,它会从左下方向滑入,而不是仅从底部滑入。

这是一个奇怪效果的视频:http://youtu.be/9M_MHA5mt1M

我的 Controller 只是观察键盘的显示,然后适本地使用自动布局调整 TextView 的大小。这是代码:

#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

最佳答案

通过将 [self.view layoutIfNeeded] 调用包装在动画 block 中,您的 View 会按照您的要求进行动画处理。

viewDidLoad 中,您开始观察键盘变化,并且当您检测到它们时,您会为调整设置动画,这通常是正确的。但是,在 View 进行第一次布局之前,您需要显示键盘;这会导致所有 View 从 CGRectZero 到适当大小的动画。这就是您看到的效果。

所以基本上您需要让 View 有机会在您的动画 layoutIfNeeded 调用之前进行布局。可能最简单的方法就是将 [self.textView becomeFirstResponder]; 移动到 viewWillAppear:viewDidAppear:

*作为旁注,记得在出场电话中调用 super。我注意到你没有调用 [super viewWillDisappear];

关于ios - Present ViewController 模态显示奇怪的动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15306618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com