gpt4 book ai didi

iOS 键盘动画冲突

转载 作者:行者123 更新时间:2023-12-01 20:09:15 30 4
gpt4 key购买 nike

我目前正在编写一个 iOS 应用程序并遇到了一些动画问题。以下是显示我的问题的测试代码。这只是一个非常简单的动画,让 textField 在键盘显示时向上移动,在键盘隐藏时向下移动。有趣的是,如果是英文键盘,UIView 动画似乎与键盘动画有某种冲突,没有执行任何动画,但如果我切换到中文键盘,它就可以正常工作。我只是想确认它是否是来自苹果的错误....

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

UITextField *textField = [[UITextField alloc] init];
self.textField = textField;
textField.placeholder = @"Click me!!";
textField.frame = CGRectMake(100, 350, 150, 40);
[self.view addSubview:self.textField];
self.textField.delegate = self;
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
[UIView animateWithDuration:0.25 animations:^{
CGRect frame = self.textField.frame;
frame.origin.y -= 100;
self.textField.frame = frame;
}];
return YES;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[self.textField resignFirstResponder];
[UIView animateWithDuration:0.25 animations:^{
CGRect frame = self.textField.frame;
frame.origin.y += 100;
self.textField.frame = frame;
}];
return YES;
}

更新

看来是苹果的bug...我尝试使用iOS 8.3模拟器,它运行 super 流畅...在iOS 9.3中,如果您只是尝试显示和隐藏键盘,即使不移动textField,动画多次操作后会卡住...

最佳答案

我建议您将动画 block 移动到通知中的方法 UIKeyboardWill{Show|Hide}Notification .文本字段的委托(delegate)方法询问文本字段及其行为——与键盘本身无关(您甚至可以 synchronize 持续时间、曲线和延迟)。

您正在向 -textFieldShouldReturn: 返回"is"这应该只检查文本字段是否应该处理返回键点击。

根据我对动画的经验,有时在使用真实设备进行测试时一切正常。我在此处进行了建议的更改,并且在 iPhone 5c (iOS 8.1) 和 iPhone 6s Plus (iOS 10, Beta 1) 上一切正常。

这是它的外观片段:

- (void)viewDidLoad {
[super viewDidLoad];
[self setupNotifications];
[self initUIElements];
}

- (void)initUIElements {
CGRect frame = CGRectMake(0.f, 20.f, 200.f, 44.f);
self.textField = [[UITextField alloc] initWithFrame:frame];
self.textField.center = CGPointMake(self.view.center.x, 32.f);
self.textField.placeholder = @"Placeholder";
self.textField.delegate = self;
[self.view addSubview:self.textField];

self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50.f, 50.f)];
self.imageView.center = self.view.center;
self.imageView.backgroundColor = [UIColor magentaColor];
[self.view addSubview:self.imageView];
}

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

- (void)keyboardWillHide:(id)sender {
[UIView animateWithDuration:1.f animations:^{
self.textField.center = CGPointMake(self.view.center.x, 32.f);
self.imageView.center = self.view.center;
}];
}

- (void)keyboardWillShow:(id)sender {
[UIView animateWithDuration:1.f animations:^{
self.textField.center = CGPointMake(self.view.center.x, 62.f);
self.imageView.center = CGPointMake(self.view.center.x, (self.view.center.y - 50.f));
}];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}

关于iOS 键盘动画冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38065869/

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