我目前正在编写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;
}
更新
它似乎是来自苹果的一个错误...我尝试使用iOS 8.3模拟器,它工作得非常流畅...在iOS 9.3中,如果你只是尝试显示和隐藏键盘,即使没有移动textField ,动画会在几次操作后卡住......
答案 0 :(得分:2)
我建议您将动画块移动到通知UIKeyboardWill{Show|Hide}Notification
中的方法。文本字段中的委托方法询问文本字段及其行为 - 与键盘本身无关(甚至可以synchronize持续时间,曲线和延迟)。
您向-textFieldShouldReturn:
返回YES,该- (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;
}
只应检查文本字段是否应处理返回键。
根据我对动画的体验,有时在使用真实设备进行测试时一切正常。我在这里进行了建议的更改,一切都在iPhone 5c(iOS 8.1)和iPhone 6s Plus(iOS 10,Beta 1)上运行良好。
以下是它的样子:
def state
if story.school_state_territory.blank?
""
else
" | #{ story.school_state_territory }"
end
end
答案 1 :(得分:0)
@lin fu
的.m
- (void)viewDidLoad {
[super viewDidLoad];
UITextField *textField = [[UITextField alloc] init];
textField.placeholder = @"Click me!!";
textField.delegate = self;
textField.frame = CGRectMake(100, 350, 150, 40);
[self.view addSubview:textField];
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
[UIView animateWithDuration:0.25 animations:^{
CGRect frame = textField.frame;
frame.origin.y -= 100;
textField.frame = frame;
}];
return YES;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
[UIView animateWithDuration:0.25 animations:^{
CGRect frame = textField.frame;
frame.origin.y += 100;
textField.frame = frame;
}];
return YES;
}
in .h
试一试。它对我有用。
答案 2 :(得分:0)
您还有另一种选择,您可以使用第三方库调用TPAvoidingKeyBoard它会帮助您在那里