gpt4 book ai didi

ios - Obj-c - 返回原始状态时延迟动画?

转载 作者:可可西里 更新时间:2023-11-01 04:32:48 24 4
gpt4 key购买 nike

当我的键盘被激活时,我正在使用下面的代码来移动 View 和我的表格 View 。但是,当键盘关闭时,upView 在键盘关闭后需要 2 秒才能返回到原来的位置(另一方面,tableView 是即时的)。为什么会这样?

      - (void)viewDidLoad {
[super viewDidLoad];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillShowNotification object:nil];


}

- (void)keyboardWillChange:(NSNotification *)notification {


NSDictionary* keyboardInfo = [notification userInfo];

NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameEndUserInfoKey];

CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue];

UITabBarController *tabBarController = [UITabBarController new];
CGFloat tabBarHeight = tabBarController.tabBar.frame.size.height;


self.keyboardHeight = keyboardFrameBeginRect.size.height - tabBarHeight;

}


- (void) animateTextView:(BOOL) up
{

const int movementDistance = self.keyboardHeight;

const float movementDuration = 0.2f;
int movement= movement = (up ? -movementDistance : movementDistance);


[UIView beginAnimations: @"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];

self.upView.frame = CGRectOffset(self.upView.frame, 0, movement);
[UIView setAnimationDidStopSelector:@selector(afterAnimationStops)];
[UIView commitAnimations];

self.tableView.frame = CGRectOffset(self.tableView.frame, 0, movement);
[UIView setAnimationDidStopSelector:@selector(afterAnimationStops)];
[UIView commitAnimations];

}

- (void)textViewDidBeginEditing:(UITextView *)textView
{

[self animateTextView:YES];

}

- (void)textViewDidEndEditing:(UITextView *)textView
{
[self animateTextView:NO];
}

更新代码

.m

- (void)handleKeyboard:(NSNotification*)aNotification{
NSDictionary* info = [aNotification userInfo];
NSValue* value = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval duration = 3;
[value getValue:&duration];
if (aNotification.name == UIKeyboardWillHideNotification) {
/** KEYBOARD HIDE **/

[UIView animateWithDuration:0 animations:^{ self.upView.frame = CGRectOffset(self.upView.frame, 0, self.keyboardHeight); self.tableView.frame = CGRectOffset(self.tableView.frame, 0, self.keyboardHeight); } completion:^(BOOL finished) {}];


[self moveCustomView:NO duration:duration];
NSLog(@"CLOSED!");
}

if (aNotification.name == UIKeyboardWillShowNotification) {
/** KEYBOARD SHOW **/

[UIView animateWithDuration:0 animations:^{ self.upView.frame = CGRectOffset(self.upView.frame, 0, -self.keyboardHeight); self.tableView.frame = CGRectOffset(self.tableView.frame, 0, -self.keyboardHeight); } completion:^(BOOL finished) {}];


[self moveCustomView:YES duration:duration];
}
}

- (void)moveCustomView:(BOOL)move duration:(NSTimeInterval)time{

}

最佳答案

这个问题可能与动画持续时间有关,因此您可以从 -(void)handleKeyboard:(NSNotification *)notification{}

获取键盘显示和隐藏动画持续时间

并且还在同一个函数中处理显示和隐藏您的自定义 View 。将以下代码添加到您的 viewDidLoad 函数

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboard:) name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboard:) name:UIKeyboardWillShowNotification object:nil];

处理键盘操作和 UI 更改

- (void)handleKeyboard:(NSNotification*)aNotification{
NSDictionary* info = [aNotification userInfo];
NSValue* value = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval duration = 0;
[value getValue:&duration];
if (aNotification.name == UIKeyboardWillHideNotification) {
/** KEYBOARD HIDE **/

//calculate your view frames and handle UI changes
/*
.
.
.
.
.
*/
[self moveCustomView:NO duration:duration];
}

if (aNotification.name == UIKeyboardWillShowNotification) {
/** KEYBOARD SHOW **/

//calculate your view frames and handle UI changes
/*
.
.
.
.
.
*/
[self moveCustomView:YES duration:duration];
}
}

- (void)moveCustomView:(BOOL)move duration:(NSTimeInterval)time{

}

关于ios - Obj-c - 返回原始状态时延迟动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48507885/

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