Obj-c - 返回原始状态时延迟动画?
Obj-c - Delayed animation when returning to original state?
我正在使用以下代码在激活键盘时向上移动视图和表格视图。但是,当键盘关闭时,在键盘关闭到 return 到它原来的位置(另一方面,tableView 是即时的)之后,它需要 upView 稳定 2 秒。为什么会这样?
- (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 {}
获取键盘显示和隐藏动画持续时间
并在同一函数内处理显示和隐藏自定义视图。将以下代码添加到您的 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{
}
我认为你需要使用线程来立即调用动画...
- (void)textViewDidBeginEditing:(UITextView *)textView {
dispatch_async(dispatch_get_main_queue(), ^{
[self animateTextView:YES];
});
}
- (void)textViewDidEndEditing:(UITextView *)textView {
dispatch_async(dispatch_get_main_queue(), ^{
[self animateTextView:NO];
});
}
我不太了解你的问题,但试试这个,因为如果我们遇到这类问题,dispatch_async() 将解决。
我正在使用以下代码在激活键盘时向上移动视图和表格视图。但是,当键盘关闭时,在键盘关闭到 return 到它原来的位置(另一方面,tableView 是即时的)之后,它需要 upView 稳定 2 秒。为什么会这样?
- (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 {}
并在同一函数内处理显示和隐藏自定义视图。将以下代码添加到您的 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{
}
我认为你需要使用线程来立即调用动画...
- (void)textViewDidBeginEditing:(UITextView *)textView {
dispatch_async(dispatch_get_main_queue(), ^{
[self animateTextView:YES];
});
}
- (void)textViewDidEndEditing:(UITextView *)textView {
dispatch_async(dispatch_get_main_queue(), ^{
[self animateTextView:NO];
});
}
我不太了解你的问题,但试试这个,因为如果我们遇到这类问题,dispatch_async() 将解决。