gpt4 book ai didi

ios - 在键盘顶部显示 TextView 时改进动画

转载 作者:行者123 更新时间:2023-12-01 18:16:46 25 4
gpt4 key购买 nike

我正在制作动画以在 UIKeyboardWillShowNotification 时在顶部键盘上显示 TextView 正在广播如下

-(void) keyboardWillShow:(NSNotification *)notification{
// get keyboard size and loctaion
NSDictionary *info = [notification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
NSNumber *duration = [notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];

__block CGRect containerFrame = containerView.frame;

containerFrame.origin.y = self.view.bounds.size.height - (kbSize.height + containerFrame.size.height);
[UIView animateWithDuration:[duration doubleValue]
delay:0
options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
containerView.frame = containerFrame;
} completion:^(BOOL finished) {
nil;
}
];
}

动画不够好。如果你可以试一试,在动画结束时会看到小问题。

我搜索了一下,发现还有另一种制作动画的方法,而且效果很好:
-(void) keyboardWillShow:(NSNotification *)note{
// get keyboard size and loctaion
CGRect keyboardBounds;
[[note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];
NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];

// Need to translate the bounds to account for rotation.
keyboardBounds = [self.view convertRect:keyboardBounds toView:nil];

// get a rect for the textView frame
CGRect containerFrame = containerView.frame;
containerFrame.origin.y = self.view.bounds.size.height - (keyboardBounds.size.height + containerFrame.size.height);
// animations settings
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:[duration doubleValue]];
[UIView setAnimationCurve:[curve intValue]];

// set views with new info
containerView.frame = containerFrame;


// commit animations
[UIView commitAnimations];
}

但是,根据文档,我们不建议在 iOS 4 及更高版本中使用此动画...

与后一个代码相比,我无法分辨出我的代码之间的任何区别。为什么我的代码不能做完美的动画。如果您有任何想法,请提供帮助。

最佳答案

这是动画曲线。您引用的示例是将其动画曲线及其持续时间与键盘的动画曲线相匹配。你的代码没有。你的结构应该看起来更像这样:

NSNumber* curve = info[UIKeyboardAnimationCurveUserInfoKey]; // *
NSNumber* duration = info[UIKeyboardAnimationDurationUserInfoKey];
[UIView animateWithDuration:duration.floatValue delay:0
options:curve.integerValue << 16 // *
animations: ^{ // ...

关于ios - 在键盘顶部显示 TextView 时改进动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21323525/

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