该UIKeyboardAnimationCurveUserInfoKey有一个UIViewAnimationCurve值.如何将其转换为相应的UIViewAnimationOptions值以用于options参数+[UIView animateWithDuration:delay:options:animations:completion:]?
// UIView.h
typedef enum {
UIViewAnimationCurveEaseInOut, // slow at beginning and end
UIViewAnimationCurveEaseIn, // slow at beginning
UIViewAnimationCurveEaseOut, // slow at end
UIViewAnimationCurveLinear
} UIViewAnimationCurve;
// ...
enum {
// ...
UIViewAnimationOptionCurveEaseInOut = 0 << 16, // default
UIViewAnimationOptionCurveEaseIn = 1 << 16,
UIViewAnimationOptionCurveEaseOut = 2 << 16,
UIViewAnimationOptionCurveLinear = 3 << 16,
// ...
};
typedef NSUInteger UIViewAnimationOptions;
Run Code Online (Sandbox Code Playgroud)
显然,我可以使用switch语句创建一个简单的类别方法,如下所示:
// UIView+AnimationOptionsWithCurve.h
@interface UIView (AnimationOptionsWithCurve)
@end …Run Code Online (Sandbox Code Playgroud) 我意识到有许多类似的解决方案,例如TPKeyboardAvoiding,Apple着名的解决方案,以及涉及使用UIScrollView的各种建议.在我的情况下,我需要调整视图大小以适应键盘而不是滚动或移动它.这个解决方案最接近我想要达到的目标,所以这是我的基础.但是我在横向模式下工作时遇到了问题.键盘出现时调整视图大小的方法是:
- (void)keyboardWillShow:(NSNotification *)note {
NSDictionary *userInfo = note.userInfo;
NSTimeInterval duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve curve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
CGRect keyboardFrame = [[self textField].superview convertRect:[[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue] fromView:nil];
CGRect statusBarFrame = [[self textField].superview convertRect:[UIApplication sharedApplication].statusBarFrame fromView:nil];
CGRect bounds = [self textField].superview.bounds;
CGRect newFrame = CGRectMake(0.0, 0.0, bounds.size.width, keyboardFrame.origin.y + statusBarFrame.size.height);
[UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState | curve animations:^{
[self textField].superview.frame = newFrame;
} completion:nil];
}
Run Code Online (Sandbox Code Playgroud)
这在肖像模式下完美运行.

但是,在横向模式下,视图会从左到右或从右到左调整大小,具体取决于设备旋转的方向,而不是从下向上.

显然我使用坐标有些不对劲,而且在横向模式下,某些参考框架并不是我认为的那样,但我有一点时间整理出如何解决它.我尝试用-convertRect转换各种各样的东西:但是我正在尝试的任何东西都让我无处可去.
我真的希望有人不会对所有这些矩形感到困惑,以及当方向变化时他们如何改变可以发现我做错了什么以及我需要做些什么才能做到这一点.作为参考,我创建了一个项目,显示了最简单的情况,可以重现我遇到的问题.
我试图实现类似键盘交互的消息在iOS的7我有一个UIView其中包含一个UITextView,当用户选择它来开始打字,我想使这个UIView了inputAccessoryView.这将照顾我的动画,以及UIScrollViewiOS 7中新的键盘消除交互.
当UITextView开始编辑时,我正在尝试将其设置inputAccessoryView为其父级UIView(已在视图层次结构中).键盘出现但没有配件视图.
我读过有些人正在使用UITextFields 的二人组来完成这项工作,但这似乎是实现这一目标的一种不好的方法.
有什么建议?
我需要在即将开始的项目中添加聊天功能.
与此同时,我一直在努力实现我所期望的简单问题,即在屏幕底部有一个UIView,里面有一个UITextView,当用户点击UITextView时,它会用键盘设置动画.
我有它工作,但不幸的是键盘的动画略微落后于它上面的视图.这是我到目前为止的实现:
注册键盘通知:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
Run Code Online (Sandbox Code Playgroud)
键盘通知方法:
- (void)keyboardWillShow:(NSNotification*)notification
{
CGFloat duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
NSInteger curve = [notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
[UIView animateWithDuration:duration delay:0 options:curve animations:^{
CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
_chatViewBottomConstraint.constant = keyboardFrame.size.height;
[self.view layoutIfNeeded];
} completion:nil];
}
Run Code Online (Sandbox Code Playgroud)
有没有其他人做过类似的事情,能否为我提供更好的解决方案?
我有一个视图1.导航栏2.UITableView 3. UITextView.
当我开始编辑textView时,会出现一个键盘,我需要为TextView和TableView设置动画.我已经实现了:https://stackoverflow.com/a/8704371/1808179,但这会动画整个视图,覆盖导航栏.
我尝试单独动画textView,如:
- (void)keyboardWillShow:(NSNotification*)notification
{
CGRect chatTextFieldFrame = CGRectMake(chatTextField.frame.origin.x,chatTextField.frame.origin.y-218,chatTextField.frame.size.width,chatTextField.frame.size.height);
[UIView animateWithDuration:0.5 animations:^{ chatTextField.frame = chatTextFieldFrame;}];
}
Run Code Online (Sandbox Code Playgroud)
但它没有动画,也不会与TableView同步动画.
在不覆盖导航栏的情况下,为tableView和textView设置动画的最佳方法是什么?
ios ×5
objective-c ×4
uikeyboard ×2
enums ×1
ipad ×1
iphone ×1
keyboard ×1
uitableview ×1
uitextfield ×1
uiview ×1