我使用UITextField方法inputView创建了一个自定义键盘。这非常有效。但键盘显示为屏幕底部的普通键盘。我想从屏幕右侧为键盘外观设置动画。 这甚至可能吗?
感谢您的帮助!
答案 0 :(得分:1)
我最终编写了自己的键盘编码。
如果有人有兴趣:
@interface Keyboard ()
@property (assign, readwrite) BOOL visible;
@property (nonatomic, strong) UITextField *editingTextInput;
@property (nonatomic, strong) UIView *blockingView;
@end
@implementation Keyboard
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self addObservers];
self.exclusiveTouch = YES;
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self addObservers];
self.exclusiveTouch = YES;
}
return self;
}
#pragma mark Register for notifications
- (void)addObservers {
// Keep track of the textView/Field that we are editing
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(editingDidBegin:)
name:UITextFieldTextDidBeginEditingNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(editingDidBegin:)
name:UITextViewTextDidBeginEditingNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(editingDidEnd:)
name:UITextFieldTextDidEndEditingNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(editingDidEnd:)
name:UITextViewTextDidEndEditingNotification
object:nil];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UITextFieldTextDidBeginEditingNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UITextViewTextDidBeginEditingNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UITextFieldTextDidEndEditingNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UITextViewTextDidEndEditingNotification
object:nil];
}
-(BOOL)isKeyboardForTextField:(UITextField*)textField
{
// need to be implemented at child object
return NO;
}
#pragma mark Show In View
static UIView *showInView;
+(void)setShowInView:(UIView *)view {
showInView = view;
}
+(UIView*)showInView {
return showInView;
}
#pragma mark Notifications - editing DidBegin/End
// Editing just began, store a reference to the object that just became the firstResponder
- (void)editingDidBegin:(NSNotification *)notification {
if ([notification.object isKindOfClass:[UIResponder class]])
{
if ([notification.object conformsToProtocol:@protocol(UITextInput)]) {
if([self isKeyboardForTextField:notification.object]) {
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(hideKeyboard) object:nil];
_targetTextInput = notification.object;
_targetTextInput.inputView = [[UIView alloc] initWithFrame:CGRectNull];;
if(!_visible) {
[self showKeyboard];
}
}
return;
}
}
// Not a valid target for us to worry about.
_targetTextInput = nil;
}
// Editing just ended.
- (void)editingDidEnd:(NSNotification *)notification {
[self performSelector:@selector(hideKeyboard) withObject:nil afterDelay:0.1]; // so we have chance to stop it
}
#pragma mark Show / Hide keyboard
-(void)enableUserInteraction {
[_blockingView removeFromSuperview];
}
-(void)disableUserInteraction {
_blockingView = [[UIView alloc] initWithFrame:[Keyboard showInView].frame];
_blockingView.backgroundColor = [UIColor clearColor];
[[Keyboard showInView] addSubview:_blockingView];
}
- (void)showKeyboardWithAnimation:(BOOL)animation
{
[[Keyboard showInView] addSubview:self];
_visible = NO;
[self disableUserInteraction];
if (IS_IPHONE)
{
[self setOrigin:CGPointMake(0, [Keyboard showInView].size.height)];
[self updateBackground];
[UIView animateWithDuration:( animation ? .2 : 0)
delay:0.0
options: UIViewAnimationOptionBeginFromCurrentState
animations:^(void){
[self moveOriginY:-self.size.height];
}
completion:^(BOOL finished){
_visible = YES;
[self enableUserInteraction];
}];
} else {
[self setOrigin:CGPointMake([Keyboard showInView].size.width, [Keyboard showInView].size.height - self.size.height - 10)];
[UIView animateWithDuration:( animation ? .5 : 0)
delay:0.0
options: UIViewAnimationOptionBeginFromCurrentState
animations:^(void){
[self moveOriginX:-self.size.width];
}
completion:^(BOOL finished){
_visible = YES;
[self enableUserInteraction];
}];
}
}
-(void)showKeyboard
{
[self showKeyboardWithAnimation:YES];
}
- (void)hideKeyboardWithAnimation:(BOOL)animation
{
[self disableUserInteraction];
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) {
[UIView animateWithDuration:( animation ? .2 : 0)
delay:0.0
options: UIViewAnimationOptionBeginFromCurrentState
animations:^(void){
[self moveOriginY:self.size.height];
}
completion:^(BOOL finished){
[self removeFromSuperview];
_visible = NO;
[self enableUserInteraction];
}];
} else {
[UIView animateWithDuration:( animation ? .5 : 0)
delay:0.0
options: UIViewAnimationOptionBeginFromCurrentState
animations:^(void){
[self moveOriginX:self.size.width];
}
completion:^(BOOL finished){
[self removeFromSuperview];
_visible = NO;
[self enableUserInteraction];
}];
}
}
-(void)hideKeyboard
{
[self hideKeyboardWithAnimation:YES];
}
@end
UIView类别:
@implementation UIView (Frame)
-(CGPoint)origin {
return self.frame.origin;
}
-(CGSize)size {
return self.frame.size;
}
#pragma mark Change Origin
-(void)setOrigin:(CGPoint)origin
{
CGRect frame = self.frame;
frame.origin = origin;
self.frame = frame;
}
-(void)setOriginX:(CGFloat)x
{
CGRect frame = self.frame;
frame.origin.x = x;
self.frame = frame;
}
-(void)setOriginY:(CGFloat)y
{
CGRect frame = self.frame;
frame.origin.y = y;
self.frame = frame;
}
#pragma mark Move Origin
-(void)moveOrigin:(CGPoint)origin
{
CGRect frame = self.frame;
frame.origin.y += origin.y;
frame.origin.x += origin.x;
self.frame = frame;
}
-(void)moveOriginX:(CGFloat)x
{
CGRect frame = self.frame;
frame.origin.x += x;
self.frame = frame;
}
-(void)moveOriginY:(CGFloat)y
{
CGRect frame = self.frame;
frame.origin.y += y;
self.frame = frame;
}
#pragma mark Change Size
-(void)setSize:(CGSize)size
{
CGRect frame = self.frame;
frame.size = size;
self.frame = frame;
}
-(void)setSizeWidth:(CGFloat)width
{
CGRect frame = self.frame;
frame.size.width = width;
self.frame = frame;
}
-(void)setSizeHeight:(CGFloat)height
{
CGRect frame = self.frame;
frame.size.height = height;
self.frame = frame;
}
#pragma mark Change Size
-(void)addSizeWidth:(CGFloat)width
{
CGRect frame = self.frame;
frame.size.width += width;
self.frame = frame;
}
-(void)addSizeHeight:(CGFloat)height
{
CGRect frame = self.frame;
frame.size.height += height;
self.frame = frame;
}
@end