简体   繁体   English

像imessenger一样在屏幕上滑动时显示/隐藏键盘

[英]Show/Hide keyboard when swipe on screen like imessenger provides

I want to implement , when swipe bottom to top of screen the keyboard is shown on screen and when swipe from top to bottom on screen the keyboard hides. 我要实现,当从屏幕底部滑动到顶部时,键盘显示在屏幕上;当从屏幕顶部至底部滑动时,键盘隐藏。 Its like iOS 7 effect when we swipe on screen the search textfield and keyboard is shown and when swipe down its hide. 就像iOS 7效果一样,当我们在屏幕上滑动时会显示搜索文本字段和键盘,并在其隐藏处向下滑动。

Try this: 尝试这个:

//declare a property to store your current responder
@property (nonatomic, assign) id currentResponder;
//in viewDidLoad:

UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(resignOnSwipe:)];
[self.collectionView addGestureRecognizer:swipe];


//Implement the below delegate method:

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    self.currentResponder = textField;
}

//Implement resignOnSwipe:

- (void)resignOnSwipe:(id)sender {
    [self.currentResponder resignFirstResponder]
}

Try Something like this,.. It works fine 尝试这样的事情,..效果很好

In xib, 在xib中

Add a textfield and hide it. 添加一个文本字段并将其隐藏。 Connect that to your .h file and name it as tf. 将其连接到您的.h文件,并将其命名为tf。

In .h file, 在.h文件中,

Add

#import <UIKit/UIKit.h>

@interface KeyboardDisplay : UIViewController <UIGestureRecognizerDelegate>
{    

__weak IBOutlet UITextField *tf; __weak IBOutlet UITextField * tf;

}

In .m file, 在.m文件中,

- (void)viewDidLoad
{

[super viewDidLoad];

UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionUp;
[self.view addGestureRecognizer:swipeGesture];


UISwipeGestureRecognizer *swipeGesture2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
swipeGesture2.direction = UISwipeGestureRecognizerDirectionDown;
[self.view addGestureRecognizer:swipeGesture2];

}

-(void)handleSwipeGesture:(UISwipeGestureRecognizer *) sender
{

//Gesture detect - swipe up/down , can be recognized direction
if(sender.direction == UISwipeGestureRecognizerDirectionUp)
{

    [tf becomeFirstResponder];

    NSLog(@"Up");

}
else if(sender.direction == UISwipeGestureRecognizerDirectionDown)
{

    [tf resignFirstResponder];
    NSLog(@"down");
}
}

NOTE : Don't forget to hide the textfield in xib. 注意:不要忘记在xib中隐藏文本字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM