我想实现,当从屏幕底部滑动到屏幕顶部时,键盘会显示在屏幕上,当屏幕上从上到下滑动时,键盘会隐藏。 当我们在屏幕上滑动搜索文本字段和键盘时,它就像iOS 7效果一样,当向下滑动它的隐藏时。
答案 0 :(得分:3)
试试这个:
//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]
}
答案 1 :(得分:0)
尝试这样的事情,..它运作良好
在xib中,
添加文本字段并隐藏它。将其连接到.h文件并将其命名为tf。
在.h文件中,
添加
#import <UIKit/UIKit.h>
@interface KeyboardDisplay : UIViewController <UIGestureRecognizerDelegate>
{
__弱IBOutlet UITextField * tf;
}
在.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");
}
}
注意:不要忘记在xib中隐藏文本字段。