// 先创建一个MainViewController视图控制器, 然后创建一个MoveView继承于UIView,用作之后的移动视图键盘下落使用
#import "MainViewController.h"
#import "MoveView.h"
@interface MainViewController ()<UITextFieldDelegate>
@property (nonatomic, retain)MoveView *moveView;
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
[self createView];
}
- (void)createView
{
self.moveView = [[MoveView alloc] initWithFrame:CGRectMake(40, 80, 40, 40)];
self.moveView.backgroundColor = [UIColor greenColor];
[self.view addSubview:self.moveView];
self.moveView.tag = 2;
[MoveView release];
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 80, 200, 40)];
[self.view addSubview:textField];
[textField release];
textField.placeholder = @"点击return回收键盘";
textField.tag = 10;
textField.delegate = self;
textField.borderStyle = 3;
UITextField *textField1 = [[UITextField alloc]initWithFrame:CGRectMake(100, 150, 200, 40)];
[self.view addSubview:textField1];
[textField1 release];
textField1.tag = 20;
textField1.placeholder = @"点击按键回收键盘";
textField1.borderStyle = 3;
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[self.view addSubview:button];
button.frame = CGRectMake(300, 150, 80, 40);
[button setTitle:@"确定" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
UITextField *textField2 = [[UITextField alloc]initWithFrame:CGRectMake(100, 200, 200, 40)];
[self.view addSubview:textField2];
[textField2 release];
textField2.tag = 30;
textField2.placeholder = @"点击空白处回收键盘";
textField2.borderStyle = 3;
UITextField *textField3 = [[UITextField alloc]initWithFrame:CGRectMake(100, 250, 200, 40)];
[self.view addSubview:textField3];
[textField3 release];
textField3.tag = 40;
textField3.placeholder = @"向下滑动回收键盘";
textField3.borderStyle = 3;
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
[self.view addGestureRecognizer:swipe];
swipe.direction = UISwipeGestureRecognizerDirectionDown;
[swipe release];
UITextField *textField5 = [[UITextField alloc]initWithFrame:CGRectMake(100, 350, 200, 40)];
[self.view addSubview:textField5];
[textField5 release];
textField5.tag = 60;
textField5.placeholder = @"移动视图键盘回收";
textField5.borderStyle = 3;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
return [textField resignFirstResponder];
}
- (void)buttonClick:(id)sender
{
UITextField *text = (UITextField *)[self.view viewWithTag:20];
[text resignFirstResponder];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITextField *text = (UITextField *)[self.view viewWithTag:30];
[text resignFirstResponder];
}
- (void)swipe:(UISwipeGestureRecognizer *)swipe
{
UITextField *text = (UITextField *)[self.view viewWithTag:40];
[text resignFirstResponder];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// 取出一个触摸点
UITouch *touch = [touches anyObject];
// 返回接收器的当前位置的坐标
CGPoint point = [touch locationInView:self.view];
// 如果滑动的视图是设定的视图,执行键盘回收
if (touch.view == self.moveView) {
self.moveView.center = point;
[[self.view viewWithTag:60] resignFirstResponder];
}
}
@end