几种键盘回收方式

// 先创建一个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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值