码迷,mamicode.com
首页 > 其他好文 > 详细

键盘操作往往需要添加一个遮盖层,防止点击其他地方导致出错

时间:2015-05-25 18:24:11      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

1,部分声明

@property (nonatomic, strong) UIView *coverView;

@property (nonatomic, strong)UITapGestureRecognizer *sideslipTapGes;

 

 

2实现方法

- (void)showCoverView {

    //_testview:弹出键盘后遮挡的view

    _coverView=[[UIView alloc]init];

    _coverView.frame=CGRectMake(0, 0, 320, 640);

    _coverView.backgroundColor=[UIColor colorWithRed:1.f/255.f green:1.f/255.f blue:1.f/255.f alpha:0.4];

    

    //单击覆盖层手势

    

    

    _sideslipTapGes= [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handeTap)];

    [_sideslipTapGes setNumberOfTapsRequired:1];

    

    [_coverView addGestureRecognizer:_sideslipTapGes];

    [self.view addSubview:_coverView];

    _coverView.hidden=YES;

}

/**

 *explain:点击遮盖层

 */

 

-(void)handeTap{

    

[self.view endEditing:YES];

    

}

 

 

#pragma mark- 监听键盘操作

 

- (void)viewDidAppear:(BOOL)animated{

    [super viewDidAppear:animated];

    [self registerForKeyboardNotifications];

}

 

 

- (void)viewWillDisappear:(BOOL)animated{

    [super viewWillDisappear:animated];

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}

 

- (void)registerForKeyboardNotifications

{

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];

}

 

// Called when the UIKeyboardDidShowNotification is sent.

- (void)keyboardWasShown:(NSNotification*)aNotification

{

    _coverView.hidden=NO;

    

}

 

-(void)keyboardWillChangeFrame:(NSNotification*)notification

{

}

 

// Called when the UIKeyboardWillHideNotification is sent

- (void)keyboardWillBeHidden:(NSNotification*)aNotification

{

    _coverView.hidden=YES;

    

   

}

 

- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    [textField resignFirstResponder];

  

    return YES;

}

键盘操作往往需要添加一个遮盖层,防止点击其他地方导致出错

标签:

原文地址:http://www.cnblogs.com/niexiaobo/p/4528187.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!