键盘操作往往需要添加一个遮盖层,防止点击其他地方导致出错
2015-05-25 16:24
429 查看
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;
}
@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;
}
相关文章推荐
- QTP在录制脚本的时候会将鼠标点击过的对象自动添加到对象库中,而那些未被点击过的对象将不会被添加,导致针对某一对象手动添加的脚本运行不通过,因此需要手动把测试对象添加到对象库中,方法如下:
- iOS View添加手势,防止View上点击其他视图触发点击效果
- View添加手势,防止View上点击其他视图触发点击效果
- android 防止多次点击,导致事件监听响应到其他界面
- 重写dispatchTouchEvent方法,实现EditText失去焦点后(点击其他地方)收起键盘的效果
- 利用RecycleView实现类似ListView的Item点击,长按等操作事件以及点击后每一项在添加一个列表
- 在只需要一个指定正确的参数的情况下如何防止传入其他干扰的参数
- Android点击其他地方隐藏键盘
- 每天一个JavaScript实例-点击图片显示大图添加鼠标操作
- 页面点击其他地方时进行某些操作
- UITextField防止键盘遮挡以及添加手势操作消失键盘
- swift中点击其他地方键盘能够隐身的问题
- jQuery遮盖蒙板(防止重复点击按钮误操作)
- 操作链表,需要记住保存前一个结点,以及记录下一个节点,否则容易进入死循环,或者数据出错。
- UITextField防止键盘遮挡以及添加手势操作消失键盘
- vsto in excel 时 选中一个cell进入编辑状态 其他操作出错
- 太郁闷了,新建项目的时候不小心改了一个地方,导致已用到java类或者servlet 就出错!
- 今天做一个winform,想直接把窗体改成输出类库,其他地方直接调结果总提示不能注册组件,回来调度,可以,总结,windows还是直接用新建的类型项目,改容易出错
- 在情报传递过程中,为了防止情报被截获,往往需要对情报用一定的方式加密,简单的加密算法虽然不足以完全避免情报被破译,但仍然能防止情报被轻易的识别。我们给出一种最简的的加密方法,对给定的一个字符串,把其中
- 点击输入框显示键盘 点击其他地方隐藏键盘