zoukankan      html  css  js  c++  java
  • 键盘操作往往需要添加一个遮盖层,防止点击其他地方导致出错

    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;

    }

  • 相关阅读:
    第三方登录(QQ登录)开发流程详解
    网页优化方案
    linux中PHP链接MySQL主机127.0.0.1与localhost
    RSync实现文件备份同步
    网站攻击以及解决方案
    迎难而上,QPS提高22+倍
    新的一扇窗
    边缘计算开源平台
    高并发分布式计算-生产实践
    分布式UUID的生成
  • 原文地址:https://www.cnblogs.com/niexiaobo/p/4528187.html
Copyright © 2011-2022 走看看