美文网首页
iOS-回收键盘 结束应用程序

iOS-回收键盘 结束应用程序

作者: 亦晴工作室 | 来源:发表于2016-08-25 11:09 被阅读42次
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        //放弃作为第一响应者
        [self.view endEditing:YES];
    }
      **touchesbegan不能用于tableView**
    
    {
    //回收键盘
        UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(dealFinish)];
        self.navigationItem.leftBarButtonItem = leftItem;
    }
    -(void)dealFinish
    {
        [_textView resignFirstResponder];
        self.view.transform = CGAffineTransformMakeTranslation(0, 0);
    }
    
    - (BOOL)textFieldShouldReturn:(UITextField *)textField
    {
        //放弃作为第一响应者
        [textField resignFirstResponder];
        return YES;
    }
    

    //2.2 点击背景图片则回收键盘

        //问题: 如何知道背景图片被点击了啊?
        backView.userInteractionEnabled = YES;
        //定义轻击手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dealTap:)];
        [backView addGestureRecognizer:tap];
    -(void)dealTap:(UITapGestureRecognizer *)tap
    {
        //回收键盘
        [usernameTextField resignFirstResponder];
        [passwordTextField resignFirstResponder];
    }
    

    键盘弹出后遮挡问题

     //2.1  键盘弹出遮挡的处理
        //问题: 怎么知道这个键盘弹出了啊?
        
        //获取通知中心的单例对象
        //效果: 当键盘显示出来的时候, 执行self的dealShow:方法
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dealShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dealHide:) name:UIKeyboardWillHideNotification object:nil];
    
    -(void)dealHide:(NSNotification *)notification
    {
        //加入动画效果
        [UIView beginAnimations:@"" context:nil];
        [UIView setAnimationDuration:0.4];
        
        button.frame = CGRectMake(100, 300, 80, 30);
        registerButton.frame = CGRectMake(200, 300, 80, 30);
        
        //提交执行动画
        [UIView commitAnimations];
    }
    
    -(void)dealShow:(NSNotification *)notification
    {
        //加入动画效果
        [UIView beginAnimations:@"" context:nil];
        [UIView setAnimationDuration:0.4];
        
        button.frame = CGRectMake(100, 200, 80, 30);
        registerButton.frame = CGRectMake(200, 200, 80, 30);
        
        //提交执行动画
        [UIView commitAnimations];
    }
    

    结束应用程序

    - (void)exitApplication {
        
        AppDelegate *app = [UIApplication sharedApplication].delegate;
        UIWindow *window = app.window;
        
        [UIView animateWithDuration:1.0f animations:^{
            window.alpha = 0;
            window.frame = CGRectMake(0, window.bounds.size.width, 0, 0);
        } completion:^(BOOL finished) {
            exit(0);
        }];
        //exit(0);
        
    }
    

    相关文章

      网友评论

          本文标题:iOS-回收键盘 结束应用程序

          本文链接:https://www.haomeiwen.com/subject/lqlosttx.html