iOS-回收鍵盤 結束應用程序

- (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  鍵盤彈出遮擋的處理
    //問題: 怎么知道這個鍵盤彈出了啊?
    
    //獲取通知中心的單例對象
    //效果: 當鍵盤顯示出來的時候, 執(zhí)行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);
    
    //提交執(zhí)行動畫
    [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);
    
    //提交執(zhí)行動畫
    [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);
    
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容