zjf
2023-03-06 392b76515f40376b6d36f40a114850ef63650384
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//
//  WKAvoidKeyboardViewController.m
//  AvoidKeyboardDemo
//
//  Created by 吴珂 on 15/9/9.
//  Copyright (c) 2015年 MyCompany. All rights reserved.
//
 
#import "WKAvoidKeyboardViewController.h"
 
#define GetOSVersion [[UIDevice currentDevice].systemVersion floatValue]
 
#define GetTransformDistance(Distance) (GetOSVersion < 7.1 ? Distance / 2 : Distance)
 
@interface WKAvoidKeyboardViewController ()<UITextFieldDelegate, UITextViewDelegate>
 
@end
 
@implementation WKAvoidKeyboardViewController
 
- (void)searchTextViewWithView:(UIView *)view
{
    for (UIView *subview in view.subviews)
    {
        if ([subview isKindOfClass:[UITextView class]]) {
            ((UITextView *)subview).delegate = self;
        }
        if ([subview isKindOfClass:[UITextField class]]) {
            ((UITextField *)subview).delegate = self;
        }
        [self searchTextViewWithView:subview];
    }
}
 
- (void)viewDidLoad {
    [super viewDidLoad];
 
    [self searchTextViewWithView:self.view];
    //注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showKeyboard:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideKeyboard:) name:UIKeyboardWillHideNotification object:nil];
}
 
 
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 
#pragma mark - 键盘躲避
- (void)showKeyboard:(NSNotification *)noti
{
    
    self.view.transform = CGAffineTransformIdentity;
    UIView *editView = _editTextView ? _editTextView : _editTextField;
    
    CGRect tfRect = [editView.superview convertRect:editView.frame toView:self.view];
    NSValue *value = noti.userInfo[@"UIKeyboardFrameEndUserInfoKey"];
    NSLog(@"%@", value);
    CGRect keyBoardF = [value CGRectValue];
    
    CGFloat animationTime = [noti.userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] floatValue];
    CGFloat _editMaxY = CGRectGetMaxY(tfRect);
    CGFloat _keyBoardMinY = CGRectGetMinY(keyBoardF);
    NSLog(@"%f %f", _editMaxY, _keyBoardMinY);
    
    //获取状态栏的rect
    CGRect statusRect = [[UIApplication sharedApplication] statusBarFrame];
    //获取导航栏的rect
    CGRect navRect = self.navigationController.navigationBar.frame;
    //那么导航栏+状态栏的高度
    CGFloat height = statusRect.size.height + navRect.size.height;
    if (navRect.size.height == 0) {
        height = 0;
    }
    _editMaxY += height;
    if (_keyBoardMinY < _editMaxY) {
        CGFloat moveDistance = _editMaxY - _keyBoardMinY;
        [UIView animateWithDuration:animationTime animations:^{
            self.view.transform = CGAffineTransformTranslate(self.view.transform, 0, -moveDistance);
        }];
        
    }
}
 
- (void)hideKeyboard:(NSNotification *)noti
{
    //    NSLog(@"%@", noti);
    self.view.transform = CGAffineTransformIdentity;
}
 
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    _editTextField = textField;
    _editTextView = nil;
}
 
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    _editTextView = textView;
    _editTextField = nil;
    return YES;
}
 
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
 
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end