iOS 애플리케이션 개발 중 키보드 이벤트 수신을 위한 코드 인스턴스 요약

3611 단어
1. 키보드 이벤트 수신 알림 등록
 
   
  

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
   
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardShow:)
                                                 name:UIKeyboardDidShowNotification
                                               object:nil];
   
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
   
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardHide:)
                                                 name:UIKeyboardDidHideNotification
                                               object:nil];


2. 키보드에 나타날 콜백과 숨길 콜백에 애니메이션 추가
 
   
  

- (void)keyboardWillShow:(NSNotification *)notif {
    if (self.hidden == YES) {
        return;
    }
   
    CGRect rect = [[notif.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat y = rect.origin.y;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.25];
    NSArray *subviews = [self subviews];
    for (UIView *sub in subviews) {
       
        CGFloat maxY = CGRectGetMaxY(sub.frame);
        if (maxY > y - 2) {
            sub.center = CGPointMake(CGRectGetWidth(self.frame)/2.0, sub.center.y - maxY + y - 2);
        }
    }
    [UIView commitAnimations];
}

- (void)keyboardShow:(NSNotification *)notif {
    if (self.hidden == YES) {
        return;
    }
}

- (void)keyboardWillHide:(NSNotification *)notif {
    if (self.hidden == YES) {
        return;
    }
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.25];
    NSArray *subviews = [self subviews];
    for (UIView *sub in subviews) {
        if (sub.center.y < CGRectGetHeight(self.frame)/2.0) {
            sub.center = CGPointMake(CGRectGetWidth(self.frame)/2.0, CGRectGetHeight(self.frame)/2.0);
        }
    }
    [UIView commitAnimations];
}

- (void)keyboardHide:(NSNotification *)notif {
    if (self.hidden == YES) {
        return;
    }
}


3. 키보드 삭제 키의 프록시가 아닌 UITExtField와 UITExtView를 감청합니다. 삭제 키를 감청하는 방법입니다.
나는 인터넷상에서 모두 대리감청을 하는 것을 보았는데, 나는 믿을 수 없다고 생각한다.
사실 사과는 이미 매우 분명하게 썼다.
그들이 이룬 협약 안에서~
 
  
NS_CLASS_AVAILABLE_IOS(2_0) @interface UITextView : UIScrollView
@protocol UITextInput
@protocol UIKeyInput

- (BOOL)hasText;

- (void)insertText:(NSString *)text;

- (void)deleteBackward;

@end


매우 분명하게 써서 한 번 보면 곧 안다.
- deleteBackward 는 버튼 수신을 제거하는 방법입니다.
스스로 하위 클래스를 쓰기만 하면 이 방법을 다시 쓰면 감청할 수 있다.

좋은 웹페이지 즐겨찾기