developer tip

iOS 앱에서 iMessage 스타일 후진 키보드

optionbox 2020. 11. 12. 08:09
반응형

iOS 앱에서 iMessage 스타일 후진 키보드


개인 API 호출을 사용하지 않고 메시지 앱에서 Apple의 iOS5 키보드 동작을 복제 할 수 있는지 궁금합니다. 메시지 앱에서 키보드를지나 아래로 스크롤하면 키보드가 축소되어 메시지를 볼 수있는 공간이 더 많이 생깁니다. 확인해보십시오.

키보드 뷰의 인스턴스를 얻기 위해 심각한 농구를 시작하지 않고는 이것을 만드는 것을 가리키는 어떤 것도 찾을 수 없었습니다. 그리고 나는 애플이 그것에 만족하지 않을 것이라고 확신합니다.

아래에 제공된 답변 외에도 여기에서 내 구현의 완전히 구운 xcode 프로젝트를 볼 수 있습니다. https://github.com/orta/iMessage-Style-Receding-Keyboard


이것은 불완전한 해결책이지만 좋은 출발점이 될 것입니다.

UIViewController에 다음 ivar를 추가하십시오.

CGRect        keyboardSuperFrame; // frame of keyboard when initially displayed
UIView      * keyboardSuperView;  // reference to keyboard view

텍스트 컨트롤러에 inputAccessoryView를 추가합니다. accessoryView로 삽입 할 작은보기를 만들었습니다.

accView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
accView.backgroundColor = [UIColor clearColor];
textField.inputAccessoryView = accView;

위의 코드를 -(void)loadView

보기가로드 될 때 UIKeyboardDidShowNotification 및 UIKeyboardDidHideNotification을 수신하도록 등록하십시오.

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self
        selector:@selector(keyboardWillShow:)
        name:UIKeyboardWillShowNotification
        object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
        selector:@selector(keyboardDidShow:)
        name:UIKeyboardDidShowNotification
        object:nil];
    return;
}

알림에 대한 선택기로 지정된에 메서드를 추가합니다.

// method is called whenever the keyboard is about to be displayed
- (void)keyboardWillShow:(NSNotification *)notification
{
    // makes keyboard view visible incase it was hidden
    keyboardSuperView.hidden = NO;
    return;
}
// method is called whenever the keyboard is displayed
- (void) keyboardDidShow:(NSNotification *)note
{
    // save reference to keyboard so we can easily determine
    // if it is currently displayed
    keyboardSuperView  = textField.inputAccessoryView.superview;

    // save current frame of keyboard so we can reference the original position later
    keyboardSuperFrame = textField.inputAccessoryView.superview.frame;
    return;
}

터치 된 항목을 추적하고 키보드보기를 업데이트하는 메서드 추가 :

// stops tracking touches to divider
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGRect newFrame;
    CGRect bounds = [[UIScreen mainScreen] bounds];

    newFrame = keyboardSuperFrame;
    newFrame.origin.y = bounds.size.height;  

    if ((keyboardSuperView.superview))
        if (keyboardSuperFrame.origin.y != keyboardSuperView.frame.origin.y)
            [UIView animateWithDuration:0.2
                    animations:^{keyboardSuperView.frame = newFrame;}
                    completion:^(BOOL finished){
                                keyboardSuperView.hidden = YES;
                                keyboardSuperView.frame = keyboardSuperFrame;
                                [textField resignFirstResponder]; }];
    return;
}


// updates divider view position based upon movement of touches
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
   UITouch  * touch;
   CGPoint    point;
   CGFloat    updateY;

   if ((touch = [touches anyObject]))
   {
      point   = [touch locationInView:self.view];
      if ((keyboardSuperView.superview))
      {
         updateY = keyboardSuperView.frame.origin.y;
         if (point.y < keyboardSuperFrame.origin.y)
            return;
         if ((point.y > updateY) || (point.y < updateY))
            updateY = point.y;
         if (keyboardSuperView.frame.origin.y != updateY)
            keyboardSuperView.frame = CGRectMake(keyboardSuperFrame.origin.x,
                                                 point.y,
                                                 keyboardSuperFrame.size.width,
                                                 keyboardSuperFrame.size.height);
      };
   };
   return;
}

면책 조항 :

  • 첫 번째 응답으로 사임하면 키보드가 화면 밖으로 미끄러지기 전에 원래 위치로 돌아갑니다. 키보드를보다 유동적으로 닫으려면 먼저 애니메이션을 만들어 키보드를 화면 밖으로 이동 한 다음보기를 숨겨야합니다. 이 부분은 독자들에게 연습 문제로 남겨 두겠습니다.
  • iOS 5 시뮬레이터와 iOS 5가 설치된 iPhone에서만 이것을 테스트했습니다. 이전 버전의 iOS에서는 테스트하지 않았습니다.

The SlidingKeyboard project I created to test this concept is available from GitHub in the examples directory of BindleKit:

https://github.com/bindle/BindleKit

Edit: Updating example to address first disclaimer.


In iOS 7 there is a keyboardDismissMode property on UIScrollView. So just set it to "UIScrollViewKeyboardDismissModeInteractive" and you'll get this behavior. Works in UIScrollView subclasses such as UITableView.

self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;

Swift 3:

tableView.keyboardDismissMode = .interactive

Or change it in storyboard (if using it) in attributes inspector for your UIScrollView subclass.


Vladimir's simple solution will hide the keyboard as the user scrolls down. However to finish the question regarding iMessage, in order to keep a TextField always visible and anchored to the top of the keyboard, you need to implement these methods:

- (UIView *) inputAccessoryView {
     // Return your textfield, buttons, etc
}

- (BOOL) canBecomeFirstResponder {
    return YES;
}

Here's a good tutorial breaking it down more

참고URL : https://stackoverflow.com/questions/7780753/imessage-style-receding-keyboard-in-an-ios-app

반응형