본문 바로가기

프로그램/iPhone

keyboard가 나타날때 View이동

 .m [viewDidLoad에]

 //뷰 올리고 내리기(화면이 뜨자마자 등록시켜주는것

[[NSNotificationCenter defaultCenter] addObserver:self

                                            selector:@selector(keyboardWillAnimate:)

                                            name:UIKeyboardWillShowNotification //키보드가 올라왔을때keyboardWillAnimate를 실행

                                            object:nil];

    

    [[NSNotificationCenter defaultCenter] addObserver:self

                                             selector:@selector(keyboardWillAnimate:)

                                             name:UIKeyboardWillHideNotification  //들어갈때

                                             object:nil];



//keyboard가 올라가면 이 메소드가 실행되야 하기때문에 viewDidLoad밖에 선언

- (void)keyboardWillAnimate:(NSNotification *)notification

{

    NSLog(@"keyboardWillAnimate"); //Test Log

    

    CGRect keyboardBounds;

    [[notification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardBounds];

    NSNumber *duration = [notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];

    NSNumber *curve = [notification.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];

    

    keyboardBounds = [self.view convertRect:keyboardBounds toView:nil];

    [UIView beginAnimations:nil context:NULL];

    [UIView setAnimationDuration:[duration doubleValue]];

    [UIView setAnimationCurve:[curve intValue]];

    if([notification name] == UIKeyboardWillShowNotification)

    {

        [self.view setFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y - keyboardBounds.size.height, self.view.frame.size.width, self.view.frame.size.height)];

    }

    else if([notification name] == UIKeyboardWillHideNotification)

    {

        [self.view setFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y + keyboardBounds.size.height, self.view.frame.size.width, self.view.frame.size.height)];

    }

    [UIView commitAnimations];

}


//Notification해제

- (void)_removeKeyboardNotification

{

    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];

}

'프로그램 > iPhone' 카테고리의 다른 글

Keyboard return  (0) 2012.07.31