iOS鍵盤監聽事件


1.注冊鍵盤通知事件

    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];

    // 鍵盤將出現事件監聽
    [center addObserver:self selector:@selector(handleKeyboardWillShow:)

                   name:UIKeyboardWillShowNotification

                 object:nil];

    // 鍵盤將隱藏事件監聽
    [center addObserver:self selector:@selector(handleKeyboardWillHide:)

                   name:UIKeyboardWillHideNotification

                 object:nil];

 

2.處理動畫

// 鍵盤出現

- (void)handleKeyboardWillShow:(NSNotification *)paramNotification
{
    NSDictionary *userInfo = [paramNotification userInfo];

    NSValue *animationCurveObject = [userInfo valueForKey:UIKeyboardAnimationCurveUserInfoKey];

    NSValue *animationDurationObject = [userInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey];

    NSValue *keyboardEndRectObject = [userInfo valueForKey:UIKeyboardFrameEndUserInfoKey];

    NSUInteger animationCurve = 0;

    double animationDuration = 0.0f;

    CGRect keyboardEndRect = CGRectMake(0,0, 0, 0);

    [animationCurveObject getValue:&animationCurve];

    [animationDurationObject getValue:&animationDuration];

    [keyboardEndRectObject getValue:&keyboardEndRect];

    UIWindow *window = [[[UIApplication sharedApplication] delegate] window];

    CGRect intersectionOfKeyboardRectAndWindowRect = CGRectIntersection(window.frame, keyboardEndRect);

    CGFloat bottomInset = intersectionOfKeyboardRectAndWindowRect.size.height;

    [UIView beginAnimations:@"changeViewContentInset" context:NULL];

    [UIView setAnimationDuration:animationDuration];

    [UIView setAnimationCurve:(UIViewAnimationCurve) animationCurve];

    CGRect tempRect = self.contentView.frame;

    tempRect.origin.y = Main_Screen_Height - bottomInset - 260 - 20;

    self.contentView.frame = tempRect;

    [UIView commitAnimations];
}
// 鍵盤消失

- (void)handleKeyboardWillHide:(NSNotification *)paramNotification
{
    NSDictionary *userInfo = [paramNotification userInfo];

    NSValue *animationCurveObject =[userInfo valueForKey:UIKeyboardAnimationCurveUserInfoKey];

    NSValue *animationDurationObject = [userInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey];

    NSValue *keyboardEndRectObject =[userInfo valueForKey:UIKeyboardFrameEndUserInfoKey];

    NSUInteger animationCurve = 0;

    double animationDuration = 0.0f;

    CGRect keyboardEndRect = CGRectMake(0, 0, 0, 0);

    [animationCurveObject getValue:&animationCurve];

    [animationDurationObject getValue:&animationDuration];

    [keyboardEndRectObject getValue:&keyboardEndRect];

    [UIView beginAnimations:@"changeViewContentInset" context:NULL];

    [UIView setAnimationDuration:animationDuration];

    [UIView setAnimationCurve:(UIViewAnimationCurve)animationCurve];

    self.contentView.center = self.center;

    [UIView commitAnimations];
}

 

 3.當然是別忘了注銷通知哦

[[NSNotificationCenter defaultCenter] removeObserver:self];


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM