iOS Global processing keyboard events

Source: Internet
Author: User

Transferred from: http://www.cnblogs.com/xinus/archive/2013/01/22/ios-keybord-notification.html

  1. Register for notification of keyboard events
        [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (                                               Keyboardwillshow:) name:uikeyboardwillshownotification        Object:nil]; [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (keyb                                               Oardshow:) name:uikeyboarddidshownotification        Object:nil]; [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (keyb                                               Oardwillhide:) name:uikeyboardwillhidenotification        Object:nil]; [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (keyb        Oardhide:)                                         Name:uikeyboarddidhidenotification Object:nil];
  2. In the callback that the keyboard will appear and hide, add the animation.
    -(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.f        Rame)/2.0); }} [UIView commitanimations];}    -(void) Keyboardhide: (nsnotification *) Notif {if (Self.hidden = = YES) {return; }}

iOS Global processing keyboard events

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.