Listening events for iOS---keyboards

Source: Internet
Author: User

Overload the Viewwillappear method to add a notification listener event keyboardwillshow:keyboardwillhide when the view is about to appear:

-(void) Viewwillappear: (BOOL) animated

{

[Super viewwillappear:animated];

[[nsnotificationcenter defaultcenter] addobserver: Self

selector:@selector (keyboardwillshow:)

Name:uikeyboardwillshownotification

object:nil];

[[nsnotificationcenter defaultcenter] addobserver: Self

selector:@selector (keyboardwillhide:)

Name:uikeyboardwillhidenotification

object:nil];

}

When your page jumps, to remove two notifications because notifications are not removed, they persist in memory

-(void) Viewdiddisappear: (BOOL) paramanimated {

[Super viewdiddisappear:paramanimated];

[[nsnotificationcenter defaultcenter] removeobserver:self];

}

Two ways to implement a listener event

-(void) Keyboardwillshow: (nsnotification *) Note {

Get the height of the keyboard

The userInfo dictionary in the notification message nsnotification contains the location and size information for the keyboard, and the corresponding key is

Uikeyboardframebeginuserinfokey
Uikeyboardframeenduserinfokey

The corresponding value is a Nsvalue object that contains the CGRect structure, which is the position information at the start and end of the keyboard, respectively.
Uikeyboardanimationdurationuserinfokey

The corresponding value is also the NSNumber object, which is a double-type data that represents the duration of the animation when the keyboard h is displayed or disappears.
Uikeyboardanimationcurveuserinfokey

The corresponding value is the NSNumber object, which is an internal uiviewanimationcurve type of data that represents the type of animation that the keyboard displays or disappears.

Uikeyboardframeenduserinfokey the position of the pre-animated keyboard, including the CGRect Nsvalue

Uikeyboardframeenduserinfokey the keyboard position at the end of the animation, including the CGRect nsvalue

//  nsdictionary* info = [anotification userInfo];

CGRect _rect =[[info Objectforkey:uikeyboardframeenduserinfokey] cgrectvalue];

Note.info[objectforkey:uikeyboardframeenduserinfokey]

CGRect keyboardrect=[note. UserInfo[Uikeyboardframeenduserinfokey] cgrectvalue];

cgfloat Deltay=keyboardrect. size. height;

[UIView animatewithduration: [Note.userinfo[ Uikeyboardanimationdurationuserinfokey] Floatvalue] animations: ^{

self. View. Transform=cgaffinetransformmaketranslation(0,-deltay);

}];

}

-(void) Keyboardwillhide: (nsnotification *) Note {

[UIView animatewithduration: [Note.userinfo[uikeyboardanimationdurationuserinfokey] Floatvalue] Animations: ^{

Cgaffinetransformidentity Record the previous location

Cgaffinetransformmaketranslation: Each time is based on the center point of the original position for reference

Cgaffinetransformtranslate each time with the incoming transform as a reference (both overlay effect)

Cgaffinetransformidentity The center point of the initial position

Self.view.transform = cgaffinetransformidentity;

}];

}

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.