1. Register to monitor keyboard events notification
Copy Code code as follows:
[[Nsnotificationcenter Defaultcenter] Addobserver:self
Selector: @selector (keyboardwillshow:)
Name:uikeyboardwillshownotification
Object:nil];
[[Nsnotificationcenter Defaultcenter] Addobserver:self
Selector: @selector (keyboardshow:)
Name:uikeyboarddidshownotification
Object:nil];
[[Nsnotificationcenter Defaultcenter] Addobserver:self
Selector: @selector (keyboardwillhide:)
Name:uikeyboardwillhidenotification
Object:nil];
[[Nsnotificationcenter Defaultcenter] Addobserver:self
Selector: @selector (keyboardhide:)
Name:uikeyboarddidhidenotification
Object:nil];
2. In the keyboard will appear and hidden callback, add animation
Copy Code code as follows:
-(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) {
& nbsp; return;
}
[UIView beginanimations:nil Context:nil];
[UIView setanimationduration:0.25];
Nsarray *subviews = [self subviews];
for (UIView *sub in subviews) {
if (Sub.center.y & Lt Cgrectgetheight (self.frame)/2.0) {
Sub.center = Cgpointmake (Cgrectgetwidth (self.frame)/2.0, Cgrectgetheight (self.frame)/2.0);
}
}
[UIView commitanimations];
}
-(void) Keyboardhide: (nsnotification *) Notif {
if (Self.hidden = YES) {
Return
}
}
3. Monitor keyboard deletion key non-proxy implementation
in Uitextfield and Uitextview, how to monitor the deletion key.
I see the Internet are using agent monitoring, I feel not reliable.
In fact, the Apple has written very clearly.
In the agreement they've reached.
Copy Code code as follows:
Ns_class_available_ios (2_0) @interface uitextview:uiscrollview<uitextinput>
@protocol uitextinput<uikeyinput>
@protocol Uikeyinput <UITextInputTraits>
-(BOOL) HasText;
-(void) InsertText: (NSString *) text;
-(void) Deletebackward;
@end
The writing is very clear, a look will understand.
-deletebackward This method is to remove the button listener.
If you write a subclass yourself, you can override this method to listen.