欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS应用开发中监听键盘事件的代码实例小结

程序员文章站 2023-10-19 09:47:58
1.注册监听键盘事件的通知 复制代码 代码如下:     [[nsnotificationcenter defaultcenter] ad...

1.注册监听键盘事件的通知

复制代码 代码如下:

    [[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.在键盘将要出现和隐藏的回调中,加入动画

复制代码 代码如下:

- (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.frame)/2.0);
        }
    }
    [uiview commitanimations];
}

- (void)keyboardhide:(nsnotification *)notif {
    if (self.hidden == yes) {
        return;
    }
}

3.监听键盘删除键之非代理实现
在uitextfield 和 uitextview ,如何监听到删除键。

我看到网上都是用代理监听的,我觉得不靠谱。

其实苹果已经写的很清楚了。

就在他们实现的协议里面~~

复制代码 代码如下:

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


写的非常清楚,一看就明白。

-deletebackward 这个方法就是删除按钮监听。

只要自己写个子类,重写此方法就能监听。