【原】触摸背景关闭键盘的代码实现

//把bgView强制修改成UIControl,以便响应触摸事件
//UIControl是UIView的子类

//其实有很多情况都可以这样用,用一个普通的UIView来模拟某些控件,可以很灵活!!!!

 

UIView *bgView;


CGRect bgFrame = CGRectMake(0.0, 35.0, 320.0, 460.0 - 35.0);
UIControl *bgControl = [[UIControl alloc] initWithFrame:bgFrame];
[bgControl addTarget:self
action:@selector(resignKeyboardAction)
forControlEvents:UIControlEventTouchDown];
self.bgView = bgControl;
[bgControl release];
self.bgView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.5];
self.bgView.hidden = YES;
[self.view addSubview:bgView];

posted @ 2012-07-19 10:30  编程小翁  阅读(358)  评论(0编辑  收藏  举报
我是来自厦门的Jilon. 翁,请关注我的微博:真实的weng,或关注微信:Jilon