#import "AppDelegate.h"
@interface AppDelegate ()<UITextFieldDelegate>
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindowalloc]initWithFrame:[[UIScreenmainScreen] bounds]];
self.window.backgroundColor = [UIColorwhiteColor];
//调用下面的方法
[selflistenKeyboardNot];
[selfcreateControl];
[selfcreateTextFields];
[selfcreateButtons];
[selfcreateLabels];
[self.windowmakeKeyAndVisible];
returnYES;
}
#pragma MARK - 监听键盘通知
-(void)listenKeyboardNot
{
//这个是监听通知中心,就像放了个监听器,我们知道它会做什么,做了什么,这样我们就可以做点事情了。
NSNotificationCenter *center = [NSNotificationCenterdefaultCenter];
//当我们监听到消息的时候,就启动下面这两个方法,然后让他们做事情。
//当键盘隐藏的时候
[center addObserver:selfselector:@selector(KeyboardWillShow:)name:UIKeyboardWillShowNotificationobject:nil];
//当键盘开启的时候
[center addObserver:selfselector:@selector(KeyboardWillHide:)name:UIKeyboardWillHideNotificationobject:nil];
}
//键盘开启的时候,就把y轴的坐标往上移
-(void)KeyboardWillShow:(NSNotification *)not
{
for (int i =0; i<2; i++) {
UIButton *btn = (id)[self.windowviewWithTag:100+i];
btn.frame =CGRectMake(70+i*150,490-100,80, 40);
}
}
//键盘隐藏的时候就恢复坐标位置
-(void)KeyboardWillHide:(NSNotification *)not
{
for (int i =0; i<2; i++) {
UIButton *btn = (id)[self.windowviewWithTag:100+i];
btn.frame =CGRectMake(70+i*150,490, 80,40);
}
}
#pragma mark - 加个模版好看点喽
-(void)createControl
{
//创建一个用户界面控制器,他的边界是bounds
UIControl *ctl = [[UIControlalloc]initWithFrame:self.window.bounds];
ctl.backgroundColor = [UIColorbrownColor];
//设置透明度为0.3,我喜欢透明一点地。(alpha )
ctl.alpha =0.3;
//这个是调用下面地ctlClick方法(下面那个ctlClick这个方法是用来监听用户点击事件的具体下面说,继续看)
[ctl addTarget:selfaction:@selector(ctlClick)forControlEvents:UIControlEventTouchDown];
[self.windowaddSubview:ctl];
//把ctl控件挪到窗口所有子视图的后面--之前在视图层次结构那漏讲了,这里补充下
[self.windowsendSubviewToBack:ctl];
}
#pragma mark - 创建文本输入框
-(void)createTextFields
{
//这个是创建用户名的文本框
UITextField *account = [[UITextFieldalloc]init];
account.frame =CGRectMake(120,120, 200,40);
account.borderStyle =UITextBorderStyleRoundedRect;
account.font = [UIFontsystemFontOfSize:20];
account.keyboardType =UIKeyboardTypeNumberPad;
account.delegate =self;
[self.windowaddSubview:account];
//这个是创建密码文本框
UITextField *pwd = [[UITextFieldalloc]init];
pwd.frame =CGRectMake(120,240, 200,40);
pwd.borderStyle =UITextBorderStyleRoundedRect;
pwd.font = [UIFontsystemFontOfSize:20];
pwd.clearButtonMode =UITextFieldViewModeAlways;
pwd.secureTextEntry =YES;
//这个方法一开始你一定很困惑,这个有什么用,这里就不告诉你了,你自己调试下程序,你就能发现其中的秘密
pwd.clearsOnBeginEditing =YES;
//调用代理协议方法--这个还一时不好说,等到后面我会抽一节来细细讲,现在就先用着(对了最上面的不要忘记添加协议方法就是那个< >里面的)
pwd.delegate =self;
[self.windowaddSubview:pwd];
}
#pragma mark - 创建注册登录
-(void)createButtons
{
//这里是用数组的方法,你学过oc就知道了,数组
NSArray *array =@[@"注册",@"登录"];
for (int i =0; i<array.count; i++) {
UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeSystem];
btn.frame =CGRectMake(70 + i *100,550, 155,40);
[btn setTitle:array[i]forState:UIControlStateNormal];
btn.titleLabel.font = [UIFontsystemFontOfSize:20];
btn.tag =100+i;
[self.windowaddSubview:btn];
}
}
-(void)createLabels
{
NSArray *array =@[@"用户名:",@"密码:"];
for (int i =0; i<array.count; i++) {
UILabel *label = [[UILabelalloc]init];
label.frame =CGRectMake(50,120+i*120,65, 40);
label.text = array[i];
label.font = [UIFontsystemFontOfSize:19];
[self.windowaddSubview:label];
}
}
//这里就是在上面调用的方法了,监听用户点击事件,当用户点击屏幕的时候,就会把键盘回收,我用的是最简单的方法,还有2个同样功能的方法我这里就不写了,这样是取消成为第一响应者方式的一种,如果返回yes,textfield自动成为第一响应者,当textfield进入编辑模式,并且弹出键盘。这是在UITextField里面的方法
-(void)ctlClick
{
[self.windowendEditing:YES];
}
@end