您的位置:首页 > 产品设计 > UI/UE

ios UI注册登录界面

2015-02-13 17:02 253 查看
这一个知识章节已经结束了,你会发现这扇大门深处的世界是多么的富有挑战性,是不是很期待?当然了,我们现在还在新手村,不能去打副本什么的。那么今天我代理大家去玩玩。你们准备好了吗?

。这次任务比较多。

好啦,我们准备好我们的装备武器--新建一个工程:

#import "AppDelegate.h"

@interface AppDelegate ()<UITextFieldDelegate>

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions {

    self.window = [[UIWindow
alloc]initWithFrame:[[UIScreen
mainScreen] bounds]];
    
    self.window.backgroundColor = [UIColor
whiteColor];
    //调用下面的方法
    [self
listenKeyboardNot];
    [self
createControl];
    [self
createTextFields];
    [self
createButtons];
    [self
createLabels];
     [self.window
makeKeyAndVisible];
    
    return
YES;
}
#pragma MARK - 监听键盘通知
-(void)listenKeyboardNot
{
   //这个是监听通知中心,就像放了个监听器,我们知道它会做什么,做了什么,这样我们就可以做点事情了。
    NSNotificationCenter *center = [NSNotificationCenter
defaultCenter];
  //当我们监听到消息的时候,就启动下面这两个方法,然后让他们做事情。
   //当键盘隐藏的时候
    [center addObserver:self
selector:@selector(KeyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
   //当键盘开启的时候
    [center addObserver:self
selector:@selector(KeyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
//键盘开启的时候,就把y轴的坐标往上移
-(void)KeyboardWillShow:(NSNotification *)not
{
    for (int i =
0; i<2; i++) {
        UIButton *btn = (id)[self.window
viewWithTag: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.window
viewWithTag:100+i];
        btn.frame =
CGRectMake(70+i*150,
490, 80,
40);
    }
}
#pragma mark - 加个模版好看点喽
-(void)createControl
{
   //创建一个用户界面控制器,他的边界是bounds
    UIControl *ctl = [[UIControl
alloc]initWithFrame:self.window.bounds];
    ctl.backgroundColor = [UIColor
brownColor];
   //设置透明度为0.3,我喜欢透明一点地。(alpha )
    ctl.alpha =
0.3;
    //这个是调用下面地ctlClick方法(下面那个ctlClick这个方法是用来监听用户点击事件的具体下面说,继续看)
    [ctl addTarget:self
action:@selector(ctlClick)
forControlEvents:UIControlEventTouchDown];
    [self.window
addSubview:ctl];
  
    
    //把ctl控件挪到窗口所有子视图的后面--之前在视图层次结构那漏讲了,这里补充下
    [self.window
sendSubviewToBack:ctl];
}

#pragma mark - 创建文本输入框
-(void)createTextFields
{
   //这个是创建用户名的文本框
    UITextField *account = [[UITextField
alloc]init];
    account.frame =
CGRectMake(120,
120, 200,
40);
    account.borderStyle =
UITextBorderStyleRoundedRect;
    account.font = [UIFont
systemFontOfSize:20];
    account.keyboardType =
UIKeyboardTypeNumberPad;
    account.delegate =
self;
    [self.window
addSubview:account];
    
    

   //这个是创建密码文本框    
    UITextField *pwd = [[UITextField
alloc]init];
    pwd.frame =
CGRectMake(120,
240, 200,
40);
    pwd.borderStyle =
UITextBorderStyleRoundedRect;
    pwd.font = [UIFont
systemFontOfSize:20];
    pwd.clearButtonMode =
UITextFieldViewModeAlways;
    
    pwd.secureTextEntry =
YES;
 //这个方法一开始你一定很困惑,这个有什么用,这里就不告诉你了,你自己调试下程序,你就能发现其中的秘密
    pwd.clearsOnBeginEditing =YES;
    //调用代理协议方法--这个还一时不好说,等到后面我会抽一节来细细讲,现在就先用着(对了最上面的不要忘记添加协议方法就是那个<  >里面的)
    pwd.delegate =
self;
    [self.window
addSubview:pwd];
    
    
}
#pragma mark - 创建注册登录
-(void)createButtons
{
//这里是用数组的方法,你学过oc就知道了,数组
    NSArray *array =
@[@"注册",@"登录"];
    for (int i =
0; i<array.count; i++) {
        UIButton *btn = [UIButton
buttonWithType:UIButtonTypeSystem];
        btn.frame =
CGRectMake(70 + i *100,
550, 155,
40);
        [btn setTitle:array[i]
forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont
systemFontOfSize:20];
        btn.tag =
100+i;
        [self.window
addSubview:btn];
    }
}

-(void)createLabels
{
    NSArray *array =
@[@"用户名:",@"密码:"];
    for (int i =
0; i<array.count; i++) {
        UILabel *label = [[UILabel
alloc]init];
        label.frame =
CGRectMake(50,
120+i*120,
65, 40);
        
        label.text = array[i];
        label.font = [UIFont
systemFontOfSize:19];
        [self.window
addSubview:label];
    }
}
//这里就是在上面调用的方法了,监听用户点击事件,当用户点击屏幕的时候,就会把键盘回收,我用的是最简单的方法,还有2个同样功能的方法我这里就不写了,这样是取消成为第一响应者方式的一种,如果返回yes,textfield自动成为第一响应者,当textfield进入编辑模式,并且弹出键盘。这是在UITextField里面的方法
-(void)ctlClick
{
    [self.window
endEditing:YES];
}
@end











内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息