关于IOS登录实现

        

       终于开始正式的实现功能了,常用的基本上都接触过了,现在就是运用,实现.下面来看看我的成果吧! 首先,需要画界面,因为IOS图形化特别严重,所以我们首先要做的是页面布局,下面来看看我的页面布局吧:



     这个界面主要分三块,上面的用户名和密码用一个View加载,下面是两个按钮,包括登录和忘记密码两个按钮,最下面的是第三方登录.最上面的导航栏与下面的tabar 这是之前写好的.下面来先看看我的界面代码吧:

  

<span style="font-size:18px;"> /*获取屏幕大小*/
    float width = [[UIScreen mainScreen] bounds].size.width;
    float height = [[UIScreen mainScreen] bounds].size.height;
    /*创建一个view,来承载注册信息控件*/
    UIView *infoView = [[UIView alloc]initWithFrame:CGRectMake(0, height/10+20, width, height/5)];
    infoView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:infoView];
    
    UIImageView *userImg = [[UIImageView alloc]initWithFrame:CGRectMake(width/20+10, height/16-15, 25, 25)];
    userImg.layer.borderColor = [UIColor whiteColor].CGColor;
    [userImg setImage:[UIImage imageNamed:@"user"]];
    [infoView addSubview:userImg];
    
    //用户名文本框
    self.user = [[UITextField alloc]initWithFrame:CGRectMake(width/8+30, height/16-25, 200, 50)];
    self.user.placeholder = @"用户名/手机号";
    //_userName = user.text;
    [infoView addSubview:self.user];
    
    
    /*添加一条线*/
    UIView *line1 = [[UIView alloc]initWithFrame:CGRectMake(15, height/9-10, width-30, 1)];
    line1.backgroundColor =[UIColor grayColor];
    [infoView addSubview:line1];

    /*添加锁图片*/
    UIImageView *passWord = [[UIImageView alloc]initWithFrame:CGRectMake(width/20+10, height/16+35, 25, 25)];
    [passWord setImage:[UIImage imageNamed:@"password.png"]];
    [infoView addSubview:passWord];
    /*添加密码文本框*/
    self.password = [[UITextField alloc]initWithFrame:CGRectMake(width/8+30, height/16+35, 200, 30)];
    self.password.placeholder = @"密码";
    //password = password.text;
    [infoView addSubview:self.password];

    /*添加登录按钮*/
    UIButton *loginBtn = [[UIButton alloc]initWithFrame:CGRectMake(width/10, height/3+30, width-width/5, 40)];
    //loginBtn.backgroundColor = [UIColor redColor];
    [loginBtn setImage:[UIImage imageNamed:@"login"] forState:UIControlStateNormal];
    [loginBtn addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:loginBtn];
    //忘记密码按钮
    //UIButton *forget= [[UIButton alloc]initWithFram:CGRectMake(width/10, height/10+80, 60, 30)];
    UIButton *forget = [[UIButton alloc]initWithFrame:CGRectMake(width/10, height/3+80, 80, 15)];
    //[[forget setImage:[UIImage imageNamed:@"forget"]forState:UIControlStateNormal]]];
    [forget setImage:[UIImage imageNamed:@"forget"] forState:UIControlStateNormal];
    [forget addTarget:self action:@selector(forgetPassword) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:forget];
    //添加第二条线
    UIView *line3 = [[UIView alloc]initWithFrame:CGRectMake(0, height-height/3-35, width/3, 1)];
    line3.backgroundColor =[UIColor grayColor];
    [self.view addSubview:line3];
    
    /*添加第三方入口*/
    UILabel *otherExtrence =[[UILabel alloc]initWithFrame:CGRectMake(width/3, height-height/3-50, 150, 30)];
    [otherExtrence setText:@"   或第三方登录"];
    otherExtrence.font = [UIFont boldSystemFontOfSize:14.0f];
    otherExtrence.textColor = [UIColor grayColor];
    [self.view addSubview:otherExtrence];
    
    /*添加第三条线*/
    
    UIView *line4 = [[UIView alloc]initWithFrame:CGRectMake(width-width/3, height-height/3-35, width/3, 1)];
    line4.backgroundColor =[UIColor grayColor];
    [self.view addSubview:line4];
    
    /*添加第三方入口*/
    /*微信入口*/
    
    UIButton *weChat = [[UIButton alloc]initWithFrame:CGRectMake(width/5-5, height-height/4-40, 50, 50)];
    [weChat setImage:[UIImage imageNamed:@"weChat"] forState:UIControlStateNormal];
    [weChat addTarget:self action:@selector(weChatLogin) forControlEvents:UIControlEventTouchUpInside];

    
    [self.view addSubview:weChat];
    
    
    UILabel *weChatLabel = [[UILabel alloc]initWithFrame:CGRectMake(width/5-5, height-height/4, 50, 30)];
    [weChatLabel setText:@"微信登录"];
    weChatLabel.font=[UIFont boldSystemFontOfSize:12.0f];
    [weChatLabel setTextColor:[UIColor grayColor]];
    [self.view addSubview:weChatLabel];
    
    
    /*微博登陆*/
    
    UIButton *weBlog = [[UIButton alloc]initWithFrame:CGRectMake(width/2-20, height-height/4-40, 50, 50)];
    [weBlog setImage:[UIImage imageNamed:@"weBlog"]forState:UIControlStateNormal];
    [weBlog addTarget:self action:@selector(weBlogLogin) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:weBlog];

    
    UILabel *weBlogLabel = [[UILabel alloc]initWithFrame:CGRectMake(width/2-20, height-height/4, 50, 30)];
    [weBlogLabel setText:@"微博登录"];
    weBlogLabel.font = [UIFont boldSystemFontOfSize:12.0f];
    //设置字体颜色灰色
    [weBlogLabel setTextColor:[UIColor grayColor]];
    [self.view addSubview:weBlogLabel];
    
    
    /*QQ登陆*/
    
    UIButton *qq = [[UIButton alloc]initWithFrame:CGRectMake(width-width/4-20,height-height/4-40, 50, 50)];
    //[qq setImage:[UIImage imageNamed:@"qq"]forState:UIControlStateNormal];
    [qq setImage:[UIImage imageNamed:@"QQ"] forState:UIControlStateNormal];
    [qq addTarget:self action:@selector(qqLogin) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:qq];
    

    
    UILabel *qqLabel = [[UILabel alloc]initWithFrame:CGRectMake(width-width/4-20, height-height/4, 50, 30)];
    [qqLabel setText:@"QQ登陆"];
    qqLabel.font = [UIFont boldSystemFontOfSize:12.0f];
    [qqLabel setTextColor:[UIColor grayColor]];
    [self.view addSubview:qqLabel];
    
    //加载注册
    [self loadRegister];
</span>
        

      这段代码主要是对于主界面的各种控件的加载,布局,下面来看看我的实现登录功能的代码,需要调别人的接口:

<span style="font-size:18px;"> NSString *strURL = LOGIN;
        //进行编码
    strURL =[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    //申请管理队列
    AFHTTPRequestOperationManager *manager_login = [AFHTTPRequestOperationManager manager];
    //请求对象和响应对象
    manager_login.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager_login.requestSerializer = [AFHTTPRequestSerializer serializer];
    
    manager_login.requestSerializer.timeoutInterval =5;
    //放置在字典表里面的参数
    NSDictionary *parameter = @{@"username":self.user.text,
                              @"password":self.password.text};
    //提交注册信息
    [manager_login POST:strURL parameters:parameter constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        NSLog(@"123456");
        
    }
     //设置提交成功后的操作
        success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        
        NSLog(@"返回数据:%@",str);

        
    }
        //提交失败的操作!
        failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        MBProgressHUD *hud =
        [MBProgressHUD showHUDAddedTo:self.view
                             animated:YES];
        hud.labelText = @"网络开小差啦~";
        hud.dimBackground = NO;
        hud.mode = MBProgressHUDModeText;
        hud.animationType = MBProgressHUDAnimationFade;
        [hud hide:YES afterDelay:1.2];
//        [self.loadView.header endRefreshing];
        self.user.text = @"";
        self.password.text = @"";
    }];
    }
    //登录,页面跳转
    MeViewController *MVC = [[MeViewController alloc] init];
    [self.navigationController pushViewController:MVC animated:YES];
    [ShareData sharedShareData].isLogin = YES;</span>

总结:

        整体上来看,常规的一些还是比较简单的,最难得是各种动画的实现,特别多,任重而道远啊,继续加油!

     

你可能感兴趣的:(关于IOS登录实现)