码迷,mamicode.com
首页 > 其他好文 > 详细

利用OC纯代码开发QQ登陆界面

时间:2016-04-14 22:03:46      阅读:432      评论:0      收藏:0      [点我收藏+]

标签:

//

//  ViewController.m

//  QQUI

//

//  Created by kevin_dfg on 16/4/12.

//  Copyright © 2016年 kevin_dfg. All rights reserved.

//

 

#import "ViewController.h"

 

//屏幕宽高

#define ScreenWidth  self.view.frame.size.width

#define ScreenHeight self.view.frame.size.height

 

//qq头像的宽高

#define  QQHeadX      (ScreenWidth-0.25*ScreenWidth)/2

#define  QQHeadY        80

#define QQHeadWidth     0.25*ScreenWidth

#define QQHeadHeight    0.25*ScreenWidth

 

//账号的宽高

#define AccountX    0

#define AccountY    QQHeadY+QQHeadHeight+20

#define AccountW    ScreenWidth

#define AccountH    50

 

//密码的宽高

#define PasswordX   0

#define PasswordY   AccountY+50

#define PasswordW   ScreenWidth

#define PasswordH   50

 

//登陆的宽高

#define LoginX   35

#define LoginY   PasswordY+PasswordH+20

#define LoginW   ScreenWidth-2*LoginX

#define LoginH   50

 

//无法登陆?

#define FailedX   5

#define FailedY   0.95*ScreenHeight

#define FailedW   80

#define FailedH   25

//新用户

#define NewX   0.8*ScreenWidth

#define NewY   0.95*ScreenHeight

#define NewW   80

#define NewH   25

                                                   //图片选择器协议

@interface ViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>

 

 

@property (nonatomic,strong)UIButton *UserImageBtn;

@property(nonatomic,strong )UITextField  *accountTextField;

@property (nonatomic,strong)UITextField *passwordTextField;

@property (nonatomic,strong)UIButton *LoginBtn;

@property (nonatomic,strong)UIButton *FailedBtn;

@property (nonatomic,strong)UIButton *NewBtn;

 

@end

 

 

 

 

 

@implementation ViewController

 

- (void)viewDidLoad {

    

    [super viewDidLoad];

    

  

    //分配初始化用户头像按钮。

    self.UserImageBtn =[[UIButton alloc] initWithFrame:CGRectMake( (ScreenWidth-0.25*self.view.frame.size.width) / 2,80,

                  0.25*self.view.frame.size.width ,0.25*self.view.frame.size.width )];

  

    //设置头像控件的图片

    [self.UserImageBtn setBackgroundImage:[UIImage imageNamed:@"login_header@2x.png"] forState:(UIControlStateNormal)];

    

    //圆形头像

    _UserImageBtn.layer.cornerRadius =QQHeadHeight/2;

    _UserImageBtn.layer.masksToBounds=YES;

    

    //添加头像控件的改变头像事件

    [_UserImageBtn addTarget:self action:@selector(setUserImageBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];

    

    //将头像控件添加到视图上

    [self.view addSubview:self.UserImageBtn];

    

    //=======================================================

    

    //分配初始化账号和密码控件

    self.accountTextField=[[UITextField alloc] initWithFrame:CGRectMake(AccountX, AccountY, AccountW, AccountH)];

     self.passwordTextField=[[UITextField alloc] initWithFrame:CGRectMake(PasswordX, PasswordY, PasswordW, PasswordH)];

    

    //设置账号和密码框控件的位置,提示字符,键盘类型

    _accountTextField.textAlignment=NSTextAlignmentCenter;

    _accountTextField.placeholder=@"QQ号码" ;

    _accountTextField.keyboardType=UIKeyboardTypeNumberPad;

    

    _passwordTextField.textAlignment=NSTextAlignmentCenter;

    _passwordTextField.placeholder=@"QQ密码" ;

    _passwordTextField.keyboardType=UIKeyboardTypeAlphabet;

    

    //设置密码密文显示

    _passwordTextField.secureTextEntry=YES;

 

    //将账号和密码控件添加到视图

    [self.view addSubview:self.accountTextField];

    [self.view addSubview:self.passwordTextField];

 

    //=======================================================

 

    

   //分配初始化登陆控件

    self.LoginBtn=[[UIButton alloc] initWithFrame:CGRectMake(LoginX, LoginY, LoginW, LoginH)];

 

    //设置登陆控件的效果图片

    [self.LoginBtn setBackgroundImage:[UIImage imageNamed:@"login_btn_blue_nor@2x.png"] forState:(UIControlStateNormal)];//正常

    [self.LoginBtn setBackgroundImage:[UIImage imageNamed:@"login_btn_blue_press@2x.png"] forState:(UIControlStateHighlighted)];//高亮

    

    //添加登陆控件的登陆事件

    [self.LoginBtn addTarget:self action:@selector(LoginBtnClickListener) forControlEvents:UIControlEventTouchUpInside];

    

    //将登陆控件添加到视图

    [self.view addSubview:self.LoginBtn];

    

    //=======================================================

 

    

    //分配初始化-无法登陆控件

    self.FailedBtn=[[UIButton alloc] initWithFrame:CGRectMake(FailedX , FailedY, FailedW, FailedH)];

  

    //设置无法登陆控件的背景图片

    [self.FailedBtn setBackgroundImage: [UIImage imageNamed:@"failed.png"] forState:(UIControlStateNormal)];

    

    //将无法登陆控件添加到视图

    [self.view addSubview:self.FailedBtn];

    

    //=======================================================

 

    //分配初始化新用户控件

    self.NewBtn=[[UIButton alloc] initWithFrame:CGRectMake(NewX, NewY, NewW, NewH)];

  

    //设置新用户控件的背景图片

    [self.NewBtn setBackgroundImage:[UIImage imageNamed:@"new.png"] forState:(UIControlStateNormal)];//正常

    

    //将新用户控件添加到视图

    [self.view addSubview:self.NewBtn];

    

}

 

 

//=======================================================

//=======================================================

//=======================================================

//=======================================================

 

 

//实现改变QQ头像事件

-(void)setUserImageBtnAction:(UIButton *)UserImageBtn{

    

    //分配初始化图片选择器控件

    UIImagePickerController *imagePicker =  [[UIImagePickerController alloc]init];

    

    //设置图片来源为系统相册

    imagePicker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;

    

    //使用图片代理-查看协议-并实现“didFinishPickingImage”事件

    imagePicker.delegate=self;

    

    //使用模态窗口显示相册

    [self  presentViewController:imagePicker animated:YES completion:nil];

}

 

//=======================================================

 

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo{

    

    //设置新头像

    [_UserImageBtn setBackgroundImage:image forState:(UIControlStateNormal)];

 

    //模态方式退出图像选择器

    [picker dismissViewControllerAnimated:YES completion:nil];

}

 

//=======================================================

 

//实现登陆控件的登陆事件

- (void)LoginBtnClickListener{

    

        if (self.accountTextField.text.length >0 && self.passwordTextField.text.length>0  ) {

            

            //实例化提示窗

            UIAlertController *alertController=[UIAlertController alertControllerWithTitle:@"温馨提示" message:@"登录中..." preferredStyle:(UIAlertControllerStyleAlert) ];

            

            //添加按钮到提示窗

            [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:nil]];

            

            //显示提示框

            [self presentViewController:alertController   animated:YES completion:nil];

 

        }else{

            

            //实例化提示窗

            UIAlertController *alertController=[UIAlertController alertControllerWithTitle:@"错误" message:@"用户密码不能为空" preferredStyle:(UIAlertControllerStyleAlert) ];

            

            //添加按钮到提示窗

            [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style: (UIAlertActionStyleDefault) handler:nil]];

 

            //显示提示框

            [self presentViewController:alertController   animated:YES completion:nil];

        }

}

 

@end

 

利用OC纯代码开发QQ登陆界面

标签:

原文地址:http://www.cnblogs.com/mitnick/p/5392693.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!