登錄注冊UI ---- 1

轉(zhuǎn)載自

iOS登錄界面和注冊界面

一伊履、登錄和注冊界面實現(xiàn)效果圖:

一韩容、實現(xiàn)原理

1、輸入框的實現(xiàn)原理:把兩個無邊框的UITextField 添加到一個作為背景的UIView上唐瀑,然后重寫UIView的: -(void)drawRect:(CGRect)rect 方法群凶,用Quartz2D畫出中間的那條分隔線。然后再設(shè)置UIView為帶邊框的圓角就可以實現(xiàn)上面的效果了哄辣。

注意:為了方便把textField添加到背景View上座掘,然后設(shè)置textField的frame值時根據(jù)view的frame值計算更簡單些


二、直接上代碼

1\新建一個BasicTextField 類 繼承自UITextField

BasicTextField.h文件:

#import <UIKit/UIKit.h>

@interface BasicTextField:UITextField

@end

BasicTextField.m文件

#import "BasicTextField.h"

@implementation BasicTextField

// 重寫 leftView的X 值

-(CGRect)leftViewRectForbounds:(CGRect)bounds{

? ? ? ?CGRect iconRect = [super leftViewRectForBounds:bounds];

? ? ? ?iconRect.origin.x += 10;

? ? ? return iconRect;

// 重寫占位符的x值

-(CGRect)placeholderRectForBounds:(CGRect)bounds{

? ? ? ? CGRect placeholderRect = [super placeholderRectForBounds:bounds];

? ? ? ?placeholderRect.origin.x ?+= 1;

? ? ? ? return placeholderRect;

// 重寫文字輸入時的x值

- (CGRect)editingRectForBounds:(CGRect)bounds{

CGRect editingRect = [super editingRectForBounds:bounds];

editingRect.origin.x += 20;

return editingRect;

}

// 重寫文字顯示時的x值

-(CGRect)textRectForBounds:(CGRect)bounds{?

CGRect textRect = [super editingRectForBounds:bounds];

textRect.origin.x += 20;

return textRect;

}

@end


2柔滔、新建LoginBackgroundView類繼承自UIView,作為textField的輸入框的背景溢陪。

LoginBackGroundView.h文件:

#import<UIKit/UIKit.h>

@interface LoginBackgroundView:UIView

@end

LoginBackGroundView.m文件

// 重寫此方法,用Quartz2D畫出中間分隔線

-(void)drawRect:(CGRect)rect{

CGRectContextref context = UIGraphicsGetCurrentConetext();

CGContextSetLineWidth(context,0.7);

CGContextBeginPath(path);

CGContextMoveToPoint(context,0,40);

CGContextAddLineToPoint(context,self.frame.size.width,40);

CGContextClosePath(context);

[mianBodyColor(207,207,207)setStroke];

CGContextStrokePath(context);

3睛廊、新建LoginView類繼承自UIView,把這個LoginView設(shè)置為控制器的rootView即可

LoginView.h文件

#import<UIKit/UIkit.h>

@protocol LoginViewDelegate<NSObject>

@interface LoginView:UIView

@end

LoginView.m文件

#define textFieldColor(r,g,b) [UIColor colorWithRed:r/255.0f green"g/255.0f ?blue:b/255.0f ?alpha:1];

#define mianBodyColor(r,,g,b) [UIColor colorWithRed:r/255.0f ? green:g/255.0f ?blue:b/255.0f ?alpha:1];

#import"LoginView.h"

#import"BasicTextField.h"

#import"LoginBackgroundView.h"

@interface LoginView ()<UITextFieldDelegate>

@property (noatomic, strong) LoginBackgroundView *backgroundView;

@property(noatomic, strong) BasicTextField *userTextField;

@property(noatomic, strong) BasicTextField *passwordTextField;

@property(noatomic, strong) UIButton *loginButton;

@property (noatomic, strong) UIActivityIndicatorView *loginingActivityIndicatorView;

@property (noatomic, assign) BOOL isUserEmpty;

@property (noatomic, assign) BOOL isPasswordEmpty;

@end


@implementation LoginView

-(id)initWithFrame:(CGRect)frame {

self = [super initWithFrame];

if (self) {

[self ?addLoginBackgroundView:frame];

[self customAllButons:frame];

[self customUserTextField:self.backgoundView.frame];

[self customPasswordTextField:self.backgroundView.frame];

}

return self;

}

// 添加textField的背景view

-(void)addLoginBackgroundView:(CGRect)frame{

?CGFloat backGroundX = 30;

CGFloat backGroundY = 160;

CGFloat backGroundW = frame.size.width - ?60;

CGFloat backGroundH = 80;

self.backgroundView= [[LoginBackgroundViewalloc]initWithFrame:CGRectMake(backgroundX, backgroundY, backgroundW, backgroundH)];

[self.backgroundViewsetBackgroundColor:[UIColorwhiteColor]];

self.backgroundView.layer setCornerRadius:5.0];

self.backgroundView.layer setBorderWidth:1.0];

self.backgroundView.layer setBorderColor:textFieldColor(207,207,207).CGColor];

[selfaddSubview:self.backgroundView];

}

- (void)customAllButtons:(CGRect)frame {

//返回button

UIButton*backButton = [[UIButtonalloc]initWithFrame:CGRectMake(19,35,22,22)];

[backButtonsetBackgroundImage:[UIImageimageNamed:@"back"]forState:UIControlStateNormal];

[backButtonaddTarget:selfaction:@selector(clickTheBackButton:)forControlEvents:UIControlEventTouchDown];

[self addSubview:backButton];

//登錄button



CGFloatloginButtonX =30;

CGFloatloginButtonY =250;

CGFloatloginButtonW = frame.size.width-60;

self.loginButton= [[UIButtonalloc]initWithFrame:CGRectMake(loginButtonX, loginButtonY, loginButtonW,40)];

[self.loginButton setEnabled:No];

self.loginButton.titleLable.alpha = 0.5;

[self.loginButton,layer setCornerRadius:0.3];

[self.loginButtonsetTitle:@"登錄"forState:UIControlStateNormal];

self.loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateReserved];

[self.loginButton setBackgroundColor:mianBodyColor(133,122,250)];

[self.loginButtonaddTarget:selfaction:@selector(clickLoginButton:)forControlEvents:UIControlEventTouchDown];

[selfaddSubview:self.loginButton]

//忘記密碼

CGFloatforgetButtonW =73;

CGFloatforgetButtonX = loginButtonX + loginButtonW - forgetButtonW;

CGFloatforgetButtonY =0.916* (loginButtonY +100);

CGFloatforgetButtonH =20;

UIButton*forgetButton = [[UIButtonalloc]initWithFrame:CGRectMake(forgetButtonX, forgetButtonY, forgetButtonW, forgetButtonH)];

[forgetButtonaddTarget:selfaction:@selector(clickForgetpasswordTextFieldButton:)forControlEvents:UIControlEventTouchDown];

[forgetButtonsetTitle:@"忘記密碼?"forState:UIControlStateNormal];

[forgetButton.titleLabelsetFont:[UIFontsystemFontOfSize:14]];

[forgetButtonsetTitleColor:textFieldColor(74,74,74)forState:UIControlStateNormal];

[selfaddSubview:forgetButton];

}

- (void)customUserTextField:(CGRect)frame{

self.userTextField= [[BasicTextFieldalloc]initWithFrame:CGRectMake(0,0, frame.size.width,40)];

self.userTextField.keyboardType=UIKeyboardTypeNumberPad;

self.userTextField.delegate=self;

self.userTextField.tag=7;

self.userTextField.placeholder=@"請輸入賬號";

[self.userTextFieldsetFont:[UIFontsystemFontOfSize:14]];

UIImageView*userTextFieldImage = [[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"userIcon"]];

self.userTextField.leftView= userTextFieldImage;

self.userTextField.leftViewMode=UITextFieldViewModeAlways;

self.userTextField.clearButtonMode=UITextFieldViewModeAlways;

[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(userTextFieldDidChange)name:UITextFieldTextDidChangeNotificationobject:self.userTextField];

self.isPasswordEmpty=YES;

[self.backgroundViewaddSubview:self.userTextField];

}

- (void)customPasswordTextField:(CGRect)frame{

self.passwordTextField= [[BasicTextFieldalloc]initWithFrame:CGRectMake(0,40, frame.size.width,40)];

self.passwordTextField.delegate=self;

self.passwordTextField.tag=11;

self.passwordTextField.placeholder=@"請輸入密碼";

[self.passwordTextFieldsetFont:[UIFontsystemFontOfSize:14]];

UIImageView*passwordTextFieldImage = [[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"passwordIcon"]];

self.passwordTextField.leftView= passwordTextFieldImage;

self.passwordTextField.leftViewMode=UITextFieldViewModeAlways;

self.passwordTextField.clearButtonMode=UITextFieldViewModeAlways;

self.passwordTextField.secureTextEntry=YES;

//設(shè)置監(jiān)聽

[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(passwordTextFieldDidChange)name:UITextFieldTextDidChangeNotificationobject:self.passwordTextField];

self.isUserEmpty=YES;

[self.backgroundViewaddSubview:self.passwordTextField];

}

- (void)addLogioningActivityIndicatorView{

CGFloatlogioningActivityIndicatorViewX =self.loginButton.frame.origin.x+80;

CGFloatlogioningActivityIndicatorViewY =self.loginButton.frame.origin.y;

CGFloatlogioningActivityIndicatorViewWH =self.loginButton.frame.size.height;

self.logioningActivityIndicatorView= [[UIActivityIndicatorViewalloc]initWithFrame:CGRectMake(logioningActivityIndicatorViewX, logioningActivityIndicatorViewY, logioningActivityIndicatorViewWH, logioningActivityIndicatorViewWH)];

[selfaddSubview:self.logioningActivityIndicatorView];

}

- (void)clickLoginButton:(id)sender{

[self.loginButtonsetTitle:@"登錄中..."forState:UIControlStateNormal];

[selfaddLogioningActivityIndicatorView];

[self.logioningActivityIndicatorViewstartAnimating];

//當(dāng)點擊登錄按鈕時形真,賬號和密碼輸入框放棄第一響應(yīng)者,此時鍵盤退出

[self.userTextFieldresignFirstResponder];

[self.passwordTextFieldresignFirstResponder];

}

- (void)clickForgetpasswordTextFieldButton:(id)sender{

//點擊忘記密碼button后需要執(zhí)行的代碼

}

- (void)clickTheBackButton:(id)sender{

//點擊左上角圈叉按鈕返回上一界面

}

#pragma makr --UITextFieldDelegate

//UITextField的代理方法超全,點擊鍵盤return按鈕退出鍵盤

- (BOOL)textFieldShouldReturn:(UITextField*)textField{

[self.passwordTextFieldresignFirstResponder];

returnYES;

}

//此處為userTextField的監(jiān)聽方法咆霜,后面會細講,主要是實時監(jiān)聽textField值的變化

- (void)userTextFieldDidChange{

if(self.userTextField.text.length>0) {

UIImageView*loginTextFieldImage = [[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"userIconEdited"]];

self.userTextField.leftView= loginTextFieldImage;

self.isUserEmpty=NO;

if(self.isPasswordEmpty==NO) {

self.loginButton.titleLabel.alpha=1;

[self.loginButtonsetEnabled:YES];

}

}else{

UIImageView*loginTextFieldImage = [[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"userIcon"]];

self.userTextField.leftView= loginTextFieldImage;

[self.loginButtonsetTitle:@"登錄"forState:UIControlStateNormal];

self.loginButton.titleLabel.alpha=0.5;

[self.loginButtonsetEnabled:NO];

self.isUserEmpty=YES;

[self.logioningActivityIndicatorViewstopAnimating];

}

}

//passwordTextField的監(jiān)聽方法

- (void)passwordTextFieldDidChange{

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

self.isPasswordEmpty=NO;

UIImageView*loginTextFieldImage = [[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"passwordIconEdited"]];

self.passwordTextField.leftView= loginTextFieldImage;

if(self.isUserEmpty==NO){

self.loginButton.titleLabel.alpha=1;

[self.loginButtonsetEnabled:YES];

}

}else{

self.isPasswordEmpty=YES;

UIImageView*loginTextFieldImage = [[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"passwordIcon"]];

self.passwordTextField.leftView= loginTextFieldImage;

[self.loginButtonsetTitle:@"登錄"forState:UIControlStateNormal];

self.loginButton.titleLabel.alpha=0.5;

[self.loginButtonsetEnabled:NO];

[self.logioningActivityIndicatorViewstopAnimating];

}

}

//點擊界面空白處退出鍵盤

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event{

[self.userTextFieldresignFirstResponder];

[self.passwordTextFieldresignFirstResponder];

}

@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末嘶朱,一起剝皮案震驚了整個濱河市蛾坯,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌疏遏,老刑警劉巖脉课,帶你破解...
    沈念sama閱讀 212,816評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異财异,居然都是意外死亡倘零,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,729評論 3 385
  • 文/潘曉璐 我一進店門戳寸,熙熙樓的掌柜王于貴愁眉苦臉地迎上來呈驶,“玉大人,你說我怎么就攤上這事疫鹊⌒湔埃” “怎么了司致?”我有些...
    開封第一講書人閱讀 158,300評論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長聋迎。 經(jīng)常有香客問我脂矫,道長,這世上最難降的妖魔是什么砌庄? 我笑而不...
    開封第一講書人閱讀 56,780評論 1 285
  • 正文 為了忘掉前任,我火速辦了婚禮奕枢,結(jié)果婚禮上娄昆,老公的妹妹穿的比我還像新娘。我一直安慰自己缝彬,他們只是感情好萌焰,可當(dāng)我...
    茶點故事閱讀 65,890評論 6 385
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著谷浅,像睡著了一般扒俯。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上一疯,一...
    開封第一講書人閱讀 50,084評論 1 291
  • 那天撼玄,我揣著相機與錄音,去河邊找鬼墩邀。 笑死掌猛,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的眉睹。 我是一名探鬼主播荔茬,決...
    沈念sama閱讀 39,151評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼竹海!你這毒婦竟也來了慕蔚?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,912評論 0 268
  • 序言:老撾萬榮一對情侶失蹤斋配,失蹤者是張志新(化名)和其女友劉穎孔飒,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體艰争,經(jīng)...
    沈念sama閱讀 44,355評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡十偶,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,666評論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了园细。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片惦积。...
    茶點故事閱讀 38,809評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖猛频,靈堂內(nèi)的尸體忽然破棺而出狮崩,到底是詐尸還是另有隱情蛛勉,我是刑警寧澤,帶...
    沈念sama閱讀 34,504評論 4 334
  • 正文 年R本政府宣布睦柴,位于F島的核電站诽凌,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏坦敌。R本人自食惡果不足惜侣诵,卻給世界環(huán)境...
    茶點故事閱讀 40,150評論 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望狱窘。 院中可真熱鬧杜顺,春花似錦、人聲如沸蘸炸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,882評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽搭儒。三九已至穷当,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間淹禾,已是汗流浹背馁菜。 一陣腳步聲響...
    開封第一講書人閱讀 32,121評論 1 267
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留铃岔,地道東北人火邓。 一個月前我還...
    沈念sama閱讀 46,628評論 2 362
  • 正文 我出身青樓,卻偏偏與公主長得像德撬,于是被迫代替她去往敵國和親铲咨。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 43,724評論 2 351

推薦閱讀更多精彩內(nèi)容