支付類應(yīng)用通常都需要輸入6位支付密碼,有的是用 AlertView 的方式實(shí)現(xiàn)的运挫,例如在 iPhone的 App Store 中下載應(yīng)用侵俗,有時(shí)會(huì)彈窗讓你輸入密碼,界面大概是這樣的:
還有一種類似這樣的痛倚,把輸入框放在單獨(dú)的頁面上:
支付寶微信做的是類似半模態(tài)視圖的界面(下面的是淘票票的輸入密碼界面):
網(wǎng)上搜尋了許久,找了個(gè)最接近第三種方式的 Demo :
CYPasswordView_Block 源碼解析
一耸峭、概述
CYPasswordView_Block 是一個(gè)模仿支付寶輸入支付密碼的密碼框(這是一個(gè)用block傳遞事件的版本桩蓉,避免了因多級(jí)頁面切換并且都彈出密碼框而造成通知監(jiān)聽混亂的bug)
二、使用方法
- (IBAction)showPasswordView:(id)sender {
WS(weakSelf);
// 實(shí)例化 CYPasswordView 對(duì)象
self.passwordView = [[CYPasswordView alloc] init];
// 彈出密碼框
[self.passwordView showInView:self.view.window];
self.passwordView.loadingText = @"提交中...";
// 關(guān)閉密碼框
self.passwordView.cancelBlock = ^{
[weakSelf cancel];
};
// 忘記密碼
self.passwordView.forgetPasswordBlock = ^{
[weakSelf forgetPWD];
};
// 密碼輸入完成的回調(diào)
self.passwordView.finish = ^(NSString *password) {
[weakSelf.passwordView hidenKeyboard];
[weakSelf.passwordView startLoading];
CYLog(@"cy ========= 發(fā)送網(wǎng)絡(luò)請(qǐng)求 pwd=%@", password);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kRequestTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
flag = !flag;
if (flag) {
// 購買成功劳闹,跳轉(zhuǎn)到成功頁
[weakSelf.passwordView requestComplete:YES message:@"購買成功……"];
[weakSelf.passwordView stopLoading];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kDelay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[weakSelf.passwordView hide];
});
} else {
// 購買失敗触机,跳轉(zhuǎn)到失敗頁
[weakSelf.passwordView requestComplete:NO];
[weakSelf.passwordView stopLoading];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kDelay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[weakSelf.passwordView hide];
});
}
});
};
}
三帚戳、實(shí)現(xiàn)方式
需要哪些控件元素?
照著圖片看需要哪些控件元素儡首,自上而下片任,從左往右看:
- 灰色的背景圖層 coverView,用于覆蓋上一個(gè)視圖;
- 占據(jù)半個(gè)屏幕的輸入框視圖蔬胯,該視圖上有:
- 標(biāo)題文本对供;
- 關(guān)閉按鈕、忘記密碼按鈕氛濒;
- 帶6個(gè)格子的密碼輸入框产场;
- 加載圖片、加載文本
如何顯示密碼舞竿?
- 鍵盤上輸入數(shù)字時(shí)京景,并不會(huì)實(shí)時(shí)顯示123456...,而是顯示 ●●●●●●骗奖, 這是為了密碼輸入安全起見的一貫做法确徙。為 UITextField 設(shè)置
secureTextEntry
屬性為 YES≈醋溃可以禁用用戶在視圖中復(fù)制文本的功能鄙皇。 還會(huì)用* 代替輸入的字符。 - 但是原生的控件無法實(shí)現(xiàn)在一個(gè)格子里輸入一個(gè)字符仰挣,而且還要在這個(gè)格子中心顯示●伴逸,所以密碼輸入的6個(gè)方框和●都是假象,是用代碼畫上去的膘壶。
- 每次在 UITextField 中輸入错蝴、刪除數(shù)字,都要進(jìn)行判斷颓芭,根據(jù) UITextField 中文本的個(gè)數(shù)同步添加或刪除●
- 邊界情況漱竖,當(dāng)輸入的數(shù)字個(gè)數(shù)大于等于6個(gè),禁止輸入畜伐。
- 為了提高用戶體驗(yàn),當(dāng)輸入完第6個(gè)數(shù)字時(shí)躺率,自動(dòng)關(guān)閉鍵盤玛界,發(fā)起網(wǎng)絡(luò)請(qǐng)求以驗(yàn)證密碼。
CYPasswordView_Block 源碼分析
CYPasswordView代碼結(jié)構(gòu)
-
CYPasswordView.bundle
圖片庫悼吱,包含所有圖片素材
-
CYConst
通用宏定義慎框、密碼框各控件的尺寸常量
-
UIView+Extension
簡(jiǎn)化設(shè)置視圖尺寸的范疇方法
-
CYPasswordInputView
密碼框的輸入背景視圖(把
password_background@2x.png
、password_textfield@2x.png
兩個(gè)背景圖片繪制上去)后添,主要包含標(biāo)題笨枯、關(guān)閉按鈕、忘記密碼按鈕、以及繪制6個(gè)●的操作馅精。 -
CYPasswordView
包含蒙版圖層严嗜、CYPasswordInputView、UITextField 控件洲敢、再添加加載圖片漫玄、加載文字標(biāo)簽、處理判斷輸入邏輯压彭、加載顯示旋轉(zhuǎn)動(dòng)畫等
前三個(gè)不多說了睦优,主要看后兩個(gè)
CYPasswordInputView
在指定初始化方法 initWithFrame:
中添加 關(guān)閉按鈕 和 忘記密碼按鈕,然后在 layoutSubviews
方法中對(duì)這兩個(gè)控件重新布局,按鈕的 Target-Action 點(diǎn)擊事件通過 Blocks 傳遞壮不。
#pragma mark - 生命周期方法
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor clearColor];
/** 添加子控件 */
[self setupSubViews];
}
return self;
}
/** 添加子控件 */
- (void)setupSubViews {
/** 關(guān)閉按鈕 */
UIButton *btnCancel = [UIButton buttonWithType:UIButtonTypeCustom];
[self addSubview:btnCancel];
[btnCancel setBackgroundImage:[UIImage imageNamed:CYPasswordViewSrcName(@"password_close")] forState:UIControlStateNormal];
[btnCancel setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
self.btnClose = btnCancel;
[self.btnClose addTarget:self action:@selector(btnClose_Click:) forControlEvents:UIControlEventTouchUpInside];
/** 忘記密碼按鈕 */
UIButton *btnForgetPWD = [UIButton buttonWithType:UIButtonTypeCustom];
[self addSubview:btnForgetPWD];
[btnForgetPWD setTitle:@"忘記密碼汗盘?" forState:UIControlStateNormal];
[btnForgetPWD setTitleColor:CYColor(0, 125, 227) forState:UIControlStateNormal];
btnForgetPWD.titleLabel.font = CYFont(13);
[btnForgetPWD sizeToFit];
self.btnForgetPWD = btnForgetPWD;
[self.btnForgetPWD addTarget:self action:@selector(btnForgetPWD_Click:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)layoutSubviews {
[super layoutSubviews];
// 設(shè)置關(guān)閉按鈕的坐標(biāo)
self.btnClose.width = CYPasswordViewCloseButtonWH;
self.btnClose.height = CYPasswordViewCloseButtonWH;
self.btnClose.x = CYPasswordViewCloseButtonMarginLeft;
self.btnClose.centerY = CYPasswordViewTitleHeight * 0.5;
// 設(shè)置忘記密碼按鈕的坐標(biāo)
self.btnForgetPWD.x = CYScreenWidth - (CYScreenWidth - CYPasswordViewTextFieldWidth) * 0.5 - self.btnForgetPWD.width;
self.btnForgetPWD.y = CYPasswordViewTitleHeight + CYPasswordViewTextFieldMarginTop + CYPasswordViewTextFieldHeight + CYPasswordViewForgetPWDButtonMarginTop;
}
// 按鈕點(diǎn)擊
- (void)btnClose_Click:(UIButton *)sender {
if (self.cancelBlock) {
self.cancelBlock();
}
[self.inputNumArray removeAllObjects];
}
- (void)btnForgetPWD_Click:(UIButton *)sender {
if (self.forgetPasswordBlock) {
self.forgetPasswordBlock();
}
}
在 drawRect:
方法中把圖片畫在圖層上,標(biāo)題也并不是 Label 控件询一,而是畫上去的字符串隐孽,6個(gè)●也同時(shí)被畫上去了,它是遍歷數(shù)組中元素的個(gè)數(shù)畫上去的家凯。
所以說缓醋,每次在輸入框中刪除、添加數(shù)字绊诲,都會(huì)通過調(diào)用 [self setNeedsDisplay]
將 drawRect:
方法中的所有視圖重新繪制一遍送粱,目的是需要遍歷數(shù)組,重畫●
- (void)drawRect:(CGRect)rect {
// 畫背景視圖
UIImage *imgBackground =
[UIImage imageNamed:CYPasswordViewSrcName(@"password_background")];
[imgBackground drawInRect:rect];
// 畫輸入框
CGFloat textfieldY = CYPasswordViewTitleHeight + CYPasswordViewTextFieldMarginTop;
CGFloat textfieldW = CYPasswordViewTextFieldWidth;
CGFloat textfieldX = (CYScreenWidth - textfieldW) * 0.5;
CGFloat textfieldH = CYPasswordViewTextFieldHeight;
UIImage *imgTextfield =
[UIImage imageNamed:CYPasswordViewSrcName(@"password_textfield")];
[imgTextfield drawInRect:CGRectMake(textfieldX, textfieldY, textfieldW, textfieldH)];
// 畫標(biāo)題
NSString *title = (self.title ? self.title : @"輸入交易密碼");
NSDictionary *arrts = @{NSFontAttributeName:CYFontB(18)};
CGSize size = [title boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:arrts
context:nil].size;
CGFloat titleW = size.width;
CGFloat titleH = size.height;
CGFloat titleX = (self.width - titleW) * 0.5;
CGFloat titleY = (CYPasswordViewTitleHeight - titleH) * 0.5;
CGRect titleRect = CGRectMake(titleX, titleY, titleW, titleH);
NSMutableDictionary *attr = [NSMutableDictionary dictionary];
attr[NSFontAttributeName] = CYFontB(18);
attr[NSForegroundColorAttributeName] = CYColor(102, 102, 102);
[title drawInRect:titleRect withAttributes:attr];
// 畫點(diǎn)
UIImage *pointImage =
[UIImage imageNamed:CYPasswordViewSrcName(@"password_point")];
CGFloat pointW = CYPasswordViewPointnWH;
CGFloat pointH = CYPasswordViewPointnWH;
CGFloat pointY = textfieldY + (textfieldH - pointH) * 0.5;
__block CGFloat pointX;
// 一個(gè)格子的寬度
CGFloat cellW = textfieldW / kNumCount;
CGFloat padding = (cellW - pointW) * 0.5;
// 根據(jù)數(shù)組中元素的個(gè)數(shù)畫黑點(diǎn)
[self.inputNumArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
pointX = textfieldX + (2 * idx + 1) * padding + idx * pointW;
[pointImage drawInRect:CGRectMake(pointX, pointY, pointW, pointH)];
}];
}
首先來看在類擴(kuò)展中聲明的可變數(shù)組:
/** 保存用戶輸入的數(shù)字集合 */
@property (nonatomic, strong) NSMutableArray *inputNumArray;
響應(yīng)輸入的兩個(gè)方法
#pragma mark - 私有方法
// 響應(yīng)用戶按下刪除鍵事件
- (void)deleteNumber {
[self.inputNumArray removeLastObject]; // 如果是刪除鍵掂之,就移除數(shù)組中的最后一個(gè)數(shù)字
[self setNeedsDisplay]; // 根據(jù)數(shù)組的個(gè)數(shù)重繪整個(gè)視圖
}
// 響應(yīng)用戶按下數(shù)字鍵事件
- (void)number:(NSDictionary *)userInfo {
NSString *numObj = userInfo[CYPasswordViewKeyboardNumberKey];
if (numObj.length >= kNumCount) return; // ?? 這行代碼應(yīng)該是永遠(yuǎn)不會(huì)執(zhí)行的吧抗俄。等會(huì)再說為啥
[self.inputNumArray addObject:numObj]; // 這里每次輸入一個(gè)數(shù)字,就會(huì)把該數(shù)字存進(jìn)數(shù)組中
[self setNeedsDisplay]; // 根據(jù)數(shù)組的個(gè)數(shù)重繪整個(gè)視圖
}
其實(shí)可以不用重繪整個(gè)視圖世舰,因?yàn)橹皇侵刂眯A點(diǎn)动雹,可以重繪指定區(qū)域視圖:setNeedsDisplayInRect:
// 響應(yīng)用戶按下刪除鍵事件
- (void)deleteNumber {
[self.inputNumArray removeLastObject];
[self setNeedsDisplayInRect:[self textFieldRect]];
}
// 響應(yīng)用戶按下數(shù)字鍵事件
- (void)number:(NSDictionary *)userInfo {
NSString *numObj = userInfo[CYPasswordViewKeyboardNumberKey];
if (numObj.length >= kNumCount) return;
[self.inputNumArray addObject:numObj];
[self setNeedsDisplayInRect:[self textFieldRect]];
}
- (CGRect)textFieldRect {
CGFloat textfieldY = CYPasswordViewTitleHeight + CYPasswordViewTextFieldMarginTop;
CGFloat textfieldW = CYPasswordViewTextFieldWidth;
CGFloat textfieldX = (CYScreenWidth - textfieldW) * 0.5;
CGFloat textfieldH = CYPasswordViewTextFieldHeight;
return CGRectMake(textfieldX, textfieldY, textfieldW, textfieldH);
}
CYPasswordView
再看 CYPasswordView 這個(gè)類,指定初始化方法中添加了幾個(gè)子視圖跟压、蒙版胰蝠、輸入框、響應(yīng)者 UITextField震蒋、加載圖片和加載文字茸塞、單擊手勢(shì)。布局子視圖方法 layoutSubviews
中重新布局了加載圖片和加載文字標(biāo)簽的位置查剖。
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:CYScreen.bounds]; // frame 是固定的
if (self) {
[self setupUI];
}
return self;
}
- (void)setupUI {
self.backgroundColor = [UIColor clearColor];
// 蒙版
[self addSubview:self.coverView];
// 輸入框
[self addSubview:self.passwordInputView];
// 響應(yīng)者
[self addSubview:self.txfResponsder];
[self.passwordInputView addSubview:self.imgRotation];
[self.passwordInputView addSubview:self.lblMessage];
// 手勢(shì)
[self addGestureRecognizer:[[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(tap:)]];
}
- (void)layoutSubviews {
[super layoutSubviews];
// 調(diào)整旋轉(zhuǎn)圖片布局
self.imgRotation.centerX = self.passwordInputView.centerX;
self.imgRotation.centerY = self.passwordInputView.height * 0.5;
// 調(diào)整提示文本布局
self.lblMessage.x = 0;
self.lblMessage.y = CGRectGetMaxY(self.imgRotation.frame) + 20;
self.lblMessage.width = CYScreenWidth;
self.lblMessage.height = 30;
}
- ① 添加單擊手勢(shì)的作用:點(diǎn)擊輸入框钾虐,彈出鍵盤
- (void)tap:(UITapGestureRecognizer *)recognizer {
// 獲取點(diǎn)擊的坐標(biāo)位置
CGPoint p = [recognizer locationInView:self.passwordInputView];
// 6塊矩形輸入框區(qū)域
CGRect f = CGRectMake(39, 80, 297, 50);
// 判斷點(diǎn)擊區(qū)域是否包含在輸入框區(qū)域中
if (CGRectContainsPoint(f, p)) {
[self.txfResponsder becomeFirstResponder];
} else {
NSLog(@"==============");
}
}
因?yàn)橐憫?yīng)點(diǎn)擊事件,所以蒙版是一個(gè) UIControl
- (UIControl *)coverView {
if (_coverView == nil) {
_coverView = [[UIControl alloc] init];
[_coverView setBackgroundColor:[UIColor blackColor]];
_coverView.alpha = 0.4;
_coverView.frame = self.bounds;
}
return _coverView;
}
- ② 為何要手動(dòng)通過點(diǎn)擊事件彈出鍵盤笋庄,而不是點(diǎn)擊 UITextField 自動(dòng)彈出鍵盤 效扫?
// 這里的 UITextField 實(shí)例對(duì)象就像名字一樣倔监,只是作為一個(gè)響應(yīng)者,
// 它的 frame 是 CGRectMake(0, 0, 1, 1)
// 優(yōu)化點(diǎn):其實(shí)還是可以把它設(shè)置得跟圖片一樣大的菌仁,只要把它設(shè)置為透明就行了浩习,背景色、text掘托、tintColor瘦锹、通通設(shè)置為透明色,1.可以充分利用控件本身的特性闪盔。2.不用手動(dòng)添加點(diǎn)擊事件弯院。
- (UITextField *)txfResponsder {
if (_txfResponsder == nil) {
_txfResponsder = [[UITextField alloc] init];
_txfResponsder.delegate = self;
_txfResponsder.keyboardType = UIKeyboardTypeNumberPad;
_txfResponsder.frame = CGRectMake(0, 0, 1, 1);
_txfResponsder.secureTextEntry = YES;
}
return _txfResponsder;
}
這個(gè)類中實(shí)現(xiàn)取消按鈕和忘記密碼按鈕依舊是通過 Block 的方式傳遞,只是增加了重置密碼框操作
- (CYPasswordInputView *)passwordInputView {
WS(weakself);
if (_passwordInputView == nil) {
_passwordInputView = [[CYPasswordInputView alloc] init];
_passwordInputView.cancelBlock = ^{
[weakself cancel];
};
_passwordInputView.forgetPasswordBlock = ^{
[weakself forgetPassword];
};
}
return _passwordInputView;
}
/** 輸入框的取消按鈕點(diǎn)擊 */
- (void)cancel {
[self resetPasswordView];
if (self.cancelBlock) {
self.cancelBlock();
}
}
/** 輸入框的忘記密碼按鈕點(diǎn)擊 */
- (void)forgetPassword {
[self resetPasswordView];
if (self.forgetPasswordBlock) {
self.forgetPasswordBlock();
}
}
/** 重置密碼框 */
- (void)resetPasswordView {
[self hidenKeyboard:^(BOOL finished) {
self.passwordInputView.hidden = YES;
tempStr = nil;
[self removeFromSuperview];
[self hidenKeyboard:nil];
[self.passwordInputView setNeedsDisplay];
}];
}
處理鍵盤輸入的 UITextFieldDelegate
協(xié)議方法:
#pragma mark - 常量區(qū)
// 前面的角落里聲明了一個(gè)變量用戶保存密碼字符串
static NSString *tempStr;
//...
#pragma mark - <UITextFieldDelegate>
#pragma mark 處理字符串 和 刪除鍵
// 每當(dāng)一個(gè)字符被鍵入時(shí)泪掀,都會(huì)首先調(diào)用此方法听绳,詢問是否應(yīng)該將輸入的字符添加進(jìn) TextField 中。
// 因此調(diào)用該方法時(shí)异赫,正被輸入的字符實(shí)際上還沒有被添加進(jìn) TextField 中
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (!tempStr) {
tempStr = string;
}else{
// 把正在輸入的數(shù)字和之前所有輸入的數(shù)字拼接起來
// 因此 tempStr 字符串中存放的是所有的數(shù)字
tempStr = [NSString stringWithFormat:@"%@%@",tempStr,string];
}
// 判斷是否是刪除按鈕椅挣?
if ([string isEqualToString:@""]) {
// 響應(yīng)用戶按下刪除鍵事件,刪除小黑點(diǎn)
[self.passwordInputView deleteNumber];
// 如果是刪除按鈕塔拳,再判斷容器字符串的長(zhǎng)度鼠证?
if (tempStr.length > 0) { // 刪除最后一個(gè)字符串
NSString *lastStr = [tempStr substringToIndex:[tempStr length] - 1];
tempStr = lastStr;
}
} else{
// 如果不是刪除按鈕,先判斷容器字符串的長(zhǎng)度是否=6靠抑?
if (tempStr.length == 6) {
if (self.finish) {
self.finish(tempStr);
self.finish = nil;
}
tempStr = nil;
}
// 更新 CYPasswordInputView 中的黑點(diǎn)
// ??把當(dāng)前輸入的數(shù)字通過字典的方式傳遞,key為常量量九,value 是當(dāng)前輸入的數(shù)字
NSMutableDictionary *userInfoDict = [NSMutableDictionary dictionary];
userInfoDict[CYPasswordViewKeyboardNumberKey] = string;
[self.passwordInputView number:userInfoDict];
}
return YES;
}
??因?yàn)楫?dāng)前輸入的數(shù)字是通過鍵值對(duì)的方式發(fā)送給 passwordInputView
對(duì)象的,所以它響應(yīng)用戶按下數(shù)字的方法中:
// 響應(yīng)用戶按下數(shù)字鍵事件
- (void)number:(NSDictionary *)userInfo {
NSString *numObj = userInfo[CYPasswordViewKeyboardNumberKey];
if (numObj.length >= kNumCount) return; // ??
[self.inputNumArray addObject:numObj]; // 這里每次輸入一個(gè)數(shù)字颂碧,就會(huì)把該數(shù)字存進(jìn)數(shù)組中
[self setNeedsDisplay]; // 根據(jù)數(shù)組的個(gè)數(shù)重繪整個(gè)視圖
}
方法中的 numObj
就是當(dāng)前輸入的數(shù)字荠列,numObj.length
永遠(yuǎn)等于1,所以我說這行代碼是永遠(yuǎn)不會(huì)執(zhí)行的载城。
彈出密碼框的方法:
// 彈出密碼框
[self.passwordView showInView:self.view.window];
源碼:
- (void)showInView:(UIView *)view {
[view addSubview:self];
/** 輸入框起始frame */
self.passwordInputView.height = CYPasswordInputViewHeight;
self.passwordInputView.y = self.height;
self.passwordInputView.width = CYScreenWidth;
self.passwordInputView.x = 0;
/** 彈出鍵盤 */
[self showKeyboard];
}
/** 鍵盤彈出 */
- (void)showKeyboard {
[self.txfResponsder becomeFirstResponder];
[UIView animateWithDuration:CYPasswordViewAnimationDuration
delay:0
options:UIViewAnimationOptionTransitionNone
animations:^{
self.passwordInputView.y = (self.height - self.passwordInputView.height);
}
completion:^(BOOL finished) {
NSLog(@" ========= %@", NSStringFromCGRect(self.passwordInputView.frame));
}];
}
這段代碼添加了一個(gè)動(dòng)畫讓 passwordInputView
的 Y 點(diǎn)從:
self.height
→ (self.height - self.passwordInputView.height)
就是從底部向上推出的動(dòng)畫肌似,它是先彈出鍵盤,再彈出passwordInputView
诉瓦。
最后川队,旋轉(zhuǎn)動(dòng)畫都是對(duì)加載圖片和加載文字作了一些更改:
/**
* 開始旋轉(zhuǎn)
*/
- (void)startRotation:(UIView *)view {
_imgRotation.hidden = NO;
_imgRotation.image =
[UIImage imageNamed:CYPasswordViewSrcName(@"password_loading_b")];
_lblMessage.hidden = NO;
self.lblMessage.text = _loadingText;
CABasicAnimation* rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
rotationAnimation.duration = 2.0;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = MAXFLOAT;
[view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}
/**
* 結(jié)束旋轉(zhuǎn)
*/
- (void)stopRotation:(UIView *)view {
[view.layer removeAllAnimations];
}
- (void)startLoading {
[self startRotation:self.imgRotation];
[self.passwordInputView disEnalbeCloseButton:NO];
}
- (void)stopLoading {
[self stopRotation:self.imgRotation];
[self.passwordInputView disEnalbeCloseButton:YES];
}
- (void)requestComplete:(BOOL)state {
if (state) {
[self requestComplete:state message:@"支付成功"];
} else {
[self requestComplete:state message:@"支付失敗"];
}
}
- (void)requestComplete:(BOOL)state message:(NSString *)message {
if (state) {
// 請(qǐng)求成功
self.lblMessage.text = message;
self.imgRotation.image =
[UIImage imageNamed:CYPasswordViewSrcName(@"password_success")];
} else {
// 請(qǐng)求失敗
self.lblMessage.text = message;
self.imgRotation.image =
[UIImage imageNamed:CYPasswordViewSrcName(@"password_error")];
}
}
分析完以上源碼后,我又對(duì)該項(xiàng)目重構(gòu)并進(jìn)行了些許修改: