12、撥號鍵盤模塊的實現(xiàn):自定義組件

之前課程代碼:

本次教案:

提前布置:

要求學生在課下研究iPhone通訊錄的撥號頁面笤成,仔細觀察都有哪些細節(jié)實現(xiàn)

?

實現(xiàn)效果圖:

實施步驟

1、基礎UIButton實現(xiàn)自定義按鈕

2眷茁、實現(xiàn)按鈕形狀炕泳,圓形。

3蔼卡、為按鈕添加屬性(數(shù)字和字母標簽)

4喊崖、為自定義按鈕實現(xiàn)動畫

5、實現(xiàn)撥號鍵盤中的圓形按鈕布局

撥號鍵盤按鈕設計及布局分析:

自定義按鈕的實現(xiàn)其實比較簡單雇逞,只需要實現(xiàn)一個UIButton的派生類就可以了荤懂,當然也可以考慮UIControl的子類。

1塘砸、圓形按鈕的外功控制

//設置圓角半徑

self.layer.cornerRadius=self.bounds.size.width/2;

//設置子圖層蒙版

self.layer.masksToBounds=YES;

//設置按鈕的邊框寬度和顏色

self.layer.borderWidth=1.0f;

self.layer.borderColor= [[UIColordarkGrayColor]CGColor];

/*

這里CGColor其實是一個結構體节仿,UIColor的一個成員屬性

可以參考:http://www.cnblogs.com/smileEvday/archive/2012/06/05/UIColor_CIColor_CGColor.html

*/

2、動畫效果

/*

在UIKit中掉蔬,系統(tǒng)提供了animate標題打頭的屬于UIView的類方法讓我們可以輕松的制作動畫效果廊宪,每一個這樣的類方法提供了名為animations的block代碼塊,這些代碼會在方法調用后立刻或者延遲一段時間以動畫的方式執(zhí)行女轿。此外箭启,所有這些API的第一個參數(shù)都是用來設置動畫時長的。

*/

//觸摸開始蛉迹,播放前半部動畫

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

[supertouchesBegan:toucheswithEvent:event];

//在0.5秒鐘的時間內透明度由1.0->.0.2

[UIViewanimateWithDuration:0.5fanimations:^{

self.alpha=0.2f;

}];

}

//觸摸結束傅寡,播放后半段動畫

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

[supertouchesEnded:toucheswithEvent:event];

[UIViewanimateWithDuration:0.5fanimations:^{

self.alpha=1.0f;

}];

}

3、屬性標簽的實現(xiàn):

數(shù)字鍵1,只顯示一個標簽荐操,但是位置靠上

*芜抒、#、撥號鍵只顯示一個標簽托启,且居中

數(shù)字鍵1宅倒,只顯示一個標簽,但是位置靠上

總結屯耸,可以分為兩類:

有子標簽:所有數(shù)字鍵(包括“1”拐迁,可以將其看作子標簽不顯示的特殊情況): self.subTitle!=nil

沒有子標簽的:符號鍵(*、#)和撥號鍵(這里不用圖標疗绣,使用字符“call”): self.subTitle==nil

使用分支語句唠亚,兼容兩種按鈕(

if(subTitle !=nil){

//添加屬性標簽:

UILabel*mainLabel = [[UILabelalloc]initWithFrame:CGRectMake(0,0,self.bounds.size.width,self.bounds.size.height/3.0*2)];

mainLabel.text=self.mainTitle;

[mainLabelsetTextAlignment:NSTextAlignmentCenter];

[mainLabelsetTextColor:[UIColordarkGrayColor]];

[mainLabelsetFont:[UIFontfontWithName:@"Heiti SC"size:40.0f]];

[selfaddSubview:mainLabel];

//將mainLabel的中心點移至水平中線1/3點

mainLabel.center=CGPointMake(self.bounds.size.width/2,self.bounds.size.height/3.0+5);

UILabel*subLabel = [[UILabelalloc]initWithFrame:CGRectMake(0,0,self.bounds.size.width,self.bounds.size.height/3.0)];

subLabel.text=self.subTitle;

[subLabelsetTextAlignment:NSTextAlignmentCenter];

[subLabelsetTextColor:[UIColordarkGrayColor]];

//Helvetica字體是西方最常用的一種字體

[subLabelsetFont:[UIFontfontWithName:@"Helvetica"size:10.0f]];

//將subLabel的中心點移至水平中線2/3點

subLabel.center=CGPointMake(self.bounds.size.width/2.0,self.bounds.size.height/3*2);

[selfaddSubview:subLabel];

}else{

//??????????? self.titleLabel.text = self.mainTitle;

[selfsetTitle:self.mainTitleforState:UIControlStateNormal];

[selfsetTitleColor:[UIColordarkGrayColor]forState:UIControlStateNormal];

[self.titleLabelsetFont:[UIFontfontWithName:@"Heiti SC"size:40.0f]];

//??????????? [self.titleLabel setTextColor:[UIColor darkGrayColor]];

}

4、事件處理持痰,代碼同上。

老版本中需發(fā)現(xiàn)touchesBegin:和touchesEnded:方法與AddTargart:產生沖突祟蚀,比較理想的解決辦法是自己實現(xiàn)一個類似addTarget的方法工窍,這李沒有直接覆蓋是因為文檔中并沒有addTarget方法的詳細說明,所以折中自己實現(xiàn)前酿,代碼如下:

新版本中發(fā)現(xiàn)已經不存在這個問題患雏,所以可以直接使用父類中繼承的addTarget方法,因此罢维,處理機制和普通方法沒有任何區(qū)別淹仑。

本模塊項目完整代碼如下:

MPCircleButton.h

//

//? MPCircleButton.h

//仿iPhone電話App_2017

//

//? Created by GuoChunlei on 2017/6/26.

//? Copyright ? 2017年class3g. All rights reserved.

//

#import

@interfaceMPCircleButton :UIButton

//主標簽:數(shù)字、*肺孵、#匀借、Call

@property(nonatomic,strong)NSString*mainTitle;

//子標簽:字母

@property(nonatomic,strong)NSString*subTitle;

//設置屬性,初始化外觀

-(void)setMainTitle:(NSString*)mainTitle andSubTitle:(NSString*) subTitle;

@end

MPCircleButton.m

//

//? MPCircleButton.m

//仿iPhone電話App_2017

//

//? Created by GuoChunlei on 2017/6/26.

//? Copyright ? 2017年class3g. All rights reserved.

//

#import"MPCircleButton.h"

@implementationMPCircleButton

-(void)setMainTitle:(NSString*)mainTitle andSubTitle:(NSString*) subTitle{

if(self!=nil) {

self.mainTitle= mainTitle;

self.subTitle= subTitle;

//設置圓角半徑

self.layer.cornerRadius=self.bounds.size.width/2;

//設置子圖層蒙版

self.layer.masksToBounds=YES;

//設置按鈕的邊框寬度和顏色

self.layer.borderWidth=1.0f;

self.layer.borderColor= [[UIColordarkGrayColor]CGColor];

/*

這里CGColor其實是一個結構體平窘,UIColor的一個成員屬性

可以參考:http://www.cnblogs.com/smileEvday/archive/2012/06/05/UIColor_CIColor_CGColor.html

*/

if(subTitle !=nil){

//添加屬性標簽:

UILabel*mainLabel = [[UILabelalloc]initWithFrame:CGRectMake(0,0,self.bounds.size.width,self.bounds.size.height/3.0*2)];

mainLabel.text=self.mainTitle;

[mainLabelsetTextAlignment:NSTextAlignmentCenter];

[mainLabelsetTextColor:[UIColordarkGrayColor]];

[mainLabelsetFont:[UIFontfontWithName:@"Heiti SC"size:40.0f]];

[selfaddSubview:mainLabel];

//將mainLabel的中心點移至水平中線1/3點

mainLabel.center=CGPointMake(self.bounds.size.width/2,self.bounds.size.height/3.0+5);

UILabel*subLabel = [[UILabelalloc]initWithFrame:CGRectMake(0,0,self.bounds.size.width,self.bounds.size.height/3.0)];

subLabel.text=self.subTitle;

[subLabelsetTextAlignment:NSTextAlignmentCenter];

[subLabelsetTextColor:[UIColordarkGrayColor]];

//Helvetica字體是西方最常用的一種字體

[subLabelsetFont:[UIFontfontWithName:@"Helvetica"size:10.0f]];

//將subLabel的中心點移至水平中線2/3點

subLabel.center=CGPointMake(self.bounds.size.width/2.0,self.bounds.size.height/3*2);

[selfaddSubview:subLabel];

}else{

//??????????? self.titleLabel.text = self.mainTitle;

[selfsetTitle:self.mainTitleforState:UIControlStateNormal];

[selfsetTitleColor:[UIColordarkGrayColor]forState:UIControlStateNormal];

[self.titleLabelsetFont:[UIFontfontWithName:@"Heiti SC"size:40.0f]];

//??????????? [self.titleLabel setTextColor:[UIColor darkGrayColor]];

}

}

}

/*

在UIKit中吓肋,系統(tǒng)提供了animate標題打頭的屬于UIView的類方法讓我們可以輕松的制作動畫效果,每一個這樣的類方法提供了名為animations的block代碼塊瑰艘,這些代碼會在方法調用后立刻或者延遲一段時間以動畫的方式執(zhí)行是鬼。此外,所有這些API的第一個參數(shù)都是用來設置動畫時長的紫新。

*/

//觸摸開始均蜜,播放前半部動畫

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

[supertouchesBegan:toucheswithEvent:event];

//在0.5秒鐘的時間內透明度由1.0->.0.2

[UIViewanimateWithDuration:0.5fanimations:^{

self.alpha=0.2f;

}];

}

//觸摸結束,播放后半段動畫

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

[supertouchesEnded:toucheswithEvent:event];

[UIViewanimateWithDuration:0.5fanimations:^{

self.alpha=1.0f;

}];

}

//-(void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents{

//??? [super addTarget:target action:action forControlEvents:controlEvents];

//

//

//}

@end

DialPadViewController.m:測試代碼

//

//? DialPadViewController.m

//仿iPhone通訊錄

//

//? Created by GuoChunlei on 2017/6/26.

//? Copyright ? 2017年class3g. All rights reserved.

//

#import"DialPadViewController.h"

#import"MPCircleButton.h"

@interfaceDialPadViewController()

@end

@implementationDialPadViewController

- (void)viewDidLoad {

[superviewDidLoad];

// Do any additional setup after loading the view.

MPCircleButton*testBtn = [[MPCircleButtonalloc]initWithFrame:CGRectMake(100,200,100,100)];

[self.viewaddSubview:testBtn];

[testBtnsetMainTitle:@"2"andSubTitle:@"ABC"];

testBtn.backgroundColor= [UIColororangeColor];

[testBtnaddTarget:selfaction:@selector(onclick:)forControlEvents:UIControlEventTouchUpInside];

MPCircleButton*otherTestBtn = [[MPCircleButtonalloc]initWithFrame:CGRectMake(100,400,100,100)];

[self.viewaddSubview:otherTestBtn];

[otherTestBtnsetMainTitle:@"#"andSubTitle:nil];

otherTestBtn.backgroundColor= [UIColorgreenColor];

//??? otherTestBtn setTitle:<#(nullable NSString *)#> forState:<#(UIControlState)#>

[otherTestBtnaddTarget:selfaction:@selector(onOtherClick:)forControlEvents:UIControlEventTouchUpInside];

}

-(void)onclick:(MPCircleButton*)btn{

NSLog(@"%@", btn.mainTitle);

}

-(void)onOtherClick:(MPCircleButton*)btn{

NSLog(@"%@", btn.mainTitle);

}

- (void)didReceiveMemoryWarning {

[superdidReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

/*

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

// Get the new view controller using [segue destinationViewController].

// Pass the selected object to the new view controller.

}

*/

@end

本課習題:

1芒率、完成課程中講述的撥號鍵盤全部功能

2囤耳、研究嘗試實現(xiàn)號碼顯示標簽,如下圖所示

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市紫皇,隨后出現(xiàn)的幾起案子慰安,更是在濱河造成了極大的恐慌,老刑警劉巖聪铺,帶你破解...
    沈念sama閱讀 222,681評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件化焕,死亡現(xiàn)場離奇詭異,居然都是意外死亡铃剔,警方通過查閱死者的電腦和手機撒桨,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,205評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來键兜,“玉大人凤类,你說我怎么就攤上這事∑掌” “怎么了谜疤?”我有些...
    開封第一講書人閱讀 169,421評論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長现诀。 經常有香客問我夷磕,道長,這世上最難降的妖魔是什么仔沿? 我笑而不...
    開封第一講書人閱讀 60,114評論 1 300
  • 正文 為了忘掉前任坐桩,我火速辦了婚禮,結果婚禮上封锉,老公的妹妹穿的比我還像新娘绵跷。我一直安慰自己,他們只是感情好成福,可當我...
    茶點故事閱讀 69,116評論 6 398
  • 文/花漫 我一把揭開白布碾局。 她就那樣靜靜地躺著,像睡著了一般闷叉。 火紅的嫁衣襯著肌膚如雪擦俐。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,713評論 1 312
  • 那天握侧,我揣著相機與錄音蚯瞧,去河邊找鬼。 笑死品擎,一個胖子當著我的面吹牛埋合,可吹牛的內容都是我干的。 我是一名探鬼主播萄传,決...
    沈念sama閱讀 41,170評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼甚颂,長吁一口氣:“原來是場噩夢啊……” “哼蜜猾!你這毒婦竟也來了?” 一聲冷哼從身側響起振诬,我...
    開封第一講書人閱讀 40,116評論 0 277
  • 序言:老撾萬榮一對情侶失蹤蹭睡,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后赶么,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體肩豁,經...
    沈念sama閱讀 46,651評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,714評論 3 342
  • 正文 我和宋清朗相戀三年辫呻,在試婚紗的時候發(fā)現(xiàn)自己被綠了清钥。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,865評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡放闺,死狀恐怖祟昭,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情怖侦,我是刑警寧澤篡悟,帶...
    沈念sama閱讀 36,527評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站匾寝,受9級特大地震影響恰力,放射性物質發(fā)生泄漏。R本人自食惡果不足惜旗吁,卻給世界環(huán)境...
    茶點故事閱讀 42,211評論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望停局。 院中可真熱鬧很钓,春花似錦、人聲如沸董栽。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,699評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽锭碳。三九已至袁稽,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間擒抛,已是汗流浹背推汽。 一陣腳步聲響...
    開封第一講書人閱讀 33,814評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留歧沪,地道東北人歹撒。 一個月前我還...
    沈念sama閱讀 49,299評論 3 379
  • 正文 我出身青樓,卻偏偏與公主長得像诊胞,于是被迫代替她去往敵國和親暖夭。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,870評論 2 361

推薦閱讀更多精彩內容