之前課程代碼:
本次教案:
提前布置:
要求學生在課下研究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)號碼顯示標簽,如下圖所示