知識點:
1.UIButton運用和事情機制
2.UIImage
3.自定義UIButton
==================
UIButton
1.創立方式
按鈕類型
UIButtonTypeCustom 用戶自定義按鈕
UIButtonTypeRoundedRect 零碎按鈕
UIButtonTypeDetailDisclosure 更多信息按鈕 i
UIButtonTypeInfoLight 高亮信息按鈕 i
UIButtonTypeInfoDark 暗色信息按鈕 i
UIButtonTypeContactAdd 加號按鈕 +
UIButton *b2 = [UIButton buttonWithType:UIButtonTypeCustom];
2.設置按鈕的標題
-(void)setTitle:(NSString *)title forState:(UIControlState)state;
[b setTitle:@"點擊錄音" forState:UIControlStateNormal];
UIControlState:
UIControlStateNormal 按鈕普通形態
UIControlStateHighlighted 按鈕被按下(高亮)形態
UIControlStateDisabled 按鈕被禁用形態
UIControlStateSelected 按鈕被選中
//正常形態
[b setTitle:@"未選中" forState:UIControlStateNormal];
//選中形態
[b setTitle:@"選中" forState:UIControlStateSelected];
//禁用形態
[b setTitle:@"禁用" forState:UIControlStateDisabled];
//非選中形態==NO
//選中形態==YES
//btn.selected = !btn.selected;
//禁用
btn.enabled = NO;
3.設置按鈕的顏色
1)設置按鈕顏色
-(void)setTitleColor:(UIColor *)color forState:(UIControlState)state;
4.事情處置
1)IOS事情分類
觸控事情(單點、多點觸控以及各種手勢操作)
傳感器事情(重力、減速度傳感器等)
近程控制事情(近程遙控IOS設備多媒體播放等)
2)添加事情
-(void)addTarget:(id)target
action:(SEL)action
forControlEvents:(UIControlEvents)controlEvents
[b2 addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
3)刪除事情
-(void)removeTarget:(id)target
action:(SEL)action
forControlEvents:(UIControlEvents)controlEvents
5.常用事情:UIControlEvents
UIControlEventTouchDown 按鈕按下
UIControlEventTouchUpInside 按鈕按下彈起
UIControlEventTouchUpOutside 按鈕按下,按鈕外彈
[b addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
6.UIView標簽:setTag
view1.tag = 100;
==========================
UIImage
+(UIImage *)imageNamed:(NSString *)name;
運用該辦法的前提是圖片必需曾經存在與項目中,假如圖片需求屢次顯示時運用(會耗費零碎緩存 空間)
UIImage *image1 = [UIImage imageNamed:@"3_normal"];
+(UIImage *)UIImage imageWithContentsOfFile:(NSString *);
運用此辦法加載文件時,零碎不把圖像以數據的方式加載到順序中,
如不需求復用該圖像或許是一個很大的圖像時引薦運用此辦法
UIImage *image2 = [UIImage imageWithContentsOfFile:filePath]
獲取項目中的資源途徑
NSString * path = [[NSBundle mainBundle] pathForResource:@"Logo" ofType:@"png"];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"3_selected" ofType:@"png"];
==========================
自定義UIButton
1.圖片按鈕
1)設置按鈕背景圖片
-(void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;
//設置btn的背景圖片 (正常形態)
[b setBackgroundImage:image1 forState:UIControlStateNormal];
2)設置按鈕圖片
-(void)setImage:(UIImage *)image forState:(UIControlState)state;
//設置btn顯示的圖片(正常形態)
[b setImage:image1 forState:UIControlStateNormal];
[b setImage:image2 forState:UIControlStateSelected];
//設置btn顯示的圖片(高亮形態)
[b setImage:image2 forState:UIControlStateHighlighted];
2.完成進程
1)buttonType類型為 UIButtonTypeCustom
//實例化btn
UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
2)設置圖片:setImage:forState:image:圖片的文件名
UIImage *image1 = [UIImage imageNamed:@"3_normal"];
【iOS開發-UI (二)Button和Image】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!