知識點: 1.UIButton使用和事件機制 2.UIImage 3.自定義UIButton UIButton 1.創建方式 按鈕類型 UIButtonTypeCustom 用戶自定義按鈕 UIButtonTypeRoundedRect 系統按鈕 UIButtonTypeDetailDisclosu ...
知識點:
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"];