iOS的UI開發(fā)中Button的基本編寫方法講解
一、簡單說明
一般情況下,點(diǎn)擊某個(gè)控件后,會(huì)做出相應(yīng)反應(yīng)的都是按鈕
按鈕的功能比較多,既能顯示文字,又能顯示圖片,還能隨時(shí)調(diào)整內(nèi)部圖片和文字的位置
二、按鈕的三種狀態(tài)
normal(普通狀態(tài))
默認(rèn)情況(Default)
對應(yīng)的枚舉常量:UIControlStateNormal
highlighted(高亮狀態(tài))
按鈕被按下去的時(shí)候(手指還未松開)
對應(yīng)的枚舉常量:UIControlStateHighlighted
disabled(失效狀態(tài),不可用狀態(tài))
如果enabled屬性為NO,就是處于disable狀態(tài),代表按鈕不可以被點(diǎn)擊
對應(yīng)的枚舉常量:UIControlStateDisabled
三、注意點(diǎn)
(1)從Xcode5開始,圖片資源都放到Images.xcassets中進(jìn)行管理,可以使用拖拽的方式添加項(xiàng)目中用到的圖片到Images.xcassets中
(2)若干多個(gè)控件共用一段代碼,通常使用tag。
四、代碼示例
(1)
#import "LFViewController.h"
@interface LFViewController ()
@property (weak, nonatomic) IBOutlet UIButton *headImageView;
@end
@implementation LFViewController
// 在OC中,絕大多數(shù)的控件的監(jiān)聽方法的第一個(gè)參數(shù)就是控件本身
//- (IBAction)left:(UIButton *)button {
//
// NSLog(@"----");
//}
- (IBAction)move
{
// 通過frame修改head的位置
// 在OC中,不允許直接修改“對象”的“結(jié)構(gòu)體屬性”的“成員”
// 允許修改“對象”的“結(jié)構(gòu)體屬性”
// 1. 取出結(jié)構(gòu)體屬性
CGRect rect = self.headImageView.frame;
// 2. 修改結(jié)構(gòu)體成員
rect.origin.y -= 20;
// 3. 設(shè)置對象的結(jié)構(gòu)體屬性
self.headImageView.frame = rect;
}
(2)
#import "LFViewController.h"
/**
使用git
1. 創(chuàng)建項(xiàng)目時(shí),勾選git
2. 開發(fā)告一段落后,選擇"Source Control""Commit",并編寫注釋
*/
// 枚舉類型實(shí)質(zhì)上就是一個(gè)整數(shù),作用就是用來替代魔法數(shù)字
// 枚舉類型中,指定了第一個(gè)整數(shù)之后,后面的數(shù)字會(huì)遞增
typedef enum
{
kMovingDirTop = 10,
kMovingDirBottom,
kMovingDirLeft,
kMovingDirRight,
} kMovingDir;
#define kMovingDelta 50
@interface LFViewController ()
@property (weak, nonatomic) IBOutlet UIButton *headImageView;
@end
@implementation LFViewController
- (IBAction)move:(UIButton *)button
{
// CGRect rect = self.headImageView.frame;
CGPoint p = self.headImageView.center;
// magic number魔法數(shù)字,其他程序員看到代碼的時(shí)候,不知道是什么意思
switch (button.tag) {
case kMovingDirTop:
p.y -= kMovingDelta;
break;
case kMovingDirBottom:
p.y += kMovingDelta;
break;
case kMovingDirLeft:
p.x -= kMovingDelta;
break;
case kMovingDirRight:
p.x += kMovingDelta;
break;
}
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
self.headImageView.center = p;
[UIView commitAnimations];
}
- (IBAction)zoom:(UIButton *)button
{
CGRect rect = self.headImageView.bounds;
// 在C語言中,關(guān)于bool的判斷:非零即真
if (button.tag) {
rect.size.width += 50;
rect.size.height += 50;
} else {
rect.size.width -= 50;
rect.size.height -= 50;
}
// 首尾動(dòng)畫
// beginAnimations表示此后的代碼要“參與到”動(dòng)畫中
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
self.headImageView.bounds = rect;
// self.headImageView.alpha = 0;
// commitAnimations,將beginAnimation之后的所有動(dòng)畫提交并生成動(dòng)畫
[UIView commitAnimations];
}
@end
五、補(bǔ)充筆記
1. IBAction的參數(shù)
- (IBAction)left:(UIButton *)button
(1) 在OC中,絕大多數(shù)的控件監(jiān)聽方法的第一個(gè)參數(shù)就是控件本身
(2) 默認(rèn)連線時(shí)的參數(shù)類型是id
(3) 如果要在監(jiān)聽方法中,方便控件的使用,可以在連線時(shí)或者連線后,修改監(jiān)聽方法的參數(shù)類型
2. 修改對象的結(jié)構(gòu)體成員
在OC中,不允許直接修改“對象”的“結(jié)構(gòu)體屬性”的“成員”,但是允許修改“對象”的“結(jié)構(gòu)體屬性”
修改結(jié)構(gòu)體屬性的成員方法如下:
(1)使用臨時(shí)變量記錄對象的結(jié)構(gòu)體屬性
(2) 修改臨時(shí)變量的屬性
(3)將臨時(shí)變量重新設(shè)置給對象的結(jié)構(gòu)體屬性
3. 在程序開發(fā)中需要避免出現(xiàn)魔法數(shù)字(Magic Number)
使用枚舉類型,可以避免在程序中出現(xiàn)魔法數(shù)字
(1)枚舉類型實(shí)質(zhì)上就是一個(gè)整數(shù),其作用就是用來替代魔法數(shù)字
(2)枚舉類型中,指定了第一個(gè)整數(shù)之后,后面的數(shù)字會(huì)遞增
4. frame & bounds & center
1> frame可以修改對象的位置和尺寸
2> bounds可以修改對象的尺寸
3> center可以修改對象的位置
5. 首尾式動(dòng)畫
// beginAnimations表示此后的代碼要“參與到”動(dòng)畫中
[UIView beginAnimations:nil context:nil];
// setAnimationDuration用來指定動(dòng)畫持續(xù)時(shí)間
[UIView setAnimationDuration:2.0];
self.headImageView.bounds = rect;
......
// commitAnimations,將beginAnimation之后的所有動(dòng)畫提交并生成動(dòng)畫
[UIView commitAnimations];
下面來羅列一下UIButton的基本屬性羅列
第一、UIButton的定義
UIButton *button=[[UIButton buttonWithType:(UIButtonType);
能夠定義的button類型有以下6種,
typedef enum {
UIButtonTypeCustom = 0, 自定義風(fēng)格
UIButtonTypeRoundedRect, 圓角矩形
UIButtonTypeDetailDisclosure, 藍(lán)色小箭頭按鈕,主要做詳細(xì)說明用
UIButtonTypeInfoLight, 亮色感嘆號
UIButtonTypeInfoDark, 暗色感嘆號
UIButtonTypeContactAdd, 十字加號按鈕
} UIButtonType;
第二、設(shè)置frame
button1.frame = CGRectMake(20, 20, 280, 40);
[button setFrame:CGRectMake(20,20,50,50)];
第三、button背景色
button1.backgroundColor = [UIColor clearColor];
[button setBackgroundColor:[UIColor blueColor]];
第四、state狀態(tài)
forState: 這個(gè)參數(shù)的作用是定義按鈕的文字或圖片在何種狀態(tài)下才會(huì)顯現(xiàn)
enum {
UIControlStateNormal = 0, 常規(guī)狀態(tài)顯現(xiàn)
UIControlStateHighlighted = 1 << 0, 高亮狀態(tài)顯現(xiàn)
UIControlStateDisabled = 1 << 1, 禁用的狀態(tài)才會(huì)顯現(xiàn)
UIControlStateSelected = 1 << 2, 選中狀態(tài)
UIControlStateApplication = 0x00FF0000, 當(dāng)應(yīng)用程序標(biāo)志時(shí)
UIControlStateReserved = 0xFF000000 為內(nèi)部框架預(yù)留,可以不管他
};
@property(nonatomic,getter=isEnabled)BOOL enabled; // default is YES. if NO, ignores touch events and subclasses may draw differently
@property(nonatomic,getter=isSelected)BOOL selected; // default is NO may be used by some subclasses or by application
@property(nonatomic,getter=isHighlighted)BOOL highlighted;
第五 、設(shè)置button填充圖片和背景圖片
[buttonsetImage:[UIImageimageNamed:@"checkmarkControllerIcon"]forState:UIControlStateNormal];
[buttonsetBackgroundImage:[UIImageimageNamed:@"checkmarkControllerIcon"]forState:UIControlStateNormal];
第六、設(shè)置button標(biāo)題和標(biāo)題顏色
[button1 setTitle:@"點(diǎn)擊" forState:UIControlStateNormal];
[buttonsetTitleColor:[UIColorredColor]forState:UIControlStateNormal];
第七、設(shè)置按鈕按下會(huì)發(fā)光
button.showsTouchWhenHighlighted=NO;
第八、添加或刪除事件處理
[button1 addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];
[btn removeTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];
第九、 設(shè)置按鈕內(nèi)部圖片間距和標(biāo)題間距
UIEdgeInsets insets; // 設(shè)置按鈕內(nèi)部圖片間距
insets.top = insets.bottom = insets.right = insets.left = 10;
bt.contentEdgeInsets = insets;
bt.titleEdgeInsets = insets; // 標(biāo)題間距
相關(guān)文章
IOS 中UIImageView響應(yīng)點(diǎn)擊事件
這篇文章主要介紹了IOS 中UIImageView響應(yīng)點(diǎn)擊事件的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-09-09IOS UIWebView獲取404、504等錯(cuò)誤問題解決方案
這篇文章主要介紹了IOS UIWebView獲取404、504等錯(cuò)誤問題的相關(guān)資料,并對相應(yīng)的錯(cuò)誤問題提出相應(yīng)的解決方案,需要的朋友可以參考下2016-11-11IOS UITableView顏色設(shè)置的實(shí)例詳解
這篇文章主要介紹了IOS UITableView顏色設(shè)置的實(shí)例詳解的相關(guān)資料,這里提供了幾種方法幫助大家掌握這部分內(nèi)容,需要的朋友可以參考下2017-08-08iOS設(shè)置圓角的4種方法實(shí)例(附性能評測)
這篇文章主要給大家介紹了關(guān)于iOS設(shè)置圓角的4種方法,并給大家附上了性能評測,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01學(xué)習(xí)iOS開關(guān)按鈕UISwitch控件
這篇文章主要為大家詳細(xì)介紹了iOS開關(guān)按鈕UISwitch控件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08iOS如何實(shí)現(xiàn)強(qiáng)制轉(zhuǎn)屏、強(qiáng)制橫屏和強(qiáng)制豎屏的實(shí)例代碼
本篇文章主要介紹了iOS如何實(shí)現(xiàn)強(qiáng)制轉(zhuǎn)屏、強(qiáng)制橫屏和強(qiáng)制豎屏的實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07