一、UIButton
Button是最常用的控件之一,屬性也比較多,下面代碼創(chuàng)建了一個(gè)系統(tǒng)樣式的myButton 和一個(gè)自定義的myButtonCus,點(diǎn)擊myButtonCus按鈕,按鈕的圖案會(huì)變化,并會(huì)在屏幕中間產(chǎn)生一個(gè)UIImageView,展示按鈕的圖案。
添加一些新內(nèi)容
ios 去除按鈕的按下效果(陰影)
Button.adjustsImageWhenHighlighted = NO;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
//button有自己的便利初始化,設(shè)置Button的樣式
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeSystem];
//button的樣式,如果要自己設(shè)置樣式,選擇Custom
// UIButtonTypeCustom = 0, 自定義風(fēng)格
// UIButtonTypeRoundedRect = UIButtonTypeSystem, 圓角矩形
// UIButtonTypeDetailDisclosure, 藍(lán)色小箭頭按鈕,主要做詳細(xì)說(shuō)明用
// UIButtonTypeInfoLight, 亮色感嘆號(hào)
// UIButtonTypeInfoDark, 暗色感嘆號(hào)
// UIButtonTypeContactAdd, 十字加號(hào)按鈕
//常態(tài)下的標(biāo)題,Button在不同狀態(tài)下可以設(shè)置不同標(biāo)題,如長(zhǎng)按,取消等狀態(tài)
[myButton setTitle:@"button" forState:UIControlStateNormal];
[myButton setTitle:@"yes" forState:UIControlStateSelected];
//[myButton setTitle:@"no" forState:UIControlStateReserved];
//[myButton setTitle:@"Application" forState:UIControlStateApplication];
//[myButton setTitle:@"Highlighted" forState:UIControlStateHighlighted];
//設(shè)置按鈕大小,因?yàn)槌跏蓟推渌丶煌?,frame設(shè)置容易遺漏
myButton.frame = CGRectMake(50, 50, 100, 100);
//添加點(diǎn)擊事件
//Target?:動(dòng)作的執(zhí)行目標(biāo)(按鈕的action事件寫在哪個(gè)類里)
//action:按鈕的回調(diào)方法,如果方法有參數(shù),那么參數(shù)一定是按鈕本身
//events:哪種觸摸方式
//如果self寫在對(duì)象方法中,也就是OC中的減號(hào)方法中,self就代表類對(duì)象,如果寫在類方法中,就代表本類
//UIButton的觸發(fā)類型
// UIControlEventTouchDown
// 單點(diǎn)觸摸按下事件:用戶點(diǎn)觸屏幕,或者又有新手指落下的時(shí)候。
// UIControlEventTouchDownRepeat
// 多點(diǎn)觸摸按下事件,點(diǎn)觸計(jì)數(shù)大于1:用戶按下第二、三、或第四根手指的時(shí)候。
//
// UIControlEventTouchDragInside
// 當(dāng)一次觸摸在控件窗口內(nèi)拖動(dòng)時(shí)。
//
// UIControlEventTouchDragOutside
// 當(dāng)一次觸摸在控件窗口之外拖動(dòng)時(shí)。
//
// UIControlEventTouchDragEnter
// 當(dāng)一次觸摸從控件窗口之外拖動(dòng)到內(nèi)部時(shí)。
//
// UIControlEventTouchDragExit
// 當(dāng)一次觸摸從控件窗口內(nèi)部拖動(dòng)到外部時(shí)。
//
// UIControlEventTouchUpInside
// 所有在控件之內(nèi)觸摸抬起事件。
//
// UIControlEventTouchUpOutside
// 所有在控件之外觸摸抬起事件(點(diǎn)觸必須開始與控件內(nèi)部才會(huì)發(fā)送通知)。
//
// UIControlEventTouchCancel
// 所有觸摸取消事件,即一次觸摸因?yàn)榉派狭颂嗍种付蝗∠?,或者被上鎖或者電話呼叫打斷。
//
// UIControlEventTouchChanged
// 當(dāng)控件的值發(fā)生改變時(shí),發(fā)送通知。用于滑塊、分段控件、以及其他取值的控件。你可以配置滑塊控件何時(shí)發(fā)送通知,在滑塊被放下時(shí)發(fā)送,或者在被拖動(dòng)時(shí)發(fā)送。
//添加一個(gè)單擊的觸發(fā)類型
[myButton addTarget:self action:@selector(butAction:) forControlEvents:UIControlEventTouchUpInside];
[myButton setBackgroundColor:[UIColor yellowColor]];
[self.window addSubview:myButton];
//設(shè)置UIButton上字體的顏色設(shè)置UIButton上字體的顏色,不是用:
[btn.titleLabel setTextColor:[UIColorblackColor]];
btn.titleLabel.textColor=[UIColor redColor];
//而是用:
[btn setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
//自定義 custom按鈕
UIButton *myButtonCus = [UIButton buttonWithType:UIButtonTypeCustom];
//設(shè)置frame
myButtonCus.frame = CGRectMake(100, 100, 100, 100);
//為按鈕添加圖片
[myButtonCus setImage:[UIImage imageNamed:@"btnImage1.jpg"] forState:UIControlStateNormal];
[myButtonCus setImage:[UIImage imageNamed:@"btnImage2.jpg"] forState:UIControlStateHighlighted];
[myButtonCus addTarget:self action:@selector(butAction:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:myButtonCus];
//定義一個(gè)UIImageView 點(diǎn)擊按鈕時(shí)顯示按鈕上圖片
UIImageView *btnImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
btnImageView.center = self.window.center;
[self.window addSubview:btnImageView];
btnImageView.tag = 1001;
return YES;
}
-(void)butAction:(UIButton*)sender
{
//使button被選擇的狀態(tài),點(diǎn)擊一次就設(shè)置成相反的
sender.selected = !sender.selected;
//得到button上面的圖片
UIImage *btnImage = [sender imageForState:UIControlStateNormal];
if (sender.selected) {
btnImage = [sender imageForState:UIControlStateNormal];
}
else
{
btnImage = [sender imageForState:UIControlStateHighlighted];
}
//根據(jù)tag得到View,使用UIView強(qiáng)制轉(zhuǎn)換成UIImageView
UIImageView *view = (UIImageView*)[self.window viewWithTag:1001];
[view setImage:btnImage];
}
運(yùn)行效果:
點(diǎn)擊中間的按鈕,也就是暴漫圖片,屏幕中間會(huì)出現(xiàn)一個(gè)大圖,展示按鈕上的圖片
大部分的屬性和方法都寫在代碼里面,都有注釋,拷貝代碼運(yùn)行進(jìn)行嘗試就好