標籤:style blog io ar color os 使用 sp for
UIButtonA.素材準備1.圖片素材放置到Images.xcassets中 B.按鈕狀態1.normal:預設狀態 Default對應的枚舉常量:UIControlStateNormal 2.highlighted(高亮狀態)按鈕被按下去的時候(未鬆開)對應的枚舉常量:UIControlStateHighlighted 3.disabled(失效狀態,不可用狀態)如果enable屬性為NO,就是處於disabled狀態,代表按鈕不可被點擊對應的枚舉常量:UIControlStateDisabled C.按鈕屬性Type:更改為Custom,消除按下按鈕時背景的透明效果State Config:選擇按鈕狀態,進而設定不同狀態下按鈕的其他屬性 D.autolayout 自動布局autolayout會干擾手動布局編碼的運行 E.使用tag標記組合多個按鈕使用一個方法
1 - (IBAction)move:(UIButton *) sender { 2 CGRect tempFrame = self.head.frame; 3 4 CGFloat delta = 10; 5 switch (sender.tag) { 6 case 1: 7 { 8 tempFrame.origin.y -= delta; 9 break;10 }11 case 2:12 {13 tempFrame.origin.x += delta;14 break;15 }16 case 3:17 {18 tempFrame.origin.y += delta;19 break;20 }21 case 4:22 {23 tempFrame.origin.x -= delta;24 break;25 }26 default:27 break;28 }29 30 self.head.frame = tempFrame;31 } F.使用center和bounds修改位置、尺寸使用center改變位置,效果和使用frame差不多使用bounds改變尺寸,能以中心為基點改變尺寸 G.位置移動動畫 // 開始動畫
[UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:1];(位置改變代碼)... // 提交動畫 [UIView commitAnimations]; H.手動編碼建立按鈕 // 1.建立一個自訂的按鈕
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
// 2.添加圖片
[button setImage:[UIImage imageNamed:imageName] forState:state];
// 3.設定尺寸、位置
button.frame = rect; // 4.設定tag, tag最好不要用0,因為預設所有都是0,會出現混亂錯誤,使用viewWithTag:0得到的是當前控制項,不是子控制項 button.tag = [[buttonDic objectForKey:@"tag"] intValue];
// 5.監聽按鈕點擊
[button addTarget:self action:@selector(moveImage:) forControlEvents:UIControlEventTouchUpInside];
// 6.添加按鈕 [self.view addSubview:button];
[iOS基礎控制項 - 2] 按鈕的基本使用