[iOS基礎控制項 - 2] 按鈕的基本使用

來源:互聯網
上載者:User

標籤: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] 按鈕的基本使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.