iOS開發-UI (二)Button和Image,ios-ui

來源:互聯網
上載者:User

iOS開發-UI (二)Button和Image,ios-ui

 

知識點:

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"];

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.