iOS開發UI基礎—手寫控制項,frame,center和bounds屬性

來源:互聯網
上載者:User

標籤:

1.16

1.NSBundle

1> 一個NSBundle代表一個檔案夾,利用NSBundle能訪問對應的檔案夾

2> 利用mainBundle就可以訪問軟體資源套件中的任何資源

3> 模擬器應用程式的安裝路徑

/Users/aplle/資產庫/Application Support/iPhone Simulator/7.1/Applications

 

2.UIImageView和UIButton

1> 使用場合

* UIImageView: 如果僅僅是顯示圖片,不需要監聽圖片的點擊

* UIButton: 既要顯示圖片,又要監聽圖片的點擊

 

2> 相同:能顯示圖片

 

3> 不同點

* UIButton能處理點擊事件, UIImageView不能處理點擊事件

* UIButton既能顯示圖片, 又能顯示文字

* UIButton能同時顯示兩張圖片

* UIButton繼承自UIControl, 因此預設就能處理事件

* UIImageView繼承自UIView, 因此預設就不能處理事件

 

3.Xcode文檔安裝路徑

/Applications/Xcode.app/Contents/Developer/Documentation/DocSets

 

4.Xcode模擬器安裝路徑

/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs

1、複習
1)基本iOS項目的編寫步驟
-> 設計介面(拖控制項)
-> 設定屬性與事件
-> 編寫邏輯代碼
2)常見的拖線的方法
3)加法計算機(一些常用屬性,退出鍵盤的方法)
4)frame、bounds、center和transform
5)簡單的動畫
bneginAnimation
commit...
6)手動建立控制項

2、作業
secure

3、利用代碼建立按鈕
基本步驟:建立對象、設定屬性、加到view中
置中的演算法
設定圖片背景 setBackgroundImage: forState:
設定文本 setTitle: forState:
設定文本顏色 setTitleColor: forState:
...

4、添加事件:addTarget

5、什麼情況下用 storyboard? 什麼時候用代碼?都有什麼優勢?
布局結構中不變的情況下考慮 storyboard
如果布局結構常常發生變換,那麼考慮純程式碼的方式

storyboard 記錄了介面上需要什麼控制項,以及控制項的屬性
而在程式啟動並執行時候,編譯器會讀取 storyboard 檔案
根據這個檔案中的記錄,還原成代碼,一步一步建立對應的控制項
設定對應的屬性

6、 首先搭建介面
拖線

if(self.index == 0) {
self.btnLeft.enabled = NO;
} else {
self.btnLeft.enabled = YES;
}
if(self.index != 4) {
self.btnRight.enabled = YES;
} else {
self.btnRight.enabled = NO;
}
============
if(self.index != 0) {
self.btnLeft.enabled = YES;
} else {
self.btnLeft.enabled = NO;
}
if(self.index == 4) {
self.btnRight.enabled = NO;
} else {
self.btnRight.enabled = YES;
}

凡事if-else結構中使用純賦值代碼的,可以簡化為三元運算式
int num = 1 > 2 ? 1 : 2;

7、plist 檔案
property list 屬性列表

6、圖片瀏覽器例子
-> 搭建介面(按鈕的一個應用、UIImageView)
-> UIImageView 與 UIButton
UIButton繼承子UIControl
預設所有的繼承自UIControl的控制項都具有預設的事件處理方法
而UIImageView直接來自於UIView
預設是沒有事件處理的
就顯示圖片而已都是一樣的,但是由於沒有事件處理,UIImageView效能會更高
如果只考慮顯示圖片,建議使用UIImageView
如果有點擊處理,使用UIButton
-> 複習數組、索引、字典的使用
-> NSBundle用來擷取檔案的全路徑
-> 獲得數組的時候,可以使用File的全路徑

7、UIIamgeView
前面已經說到了一個動畫
UIView beginAnimations
。。。。
UIView commitAnimatiuons
序列幀動畫

8、動畫記憶體
UIImage imageNamed:@""
一般載入小圖片或系統圖片使用


9、補充的知識點
1)UIView是一個容器,裡面可以放置其他的UIView
2)frame屬性在定位的時候,使用的是父容器作為參考系
3)應用程式裝到哪裡去了

iOS開發UI基礎—手寫控制項,frame,center和bounds屬性

一、手寫控制項

1.手寫控制項的步驟(1)使用相應的控制項類建立控制項對象(2)設定該控制項的各種屬性(3)添加控制項到視圖中(4)如果是button等控制項,還需考慮控制項的單擊事件等(5)注意:View Contollor和view的關係2.注意點

在OC開發中,Storyboard中的所有操作都可以通過代碼實現,程式員一定要熟練掌握代碼布局介面的能力!

設定控制項監聽方法的範例程式碼如下:

[btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

提示:

1> addTarget方法定義在UIControl類中,這意味著可以給所有繼承自UIControl類的對象添加監聽方法

2> 監聽方法的第一個參數就是對象本身

3> 監聽方法的第二個參數是監聽控制項的事件

3.程式碼範例

 1     //1.使用類建立一個按鈕對象 2    // UIButton *headbtn=[[UIButton alloc] initWithFrame:CGRectMake(100 ,100, 100, 100)]; 3     //設定按鈕對象為自訂型 4     UIButton *headbtn=[UIButton buttonWithType:UIButtonTypeCustom]; 5      6     //2.設定對象的各項屬性 7     //(1)位置等通用屬性設定 8     headbtn.frame=CGRectMake(100, 100, 100, 100); 9     10     //(2)設定普通狀態下按鈕的屬性11     [headbtn setBackgroundImage:[UIImage imageNamed:@"i"] forState:UIControlStateNormal];12     [headbtn setTitle:@"點我!" forState:UIControlStateNormal];13     [headbtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];14     15     //(3)設定高亮狀態下按鈕的屬性16     [headbtn setBackgroundImage:[UIImage imageNamed:@"a"] forState:UIControlStateHighlighted];17     [headbtn setTitle:@"還行吧~" forState:UIControlStateHighlighted];18     [headbtn setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];19     20     //3.把對象添加到視圖中展現出來21     [self.view addSubview:headbtn];22     //注意點!23     self.headImageView=headbtn;

二、frame,center和bounds屬性

1.frame、center和bounds屬性frame:控制位置和大小center:控制位置(中心點)bounds:控制大小(以自己的左上方為原點)2.注意點

(1)通過以下屬性可以修改控制項的位置

frame.origin

center

(2)通過以下屬性可以修改控制項的尺寸

frame.size

bounds.size

3.程式碼範例

一個控製圖片上下左右平移,縮放的程式(frame、center和bounds屬性)

  1 //  2 //  YYViewController.m  3 //  01-練習使用按鈕的frame和center屬性  4 //  5 //  Created by apple on 14-5-21.  6 //  Copyright (c) 2014年 itcase. All rights reserved.  7 //  8   9 #import "YYViewController.h" 10  11 //私人擴充 12 @interface YYViewController () 13  14 @property(nonatomic,weak)IBOutlet UIButton *headImageView; 15 @end 16  17 @implementation YYViewController 18  19 //枚舉類型,從1開始 20 typedef enum 21 { 22     ktopbtntag=1, 23     kdownbtntag, 24     krightbtntag, 25     kleftbtntag 26 }btntag; 27  28 //viewDidLoad是視圖載入完成後調用的方法,通常在此方法中執行視圖控制器的初始化工作 29 - (void)viewDidLoad 30 { 31      32     //在viewDidLoad方法中,不要忘記調用父類的方法實現 33     [super viewDidLoad]; 34  35      36     //手寫控制項代碼 37     //一、寫一個按鈕控制項,上面有一張圖片 38      39     //1.使用類建立一個按鈕對象 40    // UIButton *headbtn=[[UIButton alloc] initWithFrame:CGRectMake(100 ,100, 100, 100)]; 41     //設定按鈕對象為自訂型 42     UIButton *headbtn=[UIButton buttonWithType:UIButtonTypeCustom]; 43      44     //2.設定對象的各項屬性 45     //(1)位置等通用屬性設定 46     headbtn.frame=CGRectMake(100, 100, 100, 100); 47      48     //(2)設定普通狀態下按鈕的屬性 49     [headbtn setBackgroundImage:[UIImage imageNamed:@"i"] forState:UIControlStateNormal]; 50     [headbtn setTitle:@"點我!" forState:UIControlStateNormal]; 51     [headbtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; 52      53     //(3)設定高亮狀態下按鈕的屬性 54     [headbtn setBackgroundImage:[UIImage imageNamed:@"a"] forState:UIControlStateHighlighted]; 55     [headbtn setTitle:@"還行吧~" forState:UIControlStateHighlighted]; 56     [headbtn setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted]; 57      58     //3.把對象添加到視圖中展現出來 59     [self.view addSubview:headbtn]; 60     //注意點! 61     self.headImageView=headbtn; 62  63      64     //二、寫四個控製圖片左右上下移動方向的按鈕控制項 65      66     /**================向上的按鈕=====================*/ 67     //1.建立按鈕對象 68     UIButton *topbtn=[UIButton buttonWithType:UIButtonTypeCustom]; 69      70     //2.設定對象的屬性 71     topbtn.frame=CGRectMake(100, 250, 40, 40); 72     [topbtn setBackgroundImage:[UIImage imageNamed:@"top_normal"] forState:UIControlStateNormal]; 73     [topbtn setBackgroundImage:[UIImage imageNamed:@"top_highlighted"] forState:UIControlStateHighlighted]; 74     [topbtn setTag:1]; 75     //3.把控制項添加到視圖中 76     [self.view addSubview:topbtn]; 77      78     //4.按鈕的單擊控制事件 79     [topbtn addTarget:self action:@selector(Click:) forControlEvents:UIControlEventTouchUpInside]; 80      81      82       /**================向下的按鈕=====================*/ 83     //1.建立按鈕對象 84     UIButton *downbtn=[UIButton buttonWithType:UIButtonTypeCustom]; 85     //2.設定對象的屬性 86     downbtn.frame=CGRectMake(100, 350, 40, 40); 87     [downbtn setBackgroundImage:[UIImage imageNamed:@"bottom_normal"] forState:UIControlStateNormal]; 88     [downbtn setBackgroundImage:[UIImage imageNamed:@"bottom_highlighted"] forState:UIControlStateHighlighted]; 89     [downbtn setTag:2]; 90     //3.把控制項添加到視圖中 91     [self.view addSubview:downbtn]; 92      93     //4.按鈕的單擊控制事件 94     [downbtn addTarget:self action:@selector(Click:) forControlEvents:UIControlEventTouchUpInside]; 95  96      97      /**================向左的按鈕=====================*/ 98     //1.建立按鈕對象 99     UIButton *leftbtn=[UIButton buttonWithType:UIButtonTypeCustom];100     //2.設定對象的屬性101     leftbtn.frame=CGRectMake(50, 300, 40, 40);102     [leftbtn setBackgroundImage:[UIImage imageNamed:@"left_normal"] forState:UIControlStateNormal];103     [leftbtn setBackgroundImage:[UIImage imageNamed:@"left_highlighted"] forState:UIControlStateHighlighted];104     [leftbtn setTag:4];105     //3.把控制項添加到視圖中106     [self.view addSubview:leftbtn];107     108     //4.按鈕的單擊控制事件109     [leftbtn addTarget:self action:@selector(Click:) forControlEvents:UIControlEventTouchUpInside];110     111     112     113     /**================向右的按鈕=====================*/114     //1.建立按鈕對象115     UIButton *rightbtn=[UIButton buttonWithType:UIButtonTypeCustom];116     //2.設定對象的屬性117     rightbtn.frame=CGRectMake(150, 300, 40, 40);118     [rightbtn setBackgroundImage:[UIImage imageNamed:@"right_normal"] forState:UIControlStateNormal];119     [rightbtn setBackgroundImage:[UIImage imageNamed:@"right_highlighted"] forState:UIControlStateHighlighted];120     [rightbtn setTag:3];121     //3.把控制項添加到視圖中122     [self.view addSubview:rightbtn];123     124     //4.按鈕的單擊控制事件125     [rightbtn addTarget:self action:@selector(Click:) forControlEvents:UIControlEventTouchUpInside];126     127       //三、寫兩個縮放按鈕128        /**================放大的按鈕=====================*/129     //1.建立對象130     UIButton *plusbtn=[UIButton buttonWithType:UIButtonTypeCustom];131     //2.設定屬性132     plusbtn.frame=CGRectMake(75, 400, 40, 40);133     [plusbtn setBackgroundImage:[UIImage imageNamed:@"plus_normal"] forState:UIControlStateNormal];134     [plusbtn setBackgroundImage:[UIImage imageNamed:@"plus_highlighted"] forState:UIControlStateHighlighted];135     [plusbtn setTag:1];136     //3.添加到視圖137     [self.view addSubview:plusbtn];138     //4.單擊事件139     [plusbtn addTarget:self action:@selector(Zoom:) forControlEvents:UIControlEventTouchUpInside];140     141     142     /**================縮小的按鈕=====================*/143     UIButton *minusbtn=[UIButton buttonWithType:UIButtonTypeCustom];144     minusbtn.frame=CGRectMake(125, 400, 40, 40);145     [minusbtn setBackgroundImage:[UIImage imageNamed:@"minus_normal"] forState:UIControlStateNormal];146     [minusbtn setBackgroundImage:[UIImage imageNamed:@"minus_highlighted"] forState:UIControlStateHighlighted];147     [minusbtn setTag:0];148     [self.view addSubview:minusbtn];149     [minusbtn addTarget:self action:@selector(Zoom:) forControlEvents:UIControlEventTouchUpInside];150 }151 152 //控制方向的多個按鈕調用同一個方法153 -(void)Click:(UIButton *)button154 {155 156     //練習使用frame屬性157     //CGRect frame=self.headImageView.frame;158     159     /**注意,這裡如果控制位置的兩個屬性frame和center同時使用的話,會出現很好玩的效果,注意分析*/160     //練習使用center屬性161     CGPoint center=self.headImageView.center;162     switch (button.tag) {163         case ktopbtntag:164             center.y-=30;165             break;166         case kdownbtntag:167             center.y+=30;168             break;169         case kleftbtntag:170             //發現一個bug,之前的問題是因為少寫了break,造成了它們的順序執行,sorry171            //center.x=center.x-30;172             center.x-=50;173             break;174         case krightbtntag:175             center.x+=50;176             break;177     }178     179  //  self.headImageView.frame=frame;180     181     //首尾式設定動畫效果182     [UIView beginAnimations:nil context:nil];183     self.headImageView.center=center;184     //設定時間185     [UIView setAnimationDuration:2.0];186     [UIView commitAnimations];187     NSLog(@"移動!");188     189 }190 -(void)Zoom:(UIButton *)btn191 {192     //使用frame,以自己的左上方(自己的原點)為原點193 //    CGRect frame=self.headImageView.frame;194 //    if (btn.tag) {195 //        frame.size.height+=30;196 //        frame.size.width+=30;197 //    }198 //    else199 //    {200 //        frame.size.width-=50;201 //        frame.size.height-=50;202 //    }203 //    self.headImageView.frame=frame;204     205     206     //使用bounds,以中心點位原點進行縮放207     CGRect bounds = self.headImageView.bounds;208     if (btn.tag) {209         bounds.size.height+=30;210         bounds.size.width+=30;211     }212     else213     {214         bounds.size.height-=50;215         bounds.size.width-=50;216     }217     218     //設定首尾動畫219     [UIView beginAnimations:nil context:nil];220     self.headImageView.bounds=bounds;221     [UIView setAnimationDuration:2.0];222     [UIView commitAnimations];223 }224 @end

實現效果:

三、簡單的動畫效果

簡單介紹首尾式動畫效果(1)開始動畫(2)設定動畫相關的時間等(3)參與動畫的行動(4)提交動畫註:實現代碼參考上面的代碼 

iOS開發UI基礎—手寫控制項,frame,center和bounds屬性

聯繫我們

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