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

來源:互聯網
上載者:User

一、手寫控制項 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

聯繫我們

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