[IOS_UI控制項] IOS代碼實現常用控制項UIButton、UISlider、UISwitch、UISegmentedControl

來源:互聯網
上載者:User

標籤:style   class   blog   code   http   tar   

 

IOS中最常用到的控制項UIButton、UISlider、UISwitch、UISegmentedControl通過Xib檔案拖動產生非常簡單,其實用代碼實現也是一樣的簡單,當然,用代碼實現能夠掌握到更多的東西。

中包涵提到的4種控制項,UIButton按鈕、UISlider滑塊、UISwitch開關、UISegmentedControl分類

首先建立一個名為CodeControls的Empty Application項目


AppDelegate.h和AppDelegate.m檔案中和IOS代碼實現Hello World中的一樣


MainViewController.h

 

[cpp] view plaincopyprint?
  1. <span style="font-size:10px;">#import <UIKit/UIKit.h>  
  2.   
  3. @interface MainViewController : UIViewController  
  4.   
  5. @property (strong, nonatomic) UIButton *myBtn;  
  6. @property (strong, nonatomic) UISlider *mySlider;  
  7. @property (strong, nonatomic) UISwitch *mySwitch;  
  8. @property (strong, nonatomic) UISegmentedControl *mySc;  
  9.   
  10. @end</span>  
#import <UIKit/UIKit.h>@interface MainViewController : UIViewController@property (strong, nonatomic) UIButton *myBtn;@property (strong, nonatomic) UISlider *mySlider;@property (strong, nonatomic) UISwitch *mySwitch;@property (strong, nonatomic) UISegmentedControl *mySc;@end

MainViewController.m

 

 

[cpp] view plaincopyprint?
  1. <span style="font-size:10px;">#import "MainViewController.h"  
  2.   
  3. @interface MainViewController ()  
  4.   
  5. @end  
  6.   
  7. @implementation MainViewController  
  8. @synthesize myBtn,mySlider,mySwitch,mySc;  
  9.   
  10. - (void)viewDidLoad  
  11. {  
  12.     // 載入UIView  
  13.     UIView *mainView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];  
  14.     mainView.backgroundColor = [UIColor whiteColor];  
  15.     self.view = mainView;  
  16.     [mainView release];  
  17.       
  18.     // 建立一個Button按鈕  
  19.     UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
  20.     btn.frame = CGRectMake(100, 30, 57, 57);  
  21.     [btn setTitle:@"Button" forState:UIControlStateNormal];  
  22.     [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];  
  23.     [btn setBackgroundImage:[UIImage imageNamed:@"icon.png"] forState:UIControlStateNormal];  
  24.     [btn addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];  
  25.     myBtn = btn;  
  26.     [self.view addSubview:myBtn];  
  27.       
  28.       
  29.     // 建立一個Slider劃塊按鈕  
  30.     UISlider *slider = [[[UISlider alloc] initWithFrame:CGRectMake(50, 180, 200, 10)] autorelease];  
  31.     slider.minimumValue = 0.0f;  
  32.     slider.maximumValue = 100.0f;  
  33.     slider.value = 50.0f;  
  34.     [slider addTarget:self action:@selector(onChange:) forControlEvents:UIControlEventTouchUpInside];  
  35.     mySlider = slider;  
  36.     [self.view addSubview:mySlider];  
  37.       
  38.     // 建立一個UISwitch開關按鈕  
  39.     UISwitch *sbtn = [[[UISwitch alloc] initWithFrame:CGRectMake(50, 210, 200, 50)] autorelease];  
  40.     [sbtn addTarget:self action:@selector(onSwitch:) forControlEvents:UIControlEventTouchUpInside];  
  41.     mySwitch = sbtn;  
  42.     [self.view addSubview:mySwitch];  
  43.       
  44.     // 建立一個UISegmentedControl  
  45.     NSArray *btnList = [NSArray arrayWithObjects:@"left",@"center",@"right", nil];  
  46.     UISegmentedControl *sc = [[[UISegmentedControl alloc] initWithItems:btnList] autorelease];  
  47.     sc.frame = CGRectMake(50, 250, 200, 60);  
  48.     [sc addTarget:self action:@selector(onSelect:) forControlEvents:UIControlEventTouchUpInside];  
  49.     mySc = sc;  
  50.     [self.view addSubview:mySc];  
  51.       
  52.     [super viewDidLoad];  
  53. }  
  54.   
  55. // 點擊Button觸發  
  56. - (void)onClick:(id *)sender  
  57. {  
  58.   
  59. }  
  60.   
  61. // 拉動Slider劃塊觸發  
  62. - (void)onChange:(id *)sender  
  63. {  
  64.       
  65. }  
  66.   
  67. // 選擇Switch觸發  
  68. - (void)onSwitch:(id *)sender  
  69. {  
  70.       
  71. }  
  72.   
  73. // 選擇UISegmentedControl觸發  
  74. - (void)onSelect:(id *)sender  
  75. {  
  76. }  
  77. </span>  
#import "MainViewController.h"@interface MainViewController ()@end@implementation MainViewController@synthesize myBtn,mySlider,mySwitch,mySc;- (void)viewDidLoad{    // 載入UIView    UIView *mainView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];    mainView.backgroundColor = [UIColor whiteColor];    self.view = mainView;    [mainView release];        // 建立一個Button按鈕    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];    btn.frame = CGRectMake(100, 30, 57, 57);    [btn setTitle:@"Button" forState:UIControlStateNormal];    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];    [btn setBackgroundImage:[UIImage imageNamed:@"icon.png"] forState:UIControlStateNormal];    [btn addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];    myBtn = btn;    [self.view addSubview:myBtn];            // 建立一個Slider劃塊按鈕    UISlider *slider = [[[UISlider alloc] initWithFrame:CGRectMake(50, 180, 200, 10)] autorelease];    slider.minimumValue = 0.0f;    slider.maximumValue = 100.0f;    slider.value = 50.0f;    [slider addTarget:self action:@selector(onChange:) forControlEvents:UIControlEventTouchUpInside];    mySlider = slider;    [self.view addSubview:mySlider];        // 建立一個UISwitch開關按鈕    UISwitch *sbtn = [[[UISwitch alloc] initWithFrame:CGRectMake(50, 210, 200, 50)] autorelease];    [sbtn addTarget:self action:@selector(onSwitch:) forControlEvents:UIControlEventTouchUpInside];    mySwitch = sbtn;    [self.view addSubview:mySwitch];        // 建立一個UISegmentedControl    NSArray *btnList = [NSArray arrayWithObjects:@"left",@"center",@"right", nil];    UISegmentedControl *sc = [[[UISegmentedControl alloc] initWithItems:btnList] autorelease];    sc.frame = CGRectMake(50, 250, 200, 60);    [sc addTarget:self action:@selector(onSelect:) forControlEvents:UIControlEventTouchUpInside];    mySc = sc;    [self.view addSubview:mySc];        [super viewDidLoad];}// 點擊Button觸發- (void)onClick:(id *)sender{}// 拉動Slider劃塊觸發- (void)onChange:(id *)sender{    }// 選擇Switch觸發- (void)onSwitch:(id *)sender{    }// 選擇UISegmentedControl觸發- (void)onSelect:(id *)sender{}

這裡沒有寫點擊每個控制項的具體實現方法。

UICnotrol Class 下的所有Touch事件

 

[cpp] view plaincopyprint?
  1. UIControlEventTouchDown             
  2. UIControlEventTouchDownRepeat       
  3. UIControlEventTouchDragInside       
  4. UIControlEventTouchDragOutside      
  5. UIControlEventTouchDragEnter        
  6. UIControlEventTouchDragExit         
  7. UIControlEventTouchUpInside         
  8. UIControlEventTouchUpOutside        
  9. UIControlEventTouchCancel           
  10. UIControlEventValueChanged          
  11. UIControlEventEditingDidBegin       
  12. UIControlEventEditingChanged        
  13. UIControlEventEditingDidEnd         
  14. UIControlEventEditingDidEndOnExit   
  15. UIControlEventAllTouchEvents        
  16. UIControlEventAllEditingEvents      
  17. UIControlEventApplicationReserved   
  18. UIControlEventSystemReserved        
  19. UIControlEventAllEvents  
UIControlEventTouchDown           UIControlEventTouchDownRepeat     UIControlEventTouchDragInside     UIControlEventTouchDragOutside    UIControlEventTouchDragEnter      UIControlEventTouchDragExit       UIControlEventTouchUpInside       UIControlEventTouchUpOutside      UIControlEventTouchCancel         UIControlEventValueChanged        UIControlEventEditingDidBegin     UIControlEventEditingChanged      UIControlEventEditingDidEnd       UIControlEventEditingDidEndOnExit UIControlEventAllTouchEvents      UIControlEventAllEditingEvents    UIControlEventApplicationReserved UIControlEventSystemReserved      UIControlEventAllEvents


 

UIButton Class下的所有按鈕樣式

 

[cpp] view plaincopyprint?
  1. UIButtonTypeCustom  
  2. UIButtonTypeRoundedRect  
  3. UIButtonTypeDetailDisclosure  
  4. UIButtonTypeInfoLight  
  5. UIButtonTypeInfoDark  
  6. UIButtonTypeContactAdd  
UIButtonTypeCustomUIButtonTypeRoundedRectUIButtonTypeDetailDisclosureUIButtonTypeInfoLightUIButtonTypeInfoDarkUIButtonTypeContactAdd

 

 


DEMO下載

http://pan.baidu.com/share/link?shareid=73529&uk=101519637

 

相關文章

聯繫我們

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