转自

 

IOS中最常用到的控件UIButton、UISlider、UISwitch、UISegmentedControl通过Xib文件拖动生成非常简单,其实用代码实现也是一样的简单,当然,用代码实现能够掌握到更多的东西。

上图中包涵提到的4种控件,UIButton按钮、UISlider滑块、UISwitch开关、UISegmentedControl分类

首先创建一个名为CodeControls的Empty Application项目

AppDelegate.h和AppDelegate.m文件中和IOS代码实现Hello World中的一样

 

MainViewController.h

 

  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>  

MainViewController.m

 

 

  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>  

这里没有写点击每个控件的具体实现方法。

UICnotrol Class 下的所有Touch事件

 

  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  


 

UIButton Class下的所有按钮样式

 

  1. UIButtonTypeCustom  
  2. UIButtonTypeRoundedRect  
  3. UIButtonTypeDetailDisclosure  
  4. UIButtonTypeInfoLight  
  5. UIButtonTypeInfoDark  
  6. UIButtonTypeContactAdd  

 

 

 

DEMO下载

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

arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()