首页 > 代码库 > [IOS_UI控件] IOS代码实现常用控件UIButton、UISlider、UISwitch、UISegmentedControl

[IOS_UI控件] IOS代码实现常用控件UIButton、UISlider、UISwitch、UISegmentedControl

 

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 = http://www.mamicode.com/0.0f;"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  
UIButtonTypeCustom
UIButtonTypeRoundedRect
UIButtonTypeDetailDisclosure
UIButtonTypeInfoLight
UIButtonTypeInfoDark
UIButtonTypeContactAdd

 

 


DEMO下载

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