zoukankan      html  css  js  c++  java
  • [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

    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
    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事件

    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下的所有按钮样式

    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

  • 相关阅读:
    [leetcode.com]算法题目
    [leetcode.com]算法题目
    [leetcode.com]算法题目
    [实战演练]2014年人人公司应届生校招技术笔试题
    [杂谈]笔试中一些数字逻辑推理(非技术)
    [实战演练]腾讯2013年校招软件开发类笔试题目(选择题部分)
    [实战演练]史上最长最醒目的队名
    [Linux]在linux中,常常用到ctrl和其他按键组合,常用的有哪些及意义呢
    [linux] grep awk sort uniq学习
    [IDEA] 快捷键学习
  • 原文地址:https://www.cnblogs.com/webapplee/p/3783917.html
Copyright © 2011-2022 走看看