[IOS_UI Control] iOS code implements common Controls UIButton, UISlider, Uiswitch, Uisegmentedcontrol

Source: Internet
Author: User

The most commonly used controls in iOS UIButton, UISlider, Uiswitch, Uisegmentedcontrol by the Xib file drag generation is very simple, in fact, the implementation of code is the same simple, of course, the implementation of code can grasp more things.

The 4 types of controls mentioned in, UIButton buttons, uislider sliders, Uiswitch switches, Uisegmentedcontrol categories

First create an empty application project named Codecontrols


The AppDelegate.h and APPDELEGATE.M files are the same as The iOS code implementation in Hello World


MainViewController.h

[CPP]View Plaincopyprint?< param name= "allowfullscreen" value= "false" >
    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. Li class= "alt" > @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. @interface Mainviewcontroller ()
  3. @end
  4. @implementation Mainviewcontroller
  5. @synthesize MYBTN,MYSLIDER,MYSWITCH,MYSC;
  6. -(void) viewdidload
  7. {
  8. //Load UIView   
  9. UIView *mainview = [[UIView alloc] Initwithframe:[[uiscreen Mainscreen] applicationframe]];
  10. Mainview.backgroundcolor = [Uicolor Whitecolor];
  11. Self.view = MainView;
  12. [MainView release];
  13. //Create a button   
  14. UIButton *btn = [UIButton buttonwithtype:uibuttontyperoundedrect];
  15. Btn.frame = CGRectMake (100, 30, 57, 57);
  16. [BTN settitle:@"button" Forstate:uicontrolstatenormal];
  17. [btn Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal];
  18. [Btn setbackgroundimage:[uiimage imagenamed:@"Icon.png"] forstate:uicontrolstatenormal];
  19. [Btn addtarget:self Action: @selector (OnClick:) forcontrolevents:uicontroleventtouchupinside];
  20. MYBTN = BTN;
  21. [Self.view ADDSUBVIEW:MYBTN];
  22. //Create a Slider slider button   
  23. UISlider *slider = [[[[UISlider Alloc] Initwithframe:cgrectmake ([[]] autorelease];
  24. Slider.minimumvalue = 0.0f;
  25. Slider.maximumvalue = 100.0f;
  26. Slider.value = 50.0f;
  27. [Slider addtarget:self action: @selector (OnChange:) forcontrolevents:uicontroleventtouchupinside];
  28. Myslider = slider;
  29. [Self.view Addsubview:myslider];
  30. //Create a uiswitch switch button   
  31. Uiswitch *sbtn = [[[[Uiswitch Alloc] Initwithframe:cgrectmake ((a) [[]] autorelease];
  32. [Sbtn addtarget:self Action: @selector (Onswitch:) forcontrolevents:uicontroleventtouchupinside];
  33. Myswitch = sbtn;
  34. [Self.view Addsubview:myswitch];
  35. //Create a Uisegmentedcontrol   
  36. Nsarray *btnlist = [Nsarray arraywithobjects:@"left"@"center", @"  Right ", nil);
  37. Uisegmentedcontrol *SC = [[[Uisegmentedcontrol alloc] initwithitems:btnlist] autorelease];
  38. Sc.frame = CGRectMake (50, 250, 200, 60);
  39. [SC addtarget:self action: @selector (OnSelect:) forcontrolevents:uicontroleventtouchupinside];
  40. MYSC = SC;
  41. [Self.view ADDSUBVIEW:MYSC];
  42. [Super Viewdidload];
  43. }
  44. //Click button to trigger   
  45. -(void) OnClick: (ID *) Sender
  46. {
  47. }
  48. //Pull slider slider trigger   
  49. -(void) OnChange: (ID *) Sender
  50. {
  51. }
  52. //SELECT Switch Trigger   
  53. -(void) Onswitch: (ID *) Sender
  54. {
  55. }
  56. //select Uisegmentedcontrol trigger   
  57. -(void) OnSelect: (ID *) Sender
  58. {
  59. }
  60. </span>
#import "MainViewController.h" @interface Mainviewcontroller () @end @implementation mainviewcontroller@synthesize mybtn,myslider,myswitch,mysc;-(void) viewdidload{//Load UIView UIView *mainview = [[UIView alloc] Initwithframe:[[uis    Creen Mainscreen] applicationframe];    Mainview.backgroundcolor = [Uicolor Whitecolor];    Self.view = MainView;        [MainView release];    Create a 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]; Create a slider-stroke button UISlider *slider = [[[[UISlider Alloc] Initwithframe:cgrectmake (+, +, +)] 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];    Create a uiswitch switch button uiswitch *sbtn = [[Uiswitch alloc] Initwithframe:cgrectmake ((a) autorelease];    [Sbtn addtarget:self Action: @selector (Onswitch:) forcontrolevents:uicontroleventtouchupinside];    Myswitch = sbtn;        [Self.view Addsubview:myswitch];    Create a 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];} Click button Trigger-(void) OnClick: (ID *) Sender{}//Pull Slider slider trigger-(void) OnChange: (ID *) sender{}//Select Switch Trigger-(void) Onswitch: (ID *) sender{}//Select Uisegmentedcontr OL trigger-(void) OnSelect: (ID *) sender{}

There is no specific implementation method for writing a click on each control.

All touch events under the Uicnotrol Class

[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


All button styles under the UIButton class

[CPP]View Plaincopyprint?
    1. Uibuttontypecustom
    2. Uibuttontyperoundedrect
    3. Uibuttontypedetaildisclosure
    4. Uibuttontypeinfolight
    5. Uibuttontypeinfodark
    6. Uibuttontypecontactadd
Uibuttontypecustomuibuttontyperoundedrectuibuttontypedetaildisclosureuibuttontypeinfolightuibuttontypeinfodarkuibuttontyp Econtactadd


Demo download

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.