標籤: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?
- <span style="font-size:10px;">#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</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?
- <span style="font-size:10px;">#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
- {
- }
- </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?
- UIControlEventTouchDown
- UIControlEventTouchDownRepeat
- UIControlEventTouchDragInside
- UIControlEventTouchDragOutside
- UIControlEventTouchDragEnter
- UIControlEventTouchDragExit
- UIControlEventTouchUpInside
- UIControlEventTouchUpOutside
- UIControlEventTouchCancel
- UIControlEventValueChanged
- UIControlEventEditingDidBegin
- UIControlEventEditingChanged
- UIControlEventEditingDidEnd
- UIControlEventEditingDidEndOnExit
- UIControlEventAllTouchEvents
- UIControlEventAllEditingEvents
- UIControlEventApplicationReserved
- UIControlEventSystemReserved
- 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?
- UIButtonTypeCustom
- UIButtonTypeRoundedRect
- UIButtonTypeDetailDisclosure
- UIButtonTypeInfoLight
- UIButtonTypeInfoDark
- UIButtonTypeContactAdd
UIButtonTypeCustomUIButtonTypeRoundedRectUIButtonTypeDetailDisclosureUIButtonTypeInfoLightUIButtonTypeInfoDarkUIButtonTypeContactAdd
DEMO下載
http://pan.baidu.com/share/link?shareid=73529&uk=101519637