先告訴你們一個好消息, 今天個人博客正式開通了,歡迎你們的光臨.數組
今天咱們學習了UIControl以及他的一些子類而且也學習了一些經常使用的方法其中他的子類有UIButton、UIDatePicker、UIPageControl、UISegmentedControl、UITextField、UISlider、UISwitch。其中今天咱們學習的有UISegmentedControl、UISlider和UISwitch。下面我就簡單地用幾行代碼來講明其用途,若是有疑問或者問題請你們不吝賜教:dom
1.UIcontrol:UIView的子類,是全部控制控件的基類ide
control = [[UIControl alloc] initWithFrame:CGRectMake(50, 350, 200, 200)]; control.backgroundColor = [UIColor blackColor]; [control addTarget:self action:@selector(control:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:control]; [control release];
2.下面是UIslider的例子:oop
slider = [[UISlider alloc] initWithFrame:CGRectMake(0, 100, 220, 31)]; NSLog(@"%@", slider); slider.backgroundColor = [UIColor cyanColor]; [slider addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged]; //默認的value取值範圍時0-1; //若是最小值比最大值大,slider不能滑動 slider.minimumValue = 0; slider.maximumValue = 255; [self.view addSubview:slider]; [slider release]; slider1 = [[UISlider alloc] initWithFrame:CGRectMake(0, 151, 220, 31)]; NSLog(@"%@", slider); slider1.backgroundColor = [UIColor cyanColor]; [slider1 addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged]; slider1.minimumValue = 0; slider1.maximumValue = 255; [self.view addSubview:slider1]; [slider1 release]; slider2 = [[UISlider alloc] initWithFrame:CGRectMake(0, 200, 220, 31)]; NSLog(@"%@", slider); slider2.backgroundColor = [UIColor cyanColor]; [slider2 addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged]; slider2.minimumValue = 0; slider2.maximumValue = 255; [self.view addSubview:slider2]; [slider2 release];
3.下面咱們來作個練習:經過改變UISlider進而用UILabel來顯示其示數,並由此來隨機改變UIControl區域的顏色學習
- (void)viewDidLoad { [super viewDidLoad]; //UIControl:UIView的子類,是全部控制控件的基類 control = [[UIControl alloc] initWithFrame:CGRectMake(50, 350, 200, 200)]; control.backgroundColor = [UIColor blackColor]; [control addTarget:self action:@selector(control:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:control]; [control release]; //UISegmentedControl:分段控制器 //注意點 //1.items用於segmentControl的文本顯示,因此items數組中的元素必須是NSString的類型或UIImage類型的 //2.分段的下標從0開始 //設置某個分段的寬度,若是沒有被設置寬度,每一個分段寬度相等 // [segment setWidth:200 forSegmentAtIndex:1]; NSArray *array = @[@"是", @"否"]; UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:array]; segment.frame = CGRectMake(0, 40, 320, 40); [segment addTarget:self action:@selector(changeTitle:) forControlEvents:UIControlEventValueChanged]; segment.tintColor = [UIColor blackColor]; //設置分段控制器的初始選中值(不會觸發關聯的方法) segment.selectedSegmentIndex = 0; //設置分段的圖片 UIImage *image = [UIImage imageNamed:@"1"]; [segment setImage:image forSegmentAtIndex:0]; [self.view addSubview:segment]; [segment release]; //UISlider:滑塊控件,繼承於UIControl //slider:控件的高度是31pt,不能改變 //frame:slider的可觸摸區域 label = [[UILabel alloc] initWithFrame:CGRectMake(240, 100, 70, 31)]; label.textColor = [UIColor blackColor]; label.text = @"0"; [self.view addSubview:label]; [label release]; label1 = [[UILabel alloc] initWithFrame:CGRectMake(240, 151, 70, 31)]; label1.textColor = [UIColor blackColor]; label1.text = @"0"; [self.view addSubview:label1]; [label1 release]; label2 = [[UILabel alloc] initWithFrame:CGRectMake(240, 200, 70, 31)]; label2.textColor = [UIColor blackColor]; label2.text = @"0"; [self.view addSubview:label2]; [label2 release]; slider = [[UISlider alloc] initWithFrame:CGRectMake(0, 100, 220, 31)]; NSLog(@"%@", slider); slider.backgroundColor = [UIColor cyanColor]; [slider addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged]; //默認的value取值範圍時0-1; //若是最小值比最大值大,slider不能滑動 slider.minimumValue = 0; slider.maximumValue = 255; [self.view addSubview:slider]; [slider release]; slider1 = [[UISlider alloc] initWithFrame:CGRectMake(0, 151, 220, 31)]; NSLog(@"%@", slider); slider1.backgroundColor = [UIColor cyanColor]; [slider1 addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged]; slider1.minimumValue = 0; slider1.maximumValue = 255; [self.view addSubview:slider1]; [slider1 release]; slider2 = [[UISlider alloc] initWithFrame:CGRectMake(0, 200, 220, 31)]; NSLog(@"%@", slider); slider2.backgroundColor = [UIColor cyanColor]; [slider2 addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged]; slider2.minimumValue = 0; slider2.maximumValue = 255; [self.view addSubview:slider2]; [slider2 release]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)changeTitle:(UISegmentedControl *)title { if (title.selectedSegmentIndex == 0) { [title setTitle:@"否" forSegmentAtIndex:0]; [title setTitle:@"是" forSegmentAtIndex:1]; } else { [title setTitle:@"否" forSegmentAtIndex:1]; [title setTitle:@"是" forSegmentAtIndex:0]; } } - (void)valueChange:(UISlider *)value { NSLog(@"%f", value.value); if (value == slider) { label.text = [NSString stringWithFormat:@"%.2f", value.value]; } else if (value == slider1) { label1.text = [NSString stringWithFormat:@"%.2f", value.value]; } else { label2.text = [NSString stringWithFormat:@"%.2f", value.value]; } control.backgroundColor = [UIColor colorWithRed:[label.text integerValue] / 255. green:[label1.text integerValue] / 255. blue:[label2.text integerValue] / 255. alpha:1.0]; } - (void)control:(UIControl *)control1 { float a = arc4random() % 256; float b = arc4random() % 256; float c = arc4random() % 256; control1.backgroundColor = [UIColor colorWithRed:a / 255.0 green:b / 255.0 blue:c / 255.0 alpha:1.0]; label.text = [NSString stringWithFormat:@"%.2f", a]; label1.text = [NSString stringWithFormat:@"%.2f", b]; label2.text = [NSString stringWithFormat:@"%.2f", c]; // slider.value = [label.text floatValue]; // slider1.value = [label1.text floatValue]; // slider2.value = [label2.text floatValue]; //設置slider的value,而且能夠指定是否加載動畫,讓整個過程更天然 [slider setValue:a animated:YES]; [slider1 setValue:b animated:YES]; [slider2 setValue:c animated:YES]; }
這個即是上面實現的結果,有什麼問題但願你們提出哈。動畫
4.對於UISwitch咱們今天並無學習多少,老師只是簡單地提了下,下面就和你們分享下其具體作法spa
//UISwitch,開關控件,繼承於UIControl //UISwitch,有固定的大小,寬51,高31 //設置Switch的狀態 UISwitch *swichControl = [[UISwitch alloc] initWithFrame:CGRectMake(40, 40, 100, 100)]; swichControl.on = YES; [swichControl addTarget:self action:@selector(changeCondition:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:swichControl]; [swichControl release];
以上即是今天上課的主要內容了,但願能夠和你們共同進步,一塊兒學習。線程
下面附上今天的做業練習:code
1.使⽤用UISegmentedControl切換登陸界⾯面、註冊界⾯面,找回密碼界
⾯面。
注:UISegmentedControl放在屏幕最上⽅方,登陸、註冊、找回密碼 界⾯面緊接着UISegmentedControl下⽅方顯⽰示。orm
- (void)viewDidLoad { [super viewDidLoad]; UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:@[@"登錄界面", @"註冊界面", @"找回密碼界面"]]; segment.frame = CGRectMake(0, 20, 320, 30); [segment addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:segment]; [segment release]; } - (void)change:(UISegmentedControl *)segmented { for (UIView *view in self.view.subviews) { NSLog(@"%@", view); NSLog(@"%@", segmented); if (view != segmented) { [view removeFromSuperview]; } } switch (segmented.selectedSegmentIndex) { case 0: { pass *password = [[pass alloc] init]; [self.view addSubview:password]; [password release]; break; } case 1: { zhuce *zhu = [[zhuce alloc] init]; [self.view addSubview:zhu]; [zhu release]; break; } case 2: { findPassWord *find = [[findPassWord alloc] init]; [self.view addSubview:find]; [find release]; } default: break; } }
- (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.frame = CGRectMake(0, 50, 320, 568); UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(40, 70, 60, 30)]; label.text = @"用戶名"; label.backgroundColor = [UIColor redColor]; label.textColor = [UIColor blackColor]; label.textAlignment = NSTextAlignmentRight; [self addSubview:label]; [label release]; UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(120, 70, 180, 30)]; field.borderStyle = UITextBorderStyleRoundedRect; field.keyboardType = UIKeyboardTypeNumberPad; field.placeholder = @"請輸入用戶名或郵箱"; field.delegate = self; [self addSubview:field]; [field release]; //設置密碼 UILabel *passWord = [[UILabel alloc] initWithFrame:CGRectMake(40, 120, 60, 30)]; passWord.text = @"密碼"; passWord.backgroundColor = [UIColor redColor]; passWord.textColor = [UIColor blackColor]; passWord.textAlignment = NSTextAlignmentRight; [self addSubview:passWord]; [passWord release]; UITextField *pass = [[UITextField alloc] initWithFrame:CGRectMake(120, 120, 180, 30)]; pass.placeholder = @"請輸入密碼"; pass.secureTextEntry = YES; pass.keyboardType = UIKeyboardTypeNumbersAndPunctuation; pass.borderStyle = UITextBorderStyleRoundedRect; pass.delegate = self; [self addSubview:pass]; [pass release]; //設置登錄按鈕 UIImage *image = [UIImage imageNamed:@"1"]; UIButton *loading = [UIButton buttonWithType:UIButtonTypeCustom]; loading.frame = CGRectMake(60, 180, 70, 30); [loading setBackgroundImage:image forState:UIControlStateNormal]; [loading setTitle:@"登錄" forState:UIControlStateNormal]; [loading setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [self addSubview:loading]; UIButton *cencle = [UIButton buttonWithType:UIButtonTypeCustom]; cencle.frame = CGRectMake(200, 180, 70, 30); [cencle setBackgroundImage:image forState:UIControlStateNormal]; [cencle setTitle:@"取消" forState:UIControlStateNormal]; [cencle setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [self addSubview:cencle]; } return self; }
#import "zhuce.h" @implementation zhuce - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.frame = CGRectMake(0, 50, 320, 568); // Initialization code NSArray *array = @[@"郵箱", @"請輸入郵箱", @"地址", @"請輸入地址", @"電話", @"請輸入電話", @"帳號", @"請輸入帳號", @"密碼", @"請輸入密碼", @"確認密碼", @"請再次輸入密碼"]; int a = 0; int b = 0; for (int i = 0; i < 6; i++) { UILabel *emailLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 50 + i * 50, 70, 30)]; emailLabel.text = array[b]; emailLabel.textAlignment = NSTextAlignmentRight; [self addSubview:emailLabel]; [emailLabel release]; UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(110, 50 + 50 * i, 160, 30)]; field.placeholder = array[b + 1]; field.borderStyle = UITextBorderStyleRoundedRect; [self addSubview:field]; [field release]; field.delegate = self; a++; b = a * 2; } UIButton *password = [UIButton buttonWithType:UIButtonTypeSystem]; [password setTitle:@"登錄" forState:UIControlStateNormal]; password.frame = CGRectMake(60, 350, 60, 30); [self addSubview:password]; UIButton *cancel = [UIButton buttonWithType:UIButtonTypeSystem]; [cancel setTitle:@"登錄" forState:UIControlStateNormal]; cancel.frame = CGRectMake(180, 350, 60, 30); [self addSubview:cancel]; } return self; }
#import "findPassWord.h" @implementation findPassWord - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code self.frame = CGRectMake(0, 50, 320, 568); UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(30, 60, 50, 30)]; label.text = @"郵箱"; [self addSubview:label]; [label release]; UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(100, 60, 200, 30)]; field.placeholder = @"請輸入郵箱或電話"; field.borderStyle = UITextBorderStyleRoundedRect; field.delegate = self; [self addSubview:field]; [field release]; UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; button.frame = CGRectMake(100, 120, 60, 30); [button setTitle:@"肯定" forState:UIControlStateNormal]; [self addSubview:button]; UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem]; button1.frame = CGRectMake(160, 120, 60, 30); [button1 setTitle:@"取消" forState:UIControlStateNormal]; [self addSubview:button1]; } return self; }
效果圖爲:
以上圖片實現了簡單窗口之間的切換。
對了今天還講了一個簡單地動畫製做,在製做過程當中,首先要找到素材,也就是簡單的幾張圖片,將其放到Xcode中去利用UISwitch進行圖片的連續切換,從而實現動畫,下面開始進入代碼部分:
- (void)viewDidLoad { [super viewDidLoad]; beginButton = [UIButton buttonWithType:UIButtonTypeCustom]; beginButton.frame = CGRectMake(70, 500, 60, 30); [beginButton setTitle:@"開始" forState:UIControlStateNormal]; [beginButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [beginButton addTarget:self action:@selector(begin:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:beginButton]; stopButton = [UIButton buttonWithType:UIButtonTypeCustom]; stopButton.frame = CGRectMake(180, 500, 60, 30); [stopButton setTitle:@"中止" forState:UIControlStateNormal]; [stopButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [stopButton addTarget:self action:@selector(begin:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:stopButton]; label = [[UILabel alloc] initWithFrame:CGRectMake(70, 450, 300, 30)]; [self.view addSubview:label]; [label release]; UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(0, 400, 300, 31)]; slider.backgroundColor = [UIColor blueColor]; [slider addTarget:self action:@selector(chang:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:slider]; [slider release]; imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"huoju_1.tiff"]]; imageView.bounds = CGRectMake(0, 0, 79 * 2, 106 * 2); imageView.center = CGPointMake(self.view.center.x, self.view.center.y - 100); //imageView的動畫 NSMutableArray *images = [NSMutableArray array]; for (int i = 1; i < 8; i++) { [images addObject:[UIImage imageNamed:[NSString stringWithFormat:@"huoju_%d.tiff", i]]]; } //設置imageView的動畫數組,數組內存放的必須是UIIMage類型的對象 imageView.animationImages = images; //設置動畫時長(圖片切換的時間間隔) imageView.animationDuration = 1; //設置動畫重複次數(0表明無限循環) imageView.animationRepeatCount = 0; [self.view addSubview:imageView]; [imageView release]; //開啓動畫 // [imageView startAnimating]; // // //中止動畫 // [imageView stopAnimating]; //UISwitch,開關控件,繼承於UIControl //UISwitch,有固定的大小,寬51,高31 //設置Switch的狀態 UISwitch *swichControl = [[UISwitch alloc] initWithFrame:CGRectMake(40, 40, 100, 100)]; swichControl.on = YES; [swichControl addTarget:self action:@selector(changeCondition:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:swichControl]; [swichControl release]; //NSTimer //建立方法1. // [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(print:) userInfo:@"asfg" repeats:YES]; //建立方法2. // NSTimer *timer2 = [[NSTimer alloc] initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:5] interval:1 target:self selector:@selector(print:) userInfo:nil repeats:YES]; // //每一個線程(thread)中都有一個事件循環(NSRunLoop),來實時的判斷是否該執行某些事件或方法 // //加入到事件循環中,通知init方式建立的timer不可以執行,須要加入到NSRunLoop中 // [[NSRunLoop currentRunLoop] addTimer:timer2 forMode:NSDefaultRunLoopMode]; // [timer2 release]; // // //當即執行第一次操做 // [timer2 fire]; // //中止計時器 // [timer2 invalidate]; //建立方法3. NSTimer *timer3 = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(print:) userInfo:nil repeats:YES]; // [[NSRunLoop currentRunLoop] addTimer:timer3 forMode:NSDefaultRunLoopMode]; //添加到主線程的事件循環 [[NSRunLoop mainRunLoop] addTimer:timer3 forMode:NSDefaultRunLoopMode]; } - (void)print:(NSTimer *)timer { NSLog(@"逗逗"); } - (void)changeCondition:(UISwitch *)condition { // condition.on = YES; [imageView startAnimating]; } - (void)chang:(UISlider *)change { imageView.animationDuration = change.value; label.text = [NSString stringWithFormat:@"當前速度爲%.2f秒每張", change.value]; [imageView startAnimating]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)begin:(UIButton *)begins { if (begins == beginButton) { [imageView startAnimating]; } else { [imageView stopAnimating]; } }
效果圖以下所示:
好了今天就先到這裏了,要回家了,望你們多多指點,不足之處儘管指出,今天好冷明天見各位,see you!