今天為大家總結(jié)UIControl及其子類的一些常用屬性.
代碼全在ViewController.m里.
我習慣先在-(void)viewDidLoad里修改下self.view.backgroundColor, 一般改為白色.
接下來介紹一些常用控件的屬性, 代碼全在-(void)loadView里.
UISwitch : 開關控件
UISwitch *s = [[UISwitch alloc] initWithFrame:CGRectMake(100, 200, 1000, 1000)];//寬和高是已經(jīng)寫好了的,想自定義只能重寫
//常用屬性:
//(1)外框顏色:
s.tintColor = [UIColor blueColor];
//(2)打開狀態(tài)下的底色:
s.onTintColor = [UIColor redColor];
//(3)圓圈按鈕的顏色:
s.thumbTintColor = [UIColor yellowColor];
//(4)設置開關狀態(tài):(on)
// [s setOn:YES];
[s setOn:YES animated:YES];//是否使用動畫效果
//(5)為開關按鈕添加觸發(fā)方法:
[s addTarget:self action:@selector(didClickedSwitch:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:s];
UISlider : 滑塊控件
//初始化imageView:
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 166, 144)];
NSMutableArray *imageArray = [NSMutableArray array];
for (NSInteger i = 0; i <= 21; i++) {
NSString *imageName = [NSString stringWithFormat:@"Zombie%ld.tiff", i];
UIImage *image = [UIImage imageNamed:imageName];
[imageArray addObject:image];
}
self.imageView.animationImages = imageArray;
self.imageView.animationDuration = 0.5;
[self.imageView startAnimating];//開始動畫
[self.view addSubview:_imageView];
[_imageView release];
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(20, 300, 335, 50)];
//常用屬性:
//(1)設置值的范圍:
slider.minimumValue = 0;
slider.maximumValue = 2;
//(2)添加滑動觸發(fā)方法:
[slider addTarget:self action:@selector(didSlideSlider:) forControlEvents:UIControlEventValueChanged];//值發(fā)生改變就觸發(fā)!
//(3)設置初始值:
slider.value = 50.0f;
//(4)修改劃過區(qū)域的顏色:
slider.minimumTrackTintColor = [UIColor redColor];
//(5)未劃過區(qū)域的顏色:
slider.maximumTrackTintColor = [UIColor yellowColor];
//(6)設置滑塊圖片:
// [slider setThumbImage:[UIImage imageNamed:@"3.jpg"] forState:UIControlStateNormal];
//(7)設置滑條左右兩邊圖片:
// [slider setMinimumValueImage:[UIImage imageNamed:@"1.png"]];
// [slider setMaximumValueImage:[UIImage imageNamed:@"2.png"]];
[self.view addSubview:slider];
UISegmentControl : 分段控件
//初始化三個視圖:
//左邊:
self.leftView = [[UIView alloc] initWithFrame:CGRectMake(20, 100, 335, 500)];
self.leftView.backgroundColor = [UIColor redColor];
[self.view addSubview:_leftView];
//中間:
self.middleView = [[UIView alloc] initWithFrame:self.leftView.frame];
self.middleView.backgroundColor = [UIColor blueColor];
[self.view addSubview:_middleView];
//右邊:
self.rightView = [[UIView alloc] initWithFrame:self.middleView.frame];
self.rightView.backgroundColor = [UIColor greenColor];
[self.view addSubview:_rightView];
//將左視圖移動到最上面:
[self.view bringSubviewToFront:_leftView];
UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:@[@"左邊", @"中間", @"右邊"]];
seg.frame = CGRectMake(20, 50, 335, 50);
//為seg添加點擊事件:
[seg addTarget:self action:@selector(didClickedSegmentedControl:) forControlEvents:UIControlEventValueChanged];
//(1)修改渲染色:
seg.tintColor = [UIColor redColor];
//(2)設置某個分區(qū)為圖片
UIImage *img = [UIImage imageNamed:@"5.jpg"];
img = [img imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];//取消圖片的渲染效果!
[seg setImage:img forSegmentAtIndex:1];
//(3)修改指定下標的title:
[seg setTitle:@"dog左邊" forSegmentAtIndex:0];
//(4)修改點擊狀態(tài)(點擊后變回原來的顏色)
seg.momentary = NO;//默認是NO
//(5)設置某個下標的分段不可點擊:
[seg setEnabled:YES forSegmentAtIndex:0];
// [seg setEnabled:NO];//整個控件失效
//(6)添加一個新的分段:
[seg insertSegmentWithTitle:@"NBA" atIndex:1 animated:NO];
//(7)移除某個分段:
[seg removeSegmentAtIndex:1 animated:NO];
//(8)設置默認選中的下標:
[seg setSelectedSegmentIndex:0];
[self.view addSubview:seg];
因為分段控件這里用了三個視圖, 需要將leftView, middleView和rightView三個視圖寫為屬性, 如下.
#import "ViewController.h"
@interface ViewController ()
@property(nonatomic, retain)UIImageView *imageView;
@property(nonatomic, retain)UIView *leftView;
@property(nonatomic, retain)UIView *middleView;
@property(nonatomic, retain)UIView *rightView;
@end
UIPageControl : 分頁控制
UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(100, 567, 175, 50)];
pageControl.backgroundColor = [UIColor grayColor];
//(1)設置小點的個數(shù):
pageControl.numberOfPages = 5;
//(2)為其添加觸發(fā)方法:
[pageControl addTarget:self action:@selector(didClickedPageControl:) forControlEvents:UIControlEventValueChanged];
//設置小點默認的顏色:
pageControl.pageIndicatorTintColor = [UIColor blueColor];
//設置被選中的小點顏色:
pageControl.currentPageIndicatorTintColor = [UIColor yellowColor];
[self.view addSubview:pageControl];
常用屬性在loadView里寫, 里邊一些控件涉及一些方法, 要寫在loadView外. 接下來是上邊使用到的方法. 使用地點請看備注.
#pragma mark -- 開關觸發(fā)方法:
-(void)didClickedSwitch:(UISwitch *)sender
{
if (sender.on == YES) {
NSLog(@"鏈接wifi");
}
else
{
NSLog(@"關閉wifi");
}
}
#pragma mark -- 滑塊控件觸發(fā)方法:
-(void)didSlideSlider:(UISlider *)sender
{
//獲取滑塊當前值:
NSLog(@"%.2f", sender.value);
//設置僵尸動畫的時間間隔:
self.imageView.animationDuration = sender.maximumValue - sender.value;
[self.imageView startAnimating];
}
#pragma mark -- 分段控件的觸發(fā)方法:
-(void)didClickedSegmentedControl:(UISegmentedControl *)seg
{
NSLog(@"點擊了第%ld個分區(qū)", seg.selectedSegmentIndex);
//顯示相應分區(qū)視圖:
switch (seg.selectedSegmentIndex) {
case 0:
[self.view bringSubviewToFront:_leftView];
break;
case 1:
[self.view bringSubviewToFront:_middleView];
break;
case 2:
[self.view bringSubviewToFront:_rightView];
break;
default:
break;
}
}
#pragma mark -- 分頁控件觸發(fā)方法:
-(void)didClickedPageControl:(UIPageControl *)pageControl
{
NSLog(@"當前選中的是第%ld張圖", pageControl.currentPage);
}
其實我也剛學不久, 上邊都是自己的學習筆記, 適合一些初學的朋友們, 希望大家多提意見, 也希望能和大家一起交流, 共同進步.