UIControl
UIControl:有控制功能的視圖的父類
只要跟控制有關(guān)的類都是繼承自該類,同時我們通常不會直接用這個類,而用的都是該類的子類
常用方法
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents; // 添加一個事件
- (void)removeTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents; // 移除一個事件
UISwitch
在創(chuàng)建過程中在frame里的size是沒有意義的,因為系統(tǒng)開關(guān)控件大小是固定的
UISwitch相關(guān)屬性
UISwitch *firstSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(100,100,0,0)]; // 創(chuàng)建UISwitch
firstSwitch.onTintColor= [UIColor redColor];// 設(shè)置開關(guān)開啟狀態(tài)的顏色
firstSwitch.tintColor= [UIColor blackColor];// 設(shè)置開關(guān)風(fēng)格顏色
firstSwitch.thumbTintColor= [UIColor blueColor];// 設(shè)置開關(guān)按鈕顏色
[firstSwitch setOn:YES animated:YES];// 手動設(shè)置開關(guān)狀態(tài)?
// firstSwitch.on 獲取開關(guān)當(dāng)前狀態(tài)
[self.view addSubview:firstSwitch]; // 將UISwitch放入視圖中
UISlider
滑塊通常用于控制視頻或者音頻的播放的進度,控制音量等操作
UISlider相關(guān)屬性
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(100,200,100,40)]; // 創(chuàng)建UISlider
slider.minimumValue=0.0;// 設(shè)置滑塊的最小值
slider.maximumValue=100.0;// 設(shè)置滑塊的最大值
slider.value=50;// 設(shè)置滑塊的值
slider.minimumTrackTintColor= [UIColorblackColor];// 設(shè)置滑塊劃過區(qū)域的顏色
[slider addTarget:selfaction:@selector(slider:) forControlEvents:UIControlEventValueChanged];// 滑塊添加事件,觸發(fā)方式為值的改變
UISegmentedControl
分段控件常用在不同類別的信息之間選擇,或者切換不同的視圖
UISegmentedControl相關(guān)屬性
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"你",@"我"]];segmentedControl.frame = CGRectMake(100,300,100,40); // 創(chuàng)建UISegmentedControl
// selectedSegmentIndex 指定被選中的分段
// segmentedControlStyle 設(shè)置樣式
segmentedControl.momentary=YES;// 設(shè)置在點擊后是否恢復(fù)原樣
// [segmentedControl setTitle:nil forSegmentAtIndex:0]; 為指定下標(biāo)的分段這是title
// [segmentedControl setImage: forSegmentAtIndex:]; 為指定小標(biāo)的分段這只圖片[segmentedControl setEnabled:NO forSegmentAtIndex:1]; // 設(shè)置指定索引是否可點
segmentedControl.tintColor= [UIColor grayColor];// 樣式顏色
[segmentedControl?addTarget:selfaction:@selector(segmentedControl:) forControlEvents:UIControlEventValueChanged];
UIPageControl
UIPageControl相關(guān)屬性
UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(100,400,100,40)]; // 創(chuàng)建UIPageControl
pageControl.backgroundColor= [UIColorgrayColor];
pageControl.numberOfPages=3;// 指定頁數(shù)
pageControl.currentPage=1;// 設(shè)置當(dāng)前頁數(shù),第一頁為0
pageControl.currentPageIndicatorTintColor= [UIColor redColor];// 設(shè)置當(dāng)前選中頁數(shù)的顏色
pageControl.pageIndicatorTintColor= [UIColor whiteColor];// 沒有選中頁數(shù)的顏色
@implementationViewController
- (void)sliderAction:(UISlider*)slider {
NSLog(@"%.2f", slider.value);
}
- (void)segmentedAction:(UISegmentedControl*)segment {
switch(segment.selectedSegmentIndex) {
case0:
NSLog(@"第一");
self.view.backgroundColor= [UIColorgreenColor];
break;
case1:
NSLog(@"第二");
self.view.backgroundColor= [UIColororangeColor];
break;
case2:
NSLog(@"第三");
self.view.backgroundColor= [UIColorlightGrayColor];
break;
default:
break;
}
}
- (void)pageControlAction:(UIPageControl*)pageControl {
NSLog(@"翻頁");
}
- (void)viewDidLoad {
[superviewDidLoad];
//開關(guān)
UISwitch*newSwitch = [[UISwitchalloc]initWithFrame:CGRectMake(200,80,0,0)];//開關(guān)大小只能是系統(tǒng)默認值
[self.viewaddSubview:newSwitch];
//設(shè)置開關(guān)開啟狀態(tài)的顏色
newSwitch.onTintColor= [UIColorblueColor];
//設(shè)置開關(guān)風(fēng)格顏色
newSwitch.tintColor= [UIColororangeColor];
//設(shè)置開關(guān)按鈕顏色
newSwitch.thumbTintColor= [UIColorblackColor];
//設(shè)置開關(guān)是否開啟
//??? newSwitch.on = NO;
//手動設(shè)置開關(guān)狀態(tài)
[newSwitchsetOn:YESanimated:NO];
//滑塊
UISlider*slider = [[UISlideralloc]initWithFrame:CGRectMake(20,150,350,40)];
[self.viewaddSubview:slider];
//設(shè)置slider的最大、最小值
slider.maximumValue=5.0;
slider.minimumValue=0.0;
//設(shè)置劃過區(qū)域的顏色
slider.minimumTrackTintColor= [UIColorredColor];
//設(shè)置未劃過區(qū)域的顏色
slider.maximumTrackTintColor= [UIColorgreenColor];
//設(shè)置滑塊的顏色
slider.thumbTintColor= [UIColorblueColor];
//添加點擊事件
[slideraddTarget:selfaction:@selector(sliderAction:)forControlEvents:UIControlEventValueChanged];
//在滑塊終點加載圖片
//??? [slider setMaximumTrackImage:image forState:UIControlStateNormal];
//分段控件
UISegmentedControl*segment = [[UISegmentedControlalloc]initWithItems:@[@"第一",@"第二",@"第三"]];
segment.frame=CGRectMake(90,300,250,40);
[self.viewaddSubview:segment];
//指定segment的顏色
segment.tintColor= [UIColorpurpleColor];
//添加事件
[segmentaddTarget:selfaction:@selector(segmentedAction:)forControlEvents:UIControlEventValueChanged];
//UIPageControl
UIPageControl*pageControl = [[UIPageControlalloc]initWithFrame:CGRectMake(90,600,200,40)];
//設(shè)置頁數(shù)
pageControl.numberOfPages=5;
//設(shè)置當(dāng)前頁默認是0(第一頁)
pageControl.currentPage=1;
pageControl.backgroundColor= [UIColorbrownColor];
//設(shè)置圓點的顏色
pageControl.currentPageIndicatorTintColor= [UIColorredColor];
pageControl.pageIndicatorTintColor= [UIColorwhiteColor];
[self.viewaddSubview:pageControl];
//添加事件
[pageControladdTarget:selfaction:@selector(pageControlAction:)forControlEvents:UIControlEventValueChanged];
}
@end