UIControl 及其子类
#import "MainViewController.h"
//1,宏定义
#define kScreenHeight CGRectGetHeight(self.view.bounds)//屏幕高度宏
#define kScreenWidth CGRectGetWidth(self.view.bounds)//屏幕宽度宏
@interface MainViewController ()
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//2分段控件
UISegmentedControl*segmentedControl=[[[UISegmentedControl alloc]initWithItems:@[@"左视图",@"右视图"]]autorelease];
segmentedControl.frame=CGRectMake(20, 50, kScreenWidth-40, 30);
segmentedControl.selectedSegmentIndex=0;
[segmentedControl addTarget:self action:@selector(handleSegmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.tag=100;
[self.view addSubview:segmentedControl];
//3,两个视图
//视图的宽度和间距
CGFloat viewWidth = 120;
CGFloat viewHeight = 200;
CGFloat offsetX = (kScreenWidth - viewWidth*2 )/3;
for (int i=0; i<2; i++) {
UIView *aView=[[[UIView alloc]initWithFrame:CGRectMake(offsetX+(viewWidth+offsetX)*i ,100, viewWidth, viewHeight)]autorelease];
aView.backgroundColor = i ? [UIColor greenColor]:[UIColor redColor];//三目运算 i==0 ,表示假;i==1,表示 真;
aView.tag = 200 + i;
[self.view addSubview:aView];
}
//4,滑杆
NSArray *colors=@[[UIColor redColor ],[UIColor greenColor],[UIColor blueColor]];
CGFloat offsetY= 20;//纵向
for (int i =0; i<colors.count; i++) {
UISlider *aSlider=[[[UISlider alloc]initWithFrame:CGRectMake(40, 400+(40+offsetY) * i, kScreenWidth-80, 40)]autorelease];
//设置最小值,最大值,和当前值
aSlider.minimumValue=0;
aSlider.maximumValue=1;
aSlider.value=0;
aSlider.minimumTrackTintColor=colors[i];
aSlider.thumbTintColor=colors[i];
aSlider.tag=300+i;
[aSlider addTarget:self action:@selector(handleSliderAction:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:aSlider];
}
//5开关
UISwitch *aSwitch=[[[UISwitch alloc]initWithFrame:CGRectMake(30, kScreenHeight-80, 0, 0)]autorelease];
aSwitch.on=NO;//设置开关当前状态,
[aSwitch addTarget:self action:@selector(handleSwitchAction:) forControlEvents:UIControlEventValueChanged];
aSwitch.tag=400;
[self.view addSubview:aSwitch];
//
UIStepper *aStepper=[[[UIStepper alloc]initWithFrame:CGRectMake(kScreenWidth - 120, kScreenHeight - 80, 0, 0)]autorelease];
aStepper.minimumValue=200;//计步器的最小值,视图原始高度
aStepper.maximumValue=280;//计步器的最大高度,视图的最终高度
aStepper.stepValue=5;//每次增加的值
aStepper.value=200;//计步器的当前值
[aStepper addTarget:self action:@selector(handleStepperAction:) forControlEvents:UIControlEventValueChanged];
aStepper.tag=401;
[self.view addSubview:aStepper];
//调用方法
[self handleSegmentAction:segmentedControl];
}
//分段方法
- (void)handleSegmentAction:(UISegmentedControl*)sender{
UISlider*redSlider=(UISlider*)[self.view viewWithTag:300];
UISlider*greenSlider=(UISlider*)[self.view viewWithTag:301];
UISlider*blueSlider=(UISlider*)[self.view viewWithTag:302];
UISwitch*aSwitch=(UISwitch*)[self.view viewWithTag:400];
UIStepper*aStepper=(UIStepper*)[self.view viewWithTag:401];
UIView*currentView=[self.view viewWithTag:200+sender.selectedSegmentIndex];
[aSwitch setOn:currentView.hidden animated:YES];
// aSwitch.on=currentView.hidden;
aStepper.value=CGRectGetHeight(currentView.frame);
CGFloat red,green,blue;
//获取颜色
[currentView.backgroundColor getRed:&red green:&green blue:&blue alpha:nil];
redSlider.value=red;
greenSlider.value=green;
blueSlider.value=blue;
}
//滑杆方法
- (void)handleSliderAction:(UISlider*)sender{
//得到三个滑杆的当前值,作为颜色三原色的色值
CGFloat red=[(UISlider *)[self.view viewWithTag:300] value];
CGFloat green=[(UISlider*)[self.view viewWithTag:301] value];
CGFloat blue=[(UISlider*)[self.view viewWithTag:302] value];
//通过segmentControl 当前选中的下标获取被改变的视图
UISegmentedControl*segmentControl=(UISegmentedControl*)[self.view viewWithTag:100];
UIView*currentView=[self.view viewWithTag:200+segmentControl.selectedSegmentIndex];
//设定视图的背景颜色
currentView.backgroundColor=[UIColor colorWithRed:red green:green blue:blue alpha:1];
}
//开关方法
- (void)handleSwitchAction:(UISwitch*)sender{
UISegmentedControl*segmentControl=(UISegmentedControl*)[self.view viewWithTag:100];
UIView*currentView=[self.view viewWithTag:200+segmentControl.selectedSegmentIndex];
currentView.hidden=sender.on;
}
//+-方法
- (void)handleStepperAction:(UIStepper*)sender{
UISegmentedControl*segmentControl=(UISegmentedControl*)[self.view viewWithTag:100];
UIView*currentView=[self.view viewWithTag:200+segmentControl.selectedSegmentIndex];
CGRect frame = currentView.frame;
frame.size.height=sender.value;
currentView.frame = frame;
}