- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UISegmentedControl * segmentedControl = [[[UISegmentedControl alloc] initWithItems:@[@"左视图",@"右视图"]]autorelease];
segmentedControl.frame = CGRectMake(20, 50, kScreenWidth-40, 30);
//设置分段的默认选中下标
segmentedControl.selectedSegmentIndex = 0;
segmentedControl.tag = 100;
[segmentedControl addTarget:self action:@selector(handleSegmentedControlAction:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:segmentedControl];
//视图的宽度和间距
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, 120, viewWidth, viewHeight)] autorelease];
aView.backgroundColor = i ? [UIColor greenColor] : [UIColor redColor];
aView.tag = 200 + i;//加tag
[self.view addSubview:aView];
}
NSArray *colors = @[[UIColor redColor],[UIColor greenColor],[UIColor blueColor]];
CGFloat offsetY = 20;//纵向偏移量
for (int i = 0; i < 3; i++) {
UISlider *aSlider = [[[UISlider alloc] initWithFrame:CGRectMake(20, 400+(40+offsetY)*i, kScreenWidth-40, 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];
}
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 - 130, kScreenHeight - 80, 0, 0)] autorelease];
aStepper.minimumValue=200;//计步器的最小值为视图的原始高度
aStepper.maximumValue = 280;//最终高度
aStepper.stepValue = 5;//每次增加的值
aStepper.value = 200;//计步器的当前值
aStepper.tag = 500;
[aStepper addTarget:self action:@selector(handleStepperAction:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:aStepper];
[self handleSegmentedControlAction:segmentedControl];
}
-(void)handleSegmentedControlAction:(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:500];
UIView *currentView = [self.view viewWithTag:200+sender.selectedSegmentIndex];
// aSwitch.on = currentView.hidden;
[aSwitch setOn:currentView.hidden animated:YES];
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];
//通过segmentedControl当前选中的下标来获取被改变的视图
UISegmentedControl *segmentedControl = (UISegmentedControl *)[self.view viewWithTag:100];
UIView *currentView = [self.view viewWithTag:200+segmentedControl.selectedSegmentIndex];
//设定设图的背景颜色
currentView.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1];
}
-(void)handleSwitchAction:(UISwitch *)sender{
UISegmentedControl *segmentedControl = (UISegmentedControl *)[self.view viewWithTag:100];
UIView *currentView = (UIView *)[self.view viewWithTag:200+segmentedControl.selectedSegmentIndex];
currentView.hidden = sender.on;
}
-(void)handleStepperAction:(UIStepper *)sender{
UISegmentedControl *segmentedControl = (UISegmentedControl *)[self.view viewWithTag:100];
UIView *currentView = (UIView *)[self.view viewWithTag:200+segmentedControl.selectedSegmentIndex];
CGRect frame = currentView.frame;
frame.size.height = sender.value;
currentView.frame = frame;
}