UISlider,UISwitch,UIStepper,UISegmentControl之间的联动及功能
2015-10-22 20:56 lzrrey 阅读(269) 评论(0) 编辑 收藏 举报#import "MainViewController.h"
#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.
//分段控件
UISegmentedControl *segmentControl = [[UISegmentedControl alloc] initWithItems:@[@"左视图",@"右视图"]];
segmentControl.frame = CGRectMake(20, 50, kScreenWidth-40, 30);
//设置分段的默认选中下标
segmentControl.selectedSegmentIndex = 0;
[segmentControl addTarget:self action:@selector(handleSegmentAction:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:segmentControl];
[segmentControl release];
[self handleSegmentAction:segmentControl];
//视图的宽度和间距
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)];
aView.backgroundColor=i ? [UIColor greenColor]:[UIColor redColor];
//加tag
aView.tag = 200 + i;
[self.view addSubview:aView];
[aView release];
}
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(20, 400+(40+offsetY)*i, kScreenWidth-40, 40)];
//设置最小值,最大值和当前值
aSlider.minimumValue = 0;
aSlider.maximumValue = 1;
aSlider.value = 0;
aSlider.minimumTrackTintColor = colors[i];
aSlider.thumbTintColor = colors[i];
//设置tag
aSlider.tag = 300 + i;
[aSlider addTarget:self action:@selector(handleSliderAction:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:aSlider];
[aSlider release];
}
UISwitch *aSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(30, kScreenHeight-80, 0, 0)];
aSwitch.on = NO; //设置开关当前状态,类似于分段的选中下标
[aSwitch addTarget:self action:@selector(handleSwitchAction:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:aSwitch];
[aSwitch release];
UIStepper *aStepper = [[UIStepper alloc] initWithFrame:CGRectMake(kScreenWidth-120, kScreenHeight-80, 0, 0)];
aStepper.minimumValue = 200; //计步器的最小值,为视图的原始高度
aStepper.maximumValue = 280; //计步器的最大值,为视图的最终高度
aStepper.stepValue = 5; //每次增加的值
aStepper.value = 200; //计步器的当前值
[aStepper addTarget:self action:@selector(handleStepperAction:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:aStepper];
[aStepper release];
//设置tag
segmentControl.tag = 100;
aSwitch.tag = 400;
aStepper.tag = 401;
[self handleSegmentAction:segmentControl];
}
-(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.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];
//通过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;
}