#pragma mark 1开关
- (void)createSwitch{
UISwitch *sw = [[UISwitch alloc]initWithFrame:CGRectMake(0, 70, 200, 40)];
sw.tintColor = [UIColor redColor];
sw.thumbTintColor = [UIColor orangeColor];
[sw setOn:YES animated:YES];
[sw addTarget: self action:@selector(swValueChanged:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:sw];
}
- (void)swValueChanged:(UISwitch *)sw{
NSLog(@"%d",sw.isOn);
}
#pragma mark 2分段
- (void)createSegment{
// UISegmentedControl *seg = [[UISegmentedControl alloc]initWithFrame:CGRectMake(0, 120, 200, 40)];
UISegmentedControl *seg = [[UISegmentedControl alloc]initWithItems:@[@"左",@"中",@"右"]];
seg.frame = CGRectMake(0, 120, 200, 40);
seg.selectedSegmentIndex = 1;
[seg setTintColor:[UIColor redColor]];
//字体大小
[seg setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor greenColor],NSFontAttributeName :[UIFont systemFontOfSize:18]} forState:UIControlStateNormal];
[seg addTarget:self action:@selector(segChanged:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:seg];
}
- (void)segChanged:(UISegmentedControl *)seg{
NSInteger index = seg.selectedSegmentIndex;
if (index == 0) {
NSLog(@"左");
}
}
#pragma mark 3步进器
- (void)createStepper{
UIStepper *st = [[UIStepper alloc]initWithFrame:CGRectMake(0, 180, 100, 40)];
st.minimumValue = 1.0;
st.maximumValue = 10.0;
st.stepValue = 1; //步进值
st.wraps = YES; //循环
[st addTarget:self action:@selector(stChange:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:st];
}
- (void)stChange:(UIStepper *)st{
NSInteger *value = (NSInteger)st.value;
NSLog(@"%ld",value);
}
#pragma mark 4滑块
- (void)createSlider{
UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake(0, 250, 200, 40)];
slider.minimumValue = 0.0;
slider.maximumValue = 100.0;
slider.minimumTrackTintColor = [UIColor redColor];
slider.maximumTrackTintColor = [UIColor greenColor];
slider.thumbTintColor = [UIColor blackColor];
// [slider setThumbImage:[[UIImage alloc]init] forState:UIControlStateNormal];
[slider addTarget:self action:@selector(sliderChanged:) forControlEvents:UIControlEventValueChanged ];
[self.view addSubview:slider];
}
- (void)sliderChanged:(UISlider *)slider{
NSLog(@"%lf",slider.value);
}
#pragma mark 5菊花加载
- (void)createActivity{
UIActivityIndicatorView *act = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0, 300, 100, 100)];
act.color = [UIColor redColor];
act.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
act.hidesWhenStopped = YES;
[act startAnimating];
//[act stopAnimating];
[self.view addSubview:act];
}
#pragma mark 6进度条 (配合定时器NSTimer使用)
- (void)createProgress{
UIProgressView *pV = [[UIProgressView alloc]initWithFrame:CGRectMake(0, 400, 200, 40)];
pV.progress = 0.5; //0-1
pV.progressTintColor = [UIColor redColor];//已走过的轨道颜色
pV.trackTintColor = [UIColor greenColor];
[self.view addSubview:pV];
}