6控件(开关/滑块/进度条......)
Posted Da雪山
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了6控件(开关/滑块/进度条......)相关的知识,希望对你有一定的参考价值。
#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];
}
以上是关于6控件(开关/滑块/进度条......)的主要内容,如果未能解决你的问题,请参考以下文章
VS2008中Slider控件制作播放器的进度条,如何在视频播放时改变滑块位置
串口屏开发之滑块控件的使用总结——如何通过滑动滑块实现进度条和文本的联动效果