声明属性:
#import <UIKit/UIKit.h> @interface ViewController : UIViewController { //滑动条(音量) UISlider *_slider; //进度条对象(一般表示进度) UIProgressView *_progressView; } //进度条 @property (nonatomic, strong) UIProgressView * pView; //滑动条 @property (nonatomic, strong) UISlider *slid; @end
创建对象:
#import "ViewController.h" @interface ViewController () @end @implementation ViewController //同步成员变量和属性 @synthesize slid = _slider; @synthesize pView = _pView; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //设置背景颜色 self.view.backgroundColor = [UIColor yellowColor]; //进度条的创建(不能主动的添加事件) _progressView = [[UIProgressView alloc]init]; //设置进度条的大小和位置(高度是不可变化的) _progressView.frame = CGRectMake(10, 50, 300, 10); //设置进度条的风格颜色值 _progressView.progressTintColor = [UIColor redColor]; //设置进度条的进度值(0--1) _progressView.progress = 0.5; //设置进度条的风格 _progressView.progressViewStyle = UIProgressViewStyleDefault; _progressView.trackTintColor = [UIColor blackColor]; //添加到当前视图上面 [self.view addSubview:_progressView]; _slider = [[UISlider alloc]init]; _slider.frame = CGRectMake(10, 100, 300, 40); //设置最大值 _slider.maximumValue = 100; //设置最小值(可以为负值) _slider.minimumValue = -100; //设置滑动条滑块的位置 _slider.value = 0.5; //滑块左侧颜色 _slider.minimumTrackTintColor = [UIColor lightGrayColor]; //滑块右侧颜色 _slider.maximumTrackTintColor = [UIColor redColor]; //设置滑块的颜色 _slider.thumbTintColor = [UIColor purpleColor]; //添加滑块滑动事件 [_slider addTarget:self action:@selector(SliderAct:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:_slider]; } - (void)SliderAct:(UISlider *)slider{ NSLog(@"value = %.2f", slider.value); //滑动滑块改变进度条的位置 // _progressView.progress = slider.value; //如果值超出范围,需要写个个算法 _progressView.progress = (slider.value - slider.minimumValue) / (_slider.maximumValue - slider.minimumValue); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end