#import <UIKit/UIKit.h> @interface VCRoot : UIViewController //进度条 @property (retain,nonatomic) UIProgressView* progressView ; //滑动条 @property (retain,nonatomic) UISlider* sliderView ; @property (retain,nonatomic) NSTimer* timer ; @property (assign,nonatomic) float value ; @end #import "VCRoot.h" #import "AppDelegate.h" #import "VCSecond.h" @interface VCRoot () @end @implementation VCRoot - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } -(void) createStepper { //建立一个步进器对象 UIStepper* stepper = [[UIStepper alloc] init] ; //设置步进器的位置,宽高不能改变 stepper.frame = CGRectMake(100, 200, 200, 40) ; //步进器的当前的计数值 stepper.value = 0 ; //每次增长或减小的值,步长值 stepper.stepValue = 10 ; //设置步进器的最小范围值 //默认为:0 //值能够为负值 stepper.minimumValue = 0 ; //设置步进器最大值的范围 //默认值为100, //要保证maximumValue>minimumValue stepper.maximumValue = 1000 ; //按住步进器按钮时是否自动进行增长减小计算 stepper.autorepeat = YES ; //是否连续调用事件函数 // stepper.continuous = YES ; //添加一个事件函数 //每当步进器的值发生变化时,调用事件函数 [stepper addTarget:self action:@selector(stepChange:) forControlEvents:UIControlEventValueChanged] ; //添加控件到视图上 [self.view addSubview:stepper] ; self.value = 0; } //步进器事件函数实现 -(void) stepChange:(UIStepper*) step { NSLog(@"value = %f",step.value) ; UIApplication* app = [UIApplication sharedApplication] ; AppDelegate* appD = app.delegate ; //得到步进器的比例值 appD.changeValue = (step.value -step.minimumValue)/ (step.maximumValue - step.minimumValue); if (step.value == self.value){ return; }else if (self.value >step.value){ _progressView.progress -= 0.01; self.value = step.value; } else{ _progressView.progress += 0.01 ; if (_progressView.progress >= 1.0f) { return ; } self.value = step.value; } } -(void) createSegment { NSArray *array1 = @[@"5元",@"10元",@"15元",@"20元"]; // 建立分组按钮控件 UISegmentedControl* seg = [[UISegmentedControl alloc] initWithItems:array1] ; //设置分组按钮的位置]; //位置和宽度能够设置,高度可变动 seg.frame = CGRectMake(20, 300, 280, 40) ; seg.tintColor = [UIColor orangeColor]; //向控件中添加一个按钮 //参数一:按钮中标题 //参数二:按钮所在位置的索引,0位置在最左侧 //参数三:是否产生一个插入的动画 // [seg insertSegmentWithTitle:@"15元" atIndex:0 animated:NO] ; // [seg insertSegmentWithTitle:@"10元" atIndex:1 animated:NO]; // [seg insertSegmentWithTitle:@"5元" atIndex:2 animated:NO] ; // [seg insertSegmentWithTitle:@"30元" atIndex:3 animated:NO] ; //添加控件的事件函数 [seg addTarget:self action:@selector(segChange:) forControlEvents:UIControlEventValueChanged] ; //初始选中第一个按钮 //设置选中按钮的索引 seg.selectedSegmentIndex = -1 ; // [seg imageForSegmentAtIndex:1]; // [seg setImage:[UIImage imageNamed:@"peas.png"] forSegmentAtIndex:1]; // [seg insertSegmentWithImage:[UIImage imageNamed:@"peas.png"] atIndex:1 animated:YES]; // [seg setBackgroundImage:[UIImage imageNamed:@"peas.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; NSString* strTitle = [seg titleForSegmentAtIndex:0] ; NSLog(@"title = %@",strTitle) ; //添加图像按钮 //[seg insertSegmentWithImage:[UIImage imageNamed:@"17_1.jpg"] atIndex:0 animated:NO] ; [self.view addSubview:seg] ; } //当更改选中按钮时,调用此函数 -(void) segChange:(UISegmentedControl*) seg { NSLog(@"index = %d",seg.selectedSegmentIndex) ; NSString* strTitle = [seg titleForSegmentAtIndex:seg.selectedSegmentIndex] ; NSLog(@"title = %@",strTitle) ; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self createStepper] ; [self createSegment] ; [self createSecond] ; } -(void)createSecond{ _progressView = [[UIProgressView alloc] init] ; _progressView.frame = CGRectMake(10, 100, 300, 40) ; _progressView.progress = 0 ; [self.view addSubview:_progressView] ; // _timer = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(updateProgress:) userInfo:nil repeats:YES] ; UIApplication* app = [UIApplication sharedApplication] ; AppDelegate* appD = app.delegate ; _value = appD.changeValue ; } -(void) updateProgress:(id) obj { _progressView.progress += 0.01 ; if (_progressView.progress >= 1.0f) { [_timer invalidate] ; } } //-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event //{ // VCSecond* vc = [[VCSecond alloc] init] ; // // [self presentViewController:vc animated:YES completion:nil] ; //} - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }