步進器和分組按鈕

#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.
}

 

相關文章
相關標籤/搜索