UIProgressView进度条,UISlider滑动条,UISegmentedControl分隔栏的简单使用

阅读更多

UISegmentedControl是开发中经常会使用到,所以刚刚在学习的时候也重点的看了下该控件

进度条和滑动条一半很少会使用到(下载的时候会经常使用到该属性),android的进度条和滑动的设置比ios复杂很多

 

先上图


UIProgressView进度条,UISlider滑动条,UISegmentedControl分隔栏的简单使用_第1张图片
 

 

1,进度条

 

    /**进度条的使用
     1.创建UIProGressView对象
     2,创建定时器
     3,将定时器的值与进度条的长度关联setProgress
     4,释放对象
    
    */
    //进度条的使用UIProgressView
    progress=[[UIProgressView alloc]initWithFrame:CGRectMake(50, 250, 300, 150)];
    progress.progressViewStyle=UIProgressViewStyleDefault;
    progress.progressTintColor=[UIColor redColor];//设置进度条的颜色,下面设置了图片会覆盖掉该颜色
    progress.trackTintColor=[UIColor grayColor];//设置背景柱状图的颜色
    progress.progressImage=[UIImage imageNamed:@"login.png"];//图片替代进度条
    progress.transform=CGAffineTransformMakeScale(1.0f, 100.0f);//设置进度条的高度
    [self.view addSubview:progress];
    [progress release];

//    //创建定时器,一秒执行一次
    time=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(show) userInfo:nil repeats:1];
    proValue=1.0;
    

 

 

方法:

 

//点击事件
-(void)show{
//    NameBean *bean=[[NameBean alloc]init];
//    bean.name=@"百合不是茶next";
//    bean.address=@"湖南next";
//    bean.age=122;
//    
//    ViewController *vc =[[ViewController alloc]init];
//    [delegate getOneNameBean:bean];//设置代理数据
//    [self.navigationController pushViewController:vc animated:1];
//    
//    [bean release];
//    [vc release];
    
    ++proValue;
    
    if (proValue>10) {
        //停止定时器
        [time invalidate];
        //计数器停止后,进度条重置
        [progress setProgress:0];
    }else{
    //进度条停止前进
        [progress setProgress:proValue/10];
    
    }

 注意:

 

进度条的方法使用了定时器,事件处理也是在定时器中操作的

 NSTimer *time=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(show) userInfo:nil repeats:1];

 

2,滑动条

 

   /**
     滑动条 UISlider
     1,创建UISlider的对象
     2,指定滑动区间
     3,设置滑动事件
     4,释放
     
     */
    
    slider=[[UISlider alloc]initWithFrame:CGRectMake(50, 300, 300, 150)];
    //指定最小值
    slider.minimumValue=0;
    //指定最大值
    slider.maximumValue=100;
    //指定可变最大值50
    slider.value=50;
    //设置点击实现
    [slider addTarget:self action:@selector(getSliderValue) forControlEvents:UIControlEventValueChanged];
    
    
    [self.view addSubview:slider];
    [slider release];

方法

 

//滑动条的事件方法。
-(void)getSliderValue{

    NSLog(@"滑动条的值是=%f",slider.value);
    
//    [progress setProgress:slider.value];
}

 

 

运行:滑动时:

2015-12-20 16:04:01.755 viewDemo[1125:189580] 滑动条的值是=16.666666

2015-12-20 16:04:01.772 viewDemo[1125:189580] 滑动条的值是=17.222218

2015-12-20 16:04:01.795 viewDemo[1125:189580] 滑动条的值是=17.444443

 

2015-12-20 16:04:01.800 viewDemo[1125:189580] 滑动条的值是=17.444443

 

 

3,分隔栏

   
    /**
     分格拦控件UISegmentControl
     */
    
    segment=[[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:@"买入",@"卖出",nil]];
    segment.frame=CGRectMake(50, 500, 300, 100);
    segment.selectedSegmentIndex=1;//默认选中第一个
//    [segment setImage:[UIImage imageNamed:@"login.png"] forSegmentAtIndex:1];//设置选中的背景图片
//    segment.segmentedControlStyle=UISegmentedControlSegmentCenter;//设置风格
    
    //插入数据
//    [segment insertSegmentWithImage:[UIImage imageNamed:@"login.png"] atIndex:1 animated:0];
    
    //背景图片的设置
    [segment setBackgroundImage:[UIImage imageNamed:@"login.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    [segment setBackgroundImage:[UIImage imageNamed:@"bk.jpg"] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
    //点击事件
    [segment addTarget:self action:@selector(getSegment) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:segment];
    [segment release];
    

 

 点击事件

//分格拦控件事件
-(void)getSegment{
    int n=segment.selectedSegmentIndex;
      [segment setBackgroundImage:[UIImage imageNamed:@"bk.jpg"] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
    
    NSLog(@"%d,标题是=%@",n,segment);
}

 设置选中背景图片

    [segment setBackgroundImage:[UIImage imageNamed:@"bk.jpg"] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];

 

设置未选中背景图片

    [segment setBackgroundImage:[UIImage imageNamed:@"login.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

 

 

完整代码:

//
//  ViewController2.m
//  viewDemo
//
//  Created by wang on 15/12/20.
//  Copyright © 2015年 wang. All rights reserved.
//

#import "ViewController2.h"
#import "ViewController.h"
#import "NameBean.h"

@interface ViewController2 (){

    UIProgressView *progress;
    NSTimer *time;
    double proValue;
    
    UISlider *slider;
    
    UISegmentedControl *segment;
}



@end

@implementation ViewController2

@synthesize nb;
@synthesize delegate;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSLog(@"%@",nb.address);
    
    self.title=@"控件的简单使用";
    //UIButton的使用
    UIButton *btn= [[UIButton alloc]initWithFrame:CGRectMake(300, 100, 100, 100)];
    
    btn.backgroundColor=[UIColor yellowColor];
    //设置文字
    //    [btn setTitle:@"按钮" forState:UIControlStateNormal];
    //
    //    //获取标题的名字
    //    NSString *titleName=[btn titleForState:UIControlStateNormal];
    //    NSLog(@"获取到标题的名字=%@",titleName);
    
    //设置图片
    [btn setBackgroundImage:[UIImage imageNamed:@"login.png"] forState:UIControlStateNormal];
    //获取按钮图片的名字
    UIImage *image=[btn imageForState:UIControlStateNormal];
    NSLog(@"图片的名称=%@",[image imageAsset]);
    
    
    //按钮的点击事件
    [btn addTarget:self action:@selector(show) forControlEvents:UIControlEventTouchUpInside];
    
    //移除点击事件
    //    [btn removeTarget:self action:@selector(show) forControlEvents:UIControlEventTouchUpInside];
    
    //按钮的外观控制
    
    
    //添加到View
    [self.view addSubview:btn];
    //释放
    [btn release];
    
    
    /**进度条的使用
     1.创建UIProGressView对象
     2,创建定时器
     3,将定时器的值与进度条的长度关联setProgress
     4,释放对象
    
    */
    //进度条的使用UIProgressView
    progress=[[UIProgressView alloc]initWithFrame:CGRectMake(50, 250, 300, 150)];
    progress.progressViewStyle=UIProgressViewStyleDefault;
    progress.progressTintColor=[UIColor redColor];//设置进度条的颜色,下面设置了图片会覆盖掉该颜色
    progress.trackTintColor=[UIColor grayColor];//设置背景柱状图的颜色
    progress.progressImage=[UIImage imageNamed:@"login.png"];//图片替代进度条
    progress.transform=CGAffineTransformMakeScale(1.0f, 100.0f);//设置进度条的高度
    [self.view addSubview:progress];
    [progress release];

//    //创建定时器,一秒执行一次
    time=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(show) userInfo:nil repeats:1];
    proValue=1.0;
    
    
    
    /**
     滑动条 UISlider
     1,创建UISlider的对象
     2,指定滑动区间
     3,设置滑动事件
     4,释放
     
     */
    
    slider=[[UISlider alloc]initWithFrame:CGRectMake(50, 300, 300, 150)];
    //指定最小值
    slider.minimumValue=0;
    //指定最大值
    slider.maximumValue=100;
    //指定可变最大值50
    slider.value=50;
    //设置点击实现
    [slider addTarget:self action:@selector(getSliderValue) forControlEvents:UIControlEventValueChanged];
    
    
    [self.view addSubview:slider];
    [slider release];
    
    
    
    /**
     分格拦控件UISegmentControl
     */
    
    segment=[[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:@"买入",@"卖出",nil]];
    segment.frame=CGRectMake(50, 500, 300, 100);
    segment.selectedSegmentIndex=1;//默认选中第一个
//    [segment setImage:[UIImage imageNamed:@"login.png"] forSegmentAtIndex:1];//设置选中的背景图片
//    segment.segmentedControlStyle=UISegmentedControlSegmentCenter;//设置风格
    
    //插入数据
//    [segment insertSegmentWithImage:[UIImage imageNamed:@"login.png"] atIndex:1 animated:0];
    
    //背景图片的设置
    [segment setBackgroundImage:[UIImage imageNamed:@"login.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    [segment setBackgroundImage:[UIImage imageNamed:@"bk.jpg"] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
    //点击事件
    [segment addTarget:self action:@selector(getSegment) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:segment];
    [segment release];
    
    
}


-(void)viewDidAppear:(BOOL)animated{

}

//分格拦控件事件
-(void)getSegment{
    int n=segment.selectedSegmentIndex;
      [segment setBackgroundImage:[UIImage imageNamed:@"bk.jpg"] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
    
    NSLog(@"%d,标题是=%@",n,segment);
}

//滑动条的事件方法。
-(void)getSliderValue{

    NSLog(@"滑动条的值是=%f",slider.value);
    
//    [progress setProgress:slider.value];
}


//点击事件
-(void)show{
//    NameBean *bean=[[NameBean alloc]init];
//    bean.name=@"百合不是茶next";
//    bean.address=@"湖南next";
//    bean.age=122;
//    
//    ViewController *vc =[[ViewController alloc]init];
//    [delegate getOneNameBean:bean];//设置代理数据
//    [self.navigationController pushViewController:vc animated:1];
//    
//    [bean release];
//    [vc release];
    
    ++proValue;
    
    if (proValue>10) {
        //停止定时器
        [time invalidate];
        //计数器停止后,进度条重置
        [progress setProgress:0];
    }else{
    //进度条停止前进
        [progress setProgress:proValue/10];
    
    }
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

 

 

  • UIProgressView进度条,UISlider滑动条,UISegmentedControl分隔栏的简单使用_第2张图片
  • 大小: 153.3 KB
  • 查看图片附件

你可能感兴趣的:(UIProgressView,UISlider)