码迷,mamicode.com
首页 > 其他好文 > 详细

NSTimer,UIActivityIndicatorView,UIProgressView等控件额使用方法

时间:2014-12-03 19:21:06      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   使用   sp   

说明

    本例子主要简示了和时间相关的一些控件的用法,模拟了一个下载器。

运行结果

bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣

主要代码

@synthesize _labelInfo;
@synthesize _textInfo;
@synthesize _buttonDownload;
@synthesize _processViewDownload;
@synthesize _activityIndicatorDownload;
@synthesize _sliderDownload;
@synthesize _switchDownload;
@synthesize _timerDownload;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    _imageBackground = [UIImage imageNamed:@"leaf.jpg"];
    self.view.backgroundColor = [UIColor colorWithPatternImage:_imageBackground];
    
    // 位置大小预设值
    CGFloat x = self.view.frame.size.width / 8;
    CGFloat y = self.view.frame.size.height / 10;
    CGFloat w = x * 6;
    CGFloat h = 40;
    CGFloat hEdge = 10;
    CGFloat cornerRadius = 10;
    
    // 标签
    _labelInfo = [[UILabel alloc] initWithFrame:CGRectMake(x, y, w, h)];
    _labelInfo.text = @"绿豆猪下载";
    _labelInfo.textColor = [UIColor greenColor];
    _labelInfo.shadowColor = [UIColor redColor];
    _labelInfo.shadowOffset = CGSizeMake(2, 3);
    _labelInfo.textAlignment = NSTextAlignmentCenter;
    NSArray* arrayFont = [UIFont familyNames];
    _labelInfo.font = [UIFont fontWithName:arrayFont[arc4random()%arrayFont.count] size:h-2];
    [self.view addSubview:_labelInfo];
    
    // 文本信息栏
    y = _labelInfo.frame.origin.y + _labelInfo.frame.size.height;
    y += 2*hEdge;
    _textInfo = [[UITextField alloc] initWithFrame:CGRectMake(x, y, w, h)];
    _textInfo.placeholder = @"Here is the app infomation list";
    _textInfo.layer.cornerRadius = cornerRadius;
    _textInfo.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_textInfo];
    
    // 猪猪图标按钮
    UIImage* imageStart = [UIImage imageNamed:@"pig.png"];
    y =  _textInfo.frame.origin.y + _labelInfo.frame.size.height;
    y += hEdge;
    _buttonDownload = [[UIButton alloc] initWithFrame:CGRectMake(x, y, w/4, h*2)];
    [_buttonDownload setImage:imageStart forState:UIControlStateNormal];
    _buttonDownload.backgroundColor = [UIColor clearColor];
    [_buttonDownload addTarget:self action:@selector(onClickBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_buttonDownload];
    
    // 下载指示器
    _activityIndicatorDownload = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(x+w/2, y, w/4, h)];
    //[_activityIndicatorDownload startAnimating];
    [self.view addSubview:_activityIndicatorDownload];
    
    // 下载开关
    _switchDownload = [[UISwitch alloc] initWithFrame:CGRectMake(x+3*w/4, y, w/4, h)];
    [_switchDownload addTarget:self action:@selector(onSwitchChange:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_switchDownload];
    
    // 下载进度条
    _processViewDownload = [[UIProgressView alloc] initWithFrame:CGRectMake(x + w/4, y + h, 3*w/4, h)];
    _processViewDownload.progress = 0;
    [self.view addSubview:_processViewDownload];
    
    // 下载文件指示器
    y = _buttonDownload.frame.origin.y + _buttonDownload.frame.size.height;
    y += hEdge;
    _sliderDownload = [[UISlider alloc] initWithFrame:CGRectMake(x, y, w, h)];
    [self.view addSubview:_sliderDownload];
}

-(IBAction)onClickBtn:(id)sender
{
    _textInfo.text = @"This is green pig app.";
}

-(IBAction)onSwitchChange:(id)sender
{
    // 启动时显示进度控件,停止时隐藏控件
    BOOL bHide = !_switchDownload.isOn;
    [_sliderDownload setHidden:bHide];
    [_activityIndicatorDownload setHidden:bHide];
    [_processViewDownload setHidden:bHide];
    if(_switchDownload.isOn)
    {
        _sliderDownload.value = 0;
        _sliderDownload.minimumValue = 0;
        _sliderDownload.maximumValue = arc4random()%1000 + 10;
        _textInfo.text = @"strat download .....";
        _timerDownload = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(onDownload) userInfo:nil repeats:YES];
        [_activityIndicatorDownload startAnimating];
        _processViewDownload.progress = 0;
    }
    else
    {
        [_timerDownload invalidate];
        [_activityIndicatorDownload stopAnimating];
        _textInfo.text = @"Download finished.";
    }
}

-(IBAction)onDownload
{
    _processViewDownload.progress += arc4random()%100 / 1000.0f;
    _sliderDownload.value = _processViewDownload.progress * _sliderDownload.maximumValue;
    _textInfo.text = [NSString stringWithFormat:@"%.2f%%  %.2fM  %.2fM", _processViewDownload.progress*100, _sliderDownload.value, _sliderDownload.maximumValue];
    if(_processViewDownload.progress >= 1)
    {
        [_switchDownload setOn:NO animated:YES];
        [self onSwitchChange:_switchDownload];
    }
}


代码链接

NSTimer,UIActivityIndicatorView,UIProgressView等控件额使用方法

标签:style   blog   http   io   ar   color   os   使用   sp   

原文地址:http://blog.csdn.net/arbboter/article/details/41700073

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!