---
title: "progress的简单使用"
author: "AndLi"
date: "2012-07-22T17:01:26Z"
tags: []
categories: ["移动开发"]
slug: "progress"
draft: false
lastmod: "2012-07-22T17:01:26Z"
id: 1603688
url: "https://andli.iteye.com/blog/1603688"
---

![](http://dl.iteye.com/upload/attachment/0071/1493/4ba1d775-7a72-342c-a5d9-c413d9e4e826.png)  
 .h文件

```
#import <UIKit/UIKit.h>

@interface ProgressViewController : UIViewController
{
    UIProgressView * progressview;
    UIProgressView * barprogressview;
    
    UIButton * startBtn;
    UIButton * stopBtn;
    
    NSTimer *timer;//定时器
}

@property(retain, nonatomic) IBOutlet UIProgressView * progressview;
@property(retain, nonatomic) IBOutlet UIProgressView * barprogressview;
@property(retain, nonatomic) IBOutlet UIButton * startBtn;
@property(retain, nonatomic) IBOutlet UIButton * stopBtn;

-(IBAction)startProgress:(id)sender;
-(IBAction)stopProgress:(id)sender;

@end
```

 .m文件

```
#import "ProgressViewController.h"



@interface ProgressViewController ()

@end

@implementation ProgressViewController

@synthesize progressview;
@synthesize barprogressview;
@synthesize startBtn;
@synthesize stopBtn;

-(void)dealloc{
    
    self.progressview=nil;
    self.barprogressview=nil;
    
    [super dealloc];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

-(void) timeChanged:(id)sender{
    self.progressview.progress +=0.01f;
    self.barprogressview.progress +=0.01f;
}
//启动进度条
-(IBAction)startProgress:(id)sender
{
    timer=[NSTimer scheduledTimerWithTimeInterval:0.03f //时间间隔
                                           target:self 
                                           selector:@selector(timeChanged:)//调用进度改变的方法
                                           userInfo:nil 
                                           repeats:YES];
//    [timer release];
}
//停止进度条
-(IBAction)stopProgress:(id)sender
{
    [timer invalidate];//使Timer失效
     timer=nil;//注意这一句的位置,不要放到release后，否则会报错的
    [timer release];

    self.progressview.progress=0.0f;
    self.barprogressview.progress=0.0f;
}

@end
```