对于这个定时器与进度条,我不是一般的郁闷,先无语凝噎泪两行了!! 作为初学者,起初我就照着课本敲代码,但很不幸教材的例子不完整,代码运行总报错,网上百度那些错误,也不知道说个啥意思。总之,反复纠结好多天,今天下 午终于结合网上实例搞定了,心情一下如阳光般灿烂,撒花撒花。。。接下来,我将说说定时器NSTimer的使用,也总结下今天两种创建进度条的方法。
@interface ViewController ()
{
NSTimer *myTimer;
}
@property (strong,nonatomic)NSTimer *myTimer;
@property (strong,nonatomic) IBOutletUIProgressView *myProgressView;
- (IBAction)startLoad:(id)sender;
@end
一、NSTimer的使用:
定时器NSTimer继承了NSObject类,能在特定的时间间隔后向某个对象发出消息;
1. 一般常用的创建定时器的方法有:
myTimer=[NSTimer scheduledTimerWithTimeInterval:<#(NSTimeInterval)#>seconds target:<#(id)#> selector:<#(SEL)#> userInfo:<#(id)#> repeats:<#(BOOL)#>];
其中,seconds参数用于设定时间间隔,target参数用于指定发送消息给哪个对象(经常是发送给自身,使用self),selector:后加上调用的方法名称,相当于一个函数指针,userInfo指给消息传递参数(若无参数传递,则使用nil),repeats表示是否重复,若重复,使用YES;
(个人理解:这个定时器有点类似于C语言中的for循环结构或while循环结构,根据指定的时间间隔,使得selector后所接的方法循环调用一次)
2.停止定时器:
[myTimer invalidate];
二、两种创建进度条的方法:
2.1,从对象库中向故事板拖入一个进度条,步骤如下:
(1),向故事板中,拖入一个进度条ProgressView和一个Button控件,将ProgressView设置为输出口myProgressView,将Button设置为动作,方法名为:startLoad,同时定义一个定时器myTimer,.h文件中代码如下:
#import "ViewController.h"
@interface ViewController ()
{
NSTimer *myTimer;
}
@property (strong,nonatomic)NSTimer *myTimer;
@property (strong,nonatomic)IBOutletUIProgressView *myProgressView;
- (IBAction)startLoad:(id)sender;
(2),完善startLoad方法:
- (IBAction)startLoad:(id)sender {
self.myProgressView.progress=0;
myTimer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(download) userInfo:nil repeats:YES];
}
<pre name="code" class="objc"><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">备注:这一块主要是启动定时器,同时向定时器中传入一个dowload方法(这个download方法用于设置进度条的属性),因此我们需要另外写一个download方法。</span>
(3),写download方法,设置myProgressView属性:
-(void)download{
self.myProgressView.progress=self.myProgressView.progress+0.05;
if(self.myProgressView.progress==1.0){
[myTimer invalidate];
UIAlertView *alert=[[UIAlertViewalloc]initWithTitle:@"downLoad completed"message:@""delegate:nilcancelButtonTitle:@"OK"otherButtonTitles:nil];
[alert show];
}else{
[self.myProgressViewsetProgress:self.myProgressView.progress];
}
}
有了这些就大功告成,可以点击运行啦,下方图一是刚运行时的结果,点击Download按钮后,出现图二的结果,进度条在一点点增加,直到满格(图三结果),最后会弹出download completed ok 等字样
图一 图二
图三 图四
2.2,直接在代码中创建一个进度条,步骤如下:
(1),向故事版中拖入一个button按钮.
.h文件如下:
@interface BIDViewController ()
{
UIProgressView *myProgressView;
NSTimer *myTime;
//double proValue;
}
@property(strong,nonatomic)NSTimer *myTimer;
@property (retain, nonatomic)UIProgressView *myProgressView;
- (IBAction)downLoadProgressView:(UIButton *)sender;
@end
(2),完善download方法,编写download方法,这部分其实跟上面那个方法相同;
-(void)downLoad{
myProgressView.progress=myProgressView.progress+0.05;
if(myProgressView.progress>1.0){
[myTimeinvalidate];
UIAlertView *alert=[[UIAlertViewalloc]initWithTitle:@"downLoad completed"message:@""delegate:nilcancelButtonTitle:@"OK"otherButtonTitles:nil];
[alertshow];
}else{
[myProgressViewsetProgress:myProgressView.progress];
}
}
- (IBAction)downLoadProgressView:(UIButton *)sender {
myTime=[NSTimerscheduledTimerWithTimeInterval:1.0 target:selfselector:@selector(downLoad)userInfo:nilrepeats:YES];
}
(3),由于这个进度条是我们自己敲代码创建的,因此我们需要重写viewDidLoad方法,如下:
-(void)viewDidLoad{
myProgressView=[[UIProgressViewalloc]initWithFrame:CGRectMake(100,100, 150, 20)];
[myProgressViewsetProgressViewStyle:UIProgressViewStyleBar];
[self.viewaddSubview:myProgressView];
[superviewDidLoad];
}
三,ActivityIndicatorView的使用
在第一个进度条实例的基础上,向故事板中添加一个ActivityIndicatorView活动指示器,并将其设置为输出口:myActivityIndicatorView,然后将startload和download方法修改如下:
- (IBAction)startLoad:(id)sender {
if([self.myActivityIndicatorViewisAnimating]){
[self.myActivityIndicatorViewstopAnimating]; //终止活动指示器
}else {
[self.myActivityIndicatorViewstartAnimating]; //启动活动指示器
}
self.myProgressView.progress=0;
myTimer=[NSTimerscheduledTimerWithTimeInterval:1.0target:selfselector:@selector(download)userInfo:nilrepeats:YES];
}
-(void)download{
self.myProgressView.progress=self.myProgressView.progress+0.05;
if(self.myProgressView.progress==1.0){
[self.myActivityIndicatorViewstopAnimating]; //终止活动指示器
[myTimerinvalidate];
UIAlertView *alert=[[UIAlertViewalloc]initWithTitle:@"downLoad completed"message:@""delegate:nilcancelButtonTitle:@"OK"otherButtonTitles:nil];
[alertshow];
}/*else{
[self.myProgressView setProgress:self.myProgressView.progress];
}*/
}
代码运行后,若点击download按钮,进度条会前进,活动指示器将会不停旋转,一旦进度条满格,活动指示器将会停止,而且弹出“download complete ok”的对话框;