.h
@interface ViewController : UIViewController
{
NSTimer *timeCount_Timer;
double progressValue;
IBOutlet UIProgressView *progressBar;
}
@property (nonatomic, retain) IBOutlet UIProgressView *progressBar;
.m
@implementation ViewController
@synthesize countDownLabel;
@synthesize progressBar;
- (void)viewWillAppear:(BOOL)animated {
progressValue=0;
[progressBar setProgress:0.0];
timeCount_Timer =[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(goProgress) userInfo:nil repeats:YES];
}
- (void)goProgress{
// 10 (總時間) / 100 = 0.1 (setProgress每次進度百分比)
if (progressValue > 10)
{
[timeCount_Timer invalidate];
}
else {
progressValue++;
[progressBar setProgress:progressValue*0.1];
}
}
倒數計時
.m
@implementation ViewController
@synthesize countDownLabel;
@synthesize progressBar;
- (void)viewWillAppear:(BOOL)animated {
progressValue= 10.0 ;
progressTotalValue= 10.0;
[progressBar setProgress:1.0]; //設為1 表100% rogressBar傳入0~1之間的小數
timeCount_Timer =[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(goProgress) userInfo:nil repeats:YES];
}
- (void)goProgress{
if (progressValue == 0)
{
[timeCount_Timer invalidate];
}
else {
progressValue--;
float progressValuePercent = progressValue / progressTotalValue ;
[progressBar setProgress:progressValuePercent];
//progressBar.progress = progressValuePercent;
}
}
倒數計時表與ProgressView同步倒數
.h
@@interface ViewController : UIViewController
{
IBOutlet UILabel *countDownLabel;
NSTimer *totalTimeCount_Timer;
int gameCountDown_int;
int gameCountDown_minutes, gameCountDown_seconds;
NSTimer *timeCount_Timer;
float progressValue;
float progressTotalValue;
IBOutlet UIProgressView *progressBar;
}
@property (nonatomic, retain) UILabel *countDownLabel;
@property (nonatomic, retain) IBOutlet UIProgressView *progressBar;
.m
#define GAME_COUNTDOWN 70.0
@implementation ViewController
@synthesize countDownLabel;
@synthesize progressBar;
- (void)viewWillAppear:(BOOL)animated {
//Label total time
gameCountDown_int = GAME_COUNTDOWN;
totalTimeCount_Timer = [NSTimer scheduledTimerWithTimeInterval: 1.0f
target: self
selector: @selector(totalTimer_Count)
userInfo: nil
repeats: YES];
// Progress
progressValue= GAME_COUNTDOWN ;
progressTotalValue= GAME_COUNTDOWN;
[progressBar setProgress:1.0]; //設為1 表100% rogressBar傳入0~1之間的小數
timeCount_Timer =[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(goProgress) userInfo:nil repeats:YES];
}
-(void) totalTimer_Count
{
if (gameCountDown_int > 0)
{
gameCountDown_int-- ;
gameCountDown_minutes = (gameCountDown_int % 3600) / 60;
gameCountDown_seconds = (gameCountDown_int % 3600) % 60;
countDownLabel.text = [NSString stringWithFormat:@"%02d:%02d", gameCountDown_minutes, gameCountDown_seconds];
}
else
{
//Invalidate timer when time reaches 0
[totalTimeCount_Timer invalidate];
}
}
- (void)goProgress{
if (progressValue == 0)
{
[timeCount_Timer invalidate];
}
else {
progressValue--;
float progressValuePercent = progressValue / progressTotalValue ;
[progressBar setProgress:progressValuePercent];
//progressBar.progress = progressValuePercent;
}
}
沒有留言:
張貼留言
注意:只有此網誌的成員可以留言。