我希望将时间从10减少到0.并且通过此代码执行此操作..但它不能正常工作
-(void)elapsedTime
{
static int i = 11;
UILabel *label;
label = [[UILabel alloc] initWithFrame:CGRectMake(850, 27, 50, 50)];
label.textColor = [UIColor redColor];
label.font = [UIFont boldSystemFontOfSize:25.0f];
label.backgroundColor=[UIColor blackColor];
label.text = [NSString stringWithFormat:@"%i",i];
[self.view addSubview:label];
i--;
if(i<0)
{
[aTimer invalidate];
aTimer = nil;
}
[label sendSubviewToBack:self.view];
}
答案 0 :(得分:1)
您应该使用NSTimer
这样的目的。
在.h文件中
{
NSTimer *timer
}
在.m文件中放这个
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateLabel) userInfo:nil repeats:YES];
现在添加一个函数,它将每1秒调用一次。您可以在此功能中更新您的标签。
-(void)updateLabel {
if (timeCount==0) {
[timer invalidate];
} else {
timeCount = timeCount-1;
label.text = [NSString stringWithFormat:@"%d",timeCount];
}
}
希望这有帮助。
答案 1 :(得分:0)
您可以使用NSTimer执行此操作。
只需按照链接,即可获得有关计时器的详细信息。
https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Timers/Timers.html
http://pravinmagdum.wordpress.com/2010/12/30/how-to-use-nstimer/
http://samplecodebank.blogspot.in/2011/05/nstimer-example.html
答案 2 :(得分:0)
为您的应用使用以下代码。并从viewDidload调用addlabel并在.h文件中定义label和i。
- (无效)addlabel {
i=10;
label = [[UILabel alloc] initWithFrame:CGRectMake(150, 27, 50, 50)];
label.textColor = [UIColor redColor];
label.font = [UIFont boldSystemFontOfSize:25.0f];
label.backgroundColor=[UIColor blackColor];
label.text = [NSString stringWithFormat:@"%i",i];
[self.view addSubview:label];
aTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(elapsedTime) userInfo:nil repeats:YES];
}
- (无效)elapsedTime {
i--;
label.text = [NSString stringWithFormat:@"%i",i];
if(i<1)
{
[aTimer invalidate];
aTimer = nil;
}
}
答案 3 :(得分:0)
试试这个
·H
NSTimer* time;
int count;
.m文件
viedidload
count=10;
time=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeShedule) userInfo:nil repeats:YES];
-(void)timeShedule {
label.text=[NSString stringWithFormat:@"%d",count];
count--;
if (count==0) {
[time invalidate];
time=nil;
}
}
答案 4 :(得分:0)
in .h
UILabel *label;
in .m
- (void)viewDidLoad
{
label = [[UILabel alloc] initWithFrame:CGRectMake(850, 27, 50, 50)];
label.textColor = [UIColor redColor];
label.font = [UIFont boldSystemFontOfSize:25.0f];
label.backgroundColor=[UIColor blackColor];
label.text = @"10";
[self.view addSubview:label];
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(elapsedTime:) userInfo:nil repeats:YES];
}
- (void)elapsedTime:(NSTimer *)timer
{
int currentValue = [label.text intValue];
currentValue--;
label.text = [NSString stringWithFormat:@"%i",currentValue];
if (currentValue == 0)
{
[self.view sendSubviewToBack:label];
[timer invalidate];
timer = nil;
}
}
答案 5 :(得分:0)
如果您创建了一些计数器应用程序,例如http://download.cnet.com/Work-Time-Counter/3000-2124_4-75903089.html
您需要使用NSDate,如下所示:
-(void) start {
// Save start time
self.started = [[NSDate alloc] init];
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(updateCallback)
userInfo:nil
repeats:YES];
}
-(void)updateCallback {
// Count time since start and notify UI
[self.callback counterTick:[ self.started timeIntervalSinceNow ]];
}