我是ios的新手,因此我需要帮助制作3:30分钟的计时器,该计时器具有添加和跳过两个选项,计时器到达00:00后将消失。请帮助我,我现在处于非常困难的境地。
这是计时器
-(void)updatelable:(NSTimer *)timer {
remainingTime=180;
for (int i=remainingTime;i==0 ;i--) {
NSInteger minutes = floor(*(remainingTime))/ 60;
remainingTime = remainingTime-(minutes*60);
NSInteger seconds = remainingTime;
second.text =[NSString stringWithFormat:@"%02ld",seconds];
minute.text =[NSString stringWithFormat:@"%02ld",minutes];
timerLabel.text = @"Time up!!";
}
[timer invalidate];
}
试试这个
.h文件
{
int counter;
NSTimer timer;
}
在.m文件中
- (void)viewDidLoad
{
counter = 210;//total Time
timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(targetMethod:) userInfo:nil repeats:YES];
}
-(void)targetMethod:(NSTimer*)timr
{
if(counter == 0)
{
[timer invalidate]
//set label hidden here after 3:30 minutes
}
//strTime is string containing remaining time
NSString *strTime = [self formatTimeFromSeconds:counter];
counter--;
}
对于标签文本,请调用此方法
-(NSString *)formatTimeFromSeconds:(int)numberOfSeconds
{
int seconds = numberOfSeconds % 60;
int minutes = (numberOfSeconds / 60) % 60;
int hours = numberOfSeconds / 3600;
//we have >=1 hour => example : 3h:25m
if (hours)
return [NSString stringWithFormat:@"%dh:%dm:%ds", hours, minutes, seconds];
//we have 0 hours and >=1 minutes => example : 3m:25s
if (minutes)
return [NSString stringWithFormat:@"%dm:%ds", minutes, seconds];
//we have only seconds example : 25s
return [NSString stringWithFormat:@"%ds", seconds];
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句