如何在 ios 中显示事件的时间
How to Show the time for an event in ios
在我的 iOS 应用程序中,我必须创建一个 HH:MM:SS
格式的数字计时器(不是系统时间),它应该从 00:00:00
开始,点击 [=13] =],我可以使用任何标准库来这样做吗?或者我应该写我自己的逻辑?
iOS Foundation 框架包括 NSDateFormatter
class(和 NSDate
数据类型),它们就是这样做的。
在您的 .m 文件中添加这些属性:
#import "MyVC.h"
@interface MyVC()
@property (strong, nonatomic) NSTimer* timer; // our timer
@property (nonatomic) NSInteger secondsPassed; // how many seconds have been passed since the start of the timer
@end
在 viewDidLoad
或 IBAction
方法中 UIButton
:
- (void)viewDidLoad {
[super viewDidLoad];
self.myLabel.text = @"00:00:00"; // start text
// invoke updateTimer every second
self.timer = [NSTimer scheduledTimerWithTimeInterval: 1.0
target:self
selector:@selector(updateTimer)
userInfo:nil
repeats: YES];
}
此方法将每秒调用一次以更新 UILabel
-(void) updateTimer{
NSInteger hours, minutes, seconds;
// increase the passed seconds
self.secondsPassed++;
// calculate the hours, minutes, seconds from the total number of seconds
hours = self.secondsPassed / 3600;
minutes = ( self.secondsPassed % 3600) / 60;
seconds = ( self.secondsPassed %3600) % 60;
// update the label with the time
self.myLabel.text = [NSString stringWithFormat:@"%02zd:%02zd:%02zd", hours, minutes, seconds];
}
或者您可以使用 MZTimerLabel
所以你可以做的一件事就是创建一个计时器并记住你创建它的时间。
@IBAction func buttonTapped() {
// Store date / time in which you tapped the button
self.initialDate = NSDate()
// Create timer that fires every second starting now (scheduled), and repeats
self.timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("timerTick"), userInfo: nil, repeats: true)
}
然后,当你有初始的东西时,你可以做timerTick
方法。在这里,您可以获得当前日期,将您存储的日期与当前日期进行区分并显示:
func timerTick() {
// Get calendar and components of the dates in interval <initialDate, currentDate>
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.CalendarUnitSecond | .CalendarUnitMinute | .CalendarUnitHour, fromDate: self.initialDate, toDate: NSDate(), options: NSCalendarOptions.allZeros)
// In this point you have minutes, seconds and hours, you can just present it
// "%02d:%02d:%02d" in format means "number, always at least 2 numbers, fill with zeroes if needed")
self.label.text = String(format: "%02d:%02d:%02d", components.hour, components.minute, components.second)
}
如果你想停止定时器,你可以调用self.timer.invalidate()
希望对您有所帮助!
在我的 iOS 应用程序中,我必须创建一个 HH:MM:SS
格式的数字计时器(不是系统时间),它应该从 00:00:00
开始,点击 [=13] =],我可以使用任何标准库来这样做吗?或者我应该写我自己的逻辑?
iOS Foundation 框架包括 NSDateFormatter
class(和 NSDate
数据类型),它们就是这样做的。
在您的 .m 文件中添加这些属性:
#import "MyVC.h"
@interface MyVC()
@property (strong, nonatomic) NSTimer* timer; // our timer
@property (nonatomic) NSInteger secondsPassed; // how many seconds have been passed since the start of the timer
@end
在 viewDidLoad
或 IBAction
方法中 UIButton
:
- (void)viewDidLoad {
[super viewDidLoad];
self.myLabel.text = @"00:00:00"; // start text
// invoke updateTimer every second
self.timer = [NSTimer scheduledTimerWithTimeInterval: 1.0
target:self
selector:@selector(updateTimer)
userInfo:nil
repeats: YES];
}
此方法将每秒调用一次以更新 UILabel
-(void) updateTimer{
NSInteger hours, minutes, seconds;
// increase the passed seconds
self.secondsPassed++;
// calculate the hours, minutes, seconds from the total number of seconds
hours = self.secondsPassed / 3600;
minutes = ( self.secondsPassed % 3600) / 60;
seconds = ( self.secondsPassed %3600) % 60;
// update the label with the time
self.myLabel.text = [NSString stringWithFormat:@"%02zd:%02zd:%02zd", hours, minutes, seconds];
}
或者您可以使用 MZTimerLabel
所以你可以做的一件事就是创建一个计时器并记住你创建它的时间。
@IBAction func buttonTapped() {
// Store date / time in which you tapped the button
self.initialDate = NSDate()
// Create timer that fires every second starting now (scheduled), and repeats
self.timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("timerTick"), userInfo: nil, repeats: true)
}
然后,当你有初始的东西时,你可以做timerTick
方法。在这里,您可以获得当前日期,将您存储的日期与当前日期进行区分并显示:
func timerTick() {
// Get calendar and components of the dates in interval <initialDate, currentDate>
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.CalendarUnitSecond | .CalendarUnitMinute | .CalendarUnitHour, fromDate: self.initialDate, toDate: NSDate(), options: NSCalendarOptions.allZeros)
// In this point you have minutes, seconds and hours, you can just present it
// "%02d:%02d:%02d" in format means "number, always at least 2 numbers, fill with zeroes if needed")
self.label.text = String(format: "%02d:%02d:%02d", components.hour, components.minute, components.second)
}
如果你想停止定时器,你可以调用self.timer.invalidate()
希望对您有所帮助!