按钮之间的时差快速按下

时间:2014-11-16 17:24:03

标签: swift

现在我正在制作一个时钟应用程序,允许用户打卡/退出工作时间。但我无法弄清楚如何使这项功能成为可能。

在我的模特中,我有:

struct TimeLog {

var punchInTime: CFAbsoluteTime
var punchOutTime: CFAbsoluteTime
var timeWorked: Double


init (pInTime: CFAbsoluteTime, pOutTime: CFAbsoluteTime) {
    self.punchInTime = pInTime
    self.punchOutTime = pOutTime
    self.timeWorked = pOutTime - pInTime
}

}

在我的视图控制器中:

class ViewController: UIViewController {


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func punchInButtonPressed(sender: AnyObject) {

// not sure what actions are needed here to start the "aTimeLog" variable

}
@IBAction func punchOutButtonPressed(sender: AnyObject) {

// not sure what actions are needed here to complete the "aTimeLog" variable

}

我试图完成这个变量:

var aTimeLog = TimeLog(pInTime: //get the punch in time here, pOutTime: //get the punch out time here)

一旦变量" aTimeLog"完成(按下punchOutButton)我想显示我的所有" timeWorked"变量

提前原谅我。你或许可以告诉我,我只是在学习编程和Swift。

4 个答案:

答案 0 :(得分:1)

作为可能的解决方案之一,您可以按如下方式更改init方法:

initWithStartTime (pInTime: CFAbsoluteTime) 
{
    self.punchInTime = pInTime
}

然后添加如下方法:

func workedTimeWithEndTime(pOutTime: CFAbsoluteTime) -> CFAbsoluteTime
{
    return pOutTime - self.punchInTime
}

最后你可能想要添加" var timeLog:TimeLog!"进入视图控制器并使用开始时间在punchInButtonPressed中初始化它。

答案 1 :(得分:1)

就个人而言,我会在NSUserDefaults中保留一个变量 - 及时存储打卡。然后,当点击打孔按钮时,它从NSUserDefaults获取dateTime变量,进行所需的计算并返回结果。这适用于用户是否关闭应用程序(记住打卡时间)。

这会创建一个NSUserDefaults对象

var punchInTime : NSUserDefaults = NSUserDefaults.standardUserDefaults()

    @IBAction func punchInButtonPressed(sender: AnyObject) {

保存当前时间

        punchInTime.setObject(NSDate(), forKey: "punchInTime")
        punchInTime.synchronize()
    }
    @IBAction func punchOutButtonPressed(sender: AnyObject) {

获取已节省的时间

        punchInTime.objectForKey("punchInTime") as NSDate

创建一个包含当前时间的变量,以便您可以比较两个

        var currentTime : NSDate = NSDate()
        currentTime.timeIntervalSinceDate(punchInTime) // returns seconds

现在你可以把开始时间,结束时间和总工作时间保存在里面,例如,带有密钥的字典作为日期...

    }

然后,当您想要显示所有工作日期时,只需循环遍历字典:)

...希望我理解正确

答案 2 :(得分:0)

你应该给你的ViewController类一个参考TimeLog类的参考(为什么你选择一个结构?也可以,但只是好奇)。

var timeLog: TimeLog?

其次,每次打入时都要用新的初始化

@IBAction func punchInButtonPressed(sender: AnyObject) {
    let now = CFAbsoluteTimeGetCurrent()
    self.timeLog = TimeLog(pInTime: now)
}

因此,您仅使用pInTime初始化,其余时间设置为0 / null。接下来,当您打出时,添加pOutTime,您可以计算timeWorked。

@IBAction func punchOutButtonPressed(sender: AnyObject) {
    if (self.timeLog == nil) {
        return
    }

    self.timeLog!.punchOut(CFAbsoluteTimeGetCurrent());
    println(self.timeLog!.timeWorked)
}

由于我们不像你创建的那样使用构造函数,而且它需要一个新函数,我们需要更改你的类(除非你知道为什么需要一个结构,否则使用类)。

class TimeLog {

var punchInTime: CFAbsoluteTime
var punchOutTime: CFAbsoluteTime
var timeWorked: Double


init (pInTime: CFAbsoluteTime) {
    self.punchInTime = pInTime
    self.punchOutTime = 0
    self.timeWorked = 0
}

func punchOut(pOutTime: CFAbsoluteTime) {
self.punchOutTime = pOutTime
self.timeWorked = self.punchOutTime - self.punchInTime
}

}

答案 3 :(得分:0)

试试这个:

// run this on first action/tap
let start = Date()

// then, run this on the second time/interaction, to get the negative elapsed time
// for example: for 0.8 seconds this will return -0.8694559335708618
print("Elapsed time: \(start.timeIntervalSinceNow) seconds")