设置WatchKit计时器的倒计时间隔

时间:2016-02-10 23:35:02

标签: swift timer watchkit

然而,新的观察发展......

我的应用让用户使用一个接口控制器上的滑块选择倒数计时器的持续时间,如下所示:

class game_settings: WKInterfaceController {

    @IBOutlet weak var halflength: WKInterfaceSlider!

    @IBOutlet weak var halflengthdisplay: WKInterfaceLabel!

    @IBOutlet var sethalflengthbutton: WKInterfaceButton!


    @IBAction func halfsliderdidchange(value: Float) {

        halflengthdisplay.setText("\(value)")
    }



    override func contextForSegueWithIdentifier(initialhalftogame: String) -> AnyObject? {
        // You may want to set the context's identifier in Interface Builder and check it here to make sure you're returning data at the proper times

        // Return data to be accessed in ResultsController
        return self.halflengthdisplay
    }

}

我从以下问题得到了这个:Passing data question

然后我希望所选间隔用于另一个接口控制器上的定时器,如下所示。

class main_game_controller: WKInterfaceController {

    @IBOutlet weak var WKTimer: WKInterfaceTimer!//timer that the user will see

    var internaltimer : NSTimer?
    var ispaused = false
    var elapsedTime : NSTimeInterval = 0.0
    var StartTime = NSDate()

    @IBOutlet var extratime_button: WKInterfaceButton!
    @IBOutlet var endgame_button: WKInterfaceButton!
    @IBOutlet var sanction_button: WKInterfaceButton!
    @IBOutlet var goal_button: WKInterfaceButton!
    @IBOutlet var additional_time_timer: WKInterfaceTimer!
    @IBOutlet var reset_timer_button: WKInterfaceButton!
    @IBOutlet var stop_timer_button: WKInterfaceButton!
    @IBOutlet var Start_timer_button: WKInterfaceButton!

    override func awakeWithContext(context: AnyObject?) {
        super.awakeWithContext(context)
        var halflengthinterval : NSTimeInterval// variable was written to, but never read
        // Configure interface objects here.
        if let halflength: String = context as? String {
            halflengthinterval  = Double(halflength)!

        }


    }

    override func willActivate() {
        super.willActivate()
    }

@IBAction func Start_button_pressed() {

        internaltimer = NSTimer.scheduledTimerWithTimeInterval(halflengthinterval, target:self, selector: Selector("timerdone"), userInfo: nil, repeats:false) //use of unfesolved identifier"halflengthinterval"
        WKTimer.setDate(NSDate(timeIntervalSinceNow: halflengthinterval))
        WKTimer.start()//use of unresolved identifier "halflengthinterval"


    }

    @IBAction func stop_timer_button_pressed() {
        if ispaused{
            ispaused = false
            internaltimer = NSTimer.scheduledTimerWithTimeInterval(halflengthinterval - elapsedTime, target: self, selector:  Selector("timerDone"), userInfo: nil, repeats: false)//use of unresolved identifier 'halflengthinterval'
            WKTimer.setDate(NSDate(timeIntervalSinceNow: halflengthinterval - elapsedTime))//use of unresolved identifier 'halflengthinterval'
            WKTimer.start()
            StartTime = NSDate()
            stop_timer_button.setTitle("Pause")
        }
        else{
            ispaused = true

            //get how much time has passed before they paused it
            let paused = NSDate()
            elapsedTime += paused.timeIntervalSinceDate(StartTime)

            //stop watchkit timer on screen
            WKTimer.stop()

            //stop internal timer
            internaltimer!.invalidate()

            //ui modification
            stop_timer_button.setTitle("Resume")
        }
    }

我正在关注此问题中提供的答案:WKInterface implementation

正如您在上面的注释行中所看到的,我收到了与变量halflengthinterval相关的多个错误。我觉得我没有正确地传递两个接口控制器之间的间隔值,但对于我的生活,我不知道该怎么做。

有人可以帮我展示

  1. 如何从第一个界面传递计时器的值 控制器到第二个接口控制器和
  2. 如何在第一个接口控制器中滑块选择的时间长度内正确设置倒数计时器。
  3. 非常感谢!

1 个答案:

答案 0 :(得分:1)

让我们首先解决有关NSInterval的错误,NSInterval只是类型Double的类型:

typealias NSTimeInterval = Double

因此,您遇到的问题是如何将String转换为Double,并采用Double构造函数的方式如下:

Double(IntValue)

关于如何以正确的方式从您正在进行的两个WKInterfaceController传递数据,但是您有一个错误需要修复。如果您想使用segues将数据从一个WKInterfaceController传递到另一个WKInterfaceController,则可以使用contextForSegueWithIdentifier,但在您的情况下,您将返回NSInterval类型或{{1然后你试图将其作为Double进行投射,并且在这行代码中总是失败:

String

如果您愿意或使用可选绑定,则必须使用// Configure interface objects here. if let halflength: String = context as? String { halflengthinterval = Double(halflength)! } 语句将其更改为此行,这取决于您:

  

<强> guard

guard
  

<强>可选的结合

guard let halflength = context as? Double else {
    return
}

self.halflengthinterval  = Double(halflength)

我希望这对你有所帮助。