简体   繁体   English

卡住:Swift iOS中的模拟时钟

[英]Stuck: Analog Clock in Swift iOS

I'm new to swift (and coding in general), and I've been working on an analog watch project. 我是新手(通常是编码),并且一直在从事模拟手表项目。 I've gotten to a place where I'm stuck. 我到了一个被卡住的地方。 None of the println() commands are putting anything out to the console unless they arrive before the function "func setTime(){...} ". 除非它们在函数“ func setTime(){...}”之前到达,否则没有任何println()命令将任何内容放入控制台。 Not sure what's happening there. 不知道那里发生了什么。 And I can't seem to get the hands to rotate with the math I found here . 而且我似乎无法用我在这里找到的数学来动手。 I'm trying to convert it from obj-c to swift. 我正在尝试将其从obj-c转换为swift。 (I know the bottom section is very wrong at the moment) I keep messing with it (the bottom section) but I don't get any rotation. (我知道当前底部是非常错误的)我一直在弄乱它(底部),但是我没有任何旋转。

Any help would be appreciated. 任何帮助,将不胜感激。 I feel like I'm close. 我觉得我接近了。

    var theTimer:NSTimer!

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

    func setTime(){

        println("set time")

        self.theTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "set time", userInfo: nil, repeats: false)

        let date = NSDate()
        let outputFormatter = NSDateFormatter()
        outputFormatter.dateFormat = "hh:mm:ss"
        let newDateString:NSString = outputFormatter.stringFromDate(date)
        println(newDateString)

        let calendar = NSCalendar.currentCalendar()
        let components = calendar.components(.CalendarUnitHour | .CalendarUnitMinute | .CalendarUnitSecond, fromDate: date)
        var hour = components.hour
        var minute = components.minute
        var second = components.second

        println(hour)
        println(minute)
        println(second)

        **var secAngle = (6 * second)
        var minAngle = (6 * minute)
        var hourAngle = (30 * hour + minute / 2)

        self.secsImage.transform = CGAffineTransformMakeRotation( CGFloat(secAngle) )
        self.minsImage.transform = CGAffineTransformMakeRotation( CGFloat(Double(minAngle)) )
        self.hoursImage.transform = CGAffineTransformMakeRotation( CGFloat(Double(hourAngle)) )**

        println(secAngle)
        println(minAngle)
        println(hourAngle)
}
}

You have declared your setTime function inside your viewDidLoad function, and you have never called it. 您已经 viewDidLoad函数中声明了setTime函数,但从未调用过它。 Also, your the selector for your NSTimer is not correct - it needs to be the name of the function and it needs to be a Selector , not just a string. 另外,您的NSTimer选择器也不正确-它必须是函数的名称,并且它必须是Selector ,而不仅仅是字符串。

You should move the setTime function declaration out of viewDidLoad and I would suggest setting it up in viewWillAppear - 您应该将setTime函数声明从viewDidLoad移出,我建议在viewWillAppear设置-

var theTimer:NSTimer?

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

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    self.setTime()

    self.theTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("setTime"), userInfo: nil, repeats: true)

}

func setTime(){

    println("set time")
    let date = NSDate()
    let outputFormatter = NSDateFormatter()
    outputFormatter.dateFormat = "hh:mm:ss"
    let newDateString:NSString = outputFormatter.stringFromDate(date)
    println(newDateString)

    let calendar = NSCalendar.currentCalendar()
    let components = calendar.components(.CalendarUnitHour | .CalendarUnitMinute | .CalendarUnitSecond, fromDate: date)
    var hour = components.hour
    var minute = components.minute
    var second = components.second

    println(hour)
    println(minute)
    println(second)

    var secAngle = (6 * second)
    var minAngle = (6 * minute)
    var hourAngle = (30 * hour + minute / 2)

    self.secsImage.transform = CGAffineTransformMakeRotation( CGFloat(secAngle) )
    self.minsImage.transform = CGAffineTransformMakeRotation( CGFloat(Double(minAngle)) )
    self.hoursImage.transform = CGAffineTransformMakeRotation( CGFloat(Double(hourAngle)) )**

    println(secAngle)
    println(minAngle)
    println(hourAngle)
}

If you are new to programming, you may want to consider running through some basic coding tutorials on the web. 如果您不熟悉编程,则可能需要考虑在网络上浏览一些基本的编码教程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM