swift新手:为什么Timer没有更新到UILabel?

时间:2017-06-11 17:14:16

标签: ios swift xcode timer uilabel

我正在尝试使用swift制作自定义时钟。我最初是用python编写的,但我认为这将是学习一门新语言的好机会,但我正在粗略地研究它。我已经阅读了很多关于使用Timer()的很好的答案,但似乎没有任何工作;它会更新一次并保持静止。到目前为止,这是我的代码:

import UIKit
import Foundation


class ViewController: UIViewController {

@IBOutlet weak var timeLabel: UILabel!
let clock = MarsTime()
var timer: Timer?

override func viewDidLoad() {
    super.viewDidLoad()

    timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { [weak self] timer in
        self?.updateTimeLabel()
            }



}

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


}

@objc func updateTimeLabel() {

    let millis = clock.currentTimeMillis()
    let jdUT = clock.julianDateUT(millis: millis)
    let jdTT = clock.julianDateTT(julianDateUT: jdUT)
    let mct = clock.marsCoordinatedTime(julianDateTT: jdTT)
    var mctClockTime = clock.clockTime(mct: mct)
    let hour = mctClockTime[0]
    let min = mctClockTime[1]
    let sec = mctClockTime[2]
    timeLabel.text = "\(hour):\(min):\(sec)"
}

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

deinit {
    NotificationCenter.default.removeObserver(self)
}

}

感谢您提供的任何帮助!

更新:我还包含了MarsTime代码

class MarsTime {

//---------------------------------\\
// Configure Earth Time Functions  \\
//---------------------------------\\

let date = Date().timeIntervalSince1970



func currentTimeMillis() -> (Int) {// Convert time to milliseconds
    let currentTimeMillis = Int(date * 1000)
    return (currentTimeMillis)
}

func julianDateUT(millis: Int) -> (Double) { // Convert julian date universial time
    let julianDateUT = 2440587.5 + (Double(millis) / (8.64 * pow(10, 7)))
    return (julianDateUT)
}

func julianDateTT(julianDateUT: Double) -> (Double) {// Convert to julian date Terrestrial time
    let julianDateTT = julianDateUT + ((32.184 + 37.0) / (86400.0))
    return (julianDateTT)
}

func deltatJ2000(julianDateTT: Double) -> (Double) {// Calculate time offset from J2000 Epoch
    let deltatJ2000 = julianDateTT - 2451545.0
    return (deltatJ2000)
}
//---------------------------------\\
//Configure Martian Time Fucntions \\
//----------------------------------\\

func marsMeanAnomaly(deltatJ2000: Double) -> (Double) { // Calculate the mean anomaly of the martian orbit
    let maUncorrected = 19.3871 + 0.52402073*(deltatJ2000)
    let n360s = Int( maUncorrected / 360.0) * 360
    let marsMeanAnomaly = maUncorrected - Double(n360s)
    return marsMeanAnomaly
}

func angleFictionMeanSun(deltatJ2000: Double) -> (Double) {  // Calulate angle of diction mean sun
    let afmsUncorrected = 270.3871 + 0.524038496*(deltatJ2000)
    let n360s = Int(afmsUncorrected / 360.0) * 360
    let angleFictionMeanSun = afmsUncorrected - Double(n360s)
    return (angleFictionMeanSun)
}

func perturbers(deltatJ2000: Double) -> (Double) { // Calculate perturbers
    let pbs = 0.0071 * cos(.pi / 180.0 * (((0.985626*deltatJ2000)/2.2353) + 49.409)) +
        0.0057 * cos(.pi / 180.0 * (((0.985626*deltatJ2000)/2.7543) + 168.173)) +
        0.0039 * cos(.pi / 180.0 * (((0.985626*deltatJ2000)/1.1177) + 191.837)) +
        0.0037 * cos(.pi / 180.0 * (((0.985626*deltatJ2000)/15.7866) + 21.736)) +
        0.0021 * cos(.pi / 180.0 * (((0.985626*deltatJ2000)/2.1354) + 15.704)) +
        0.0020 * cos(.pi / 180.0 * (((0.985626*deltatJ2000)/2.4694) + 95.528)) +
        0.0018 * cos(.pi / 180.0 * (((0.985626*deltatJ2000)/32.8493) + 49.095))
    return (pbs)
}

func v_M(deltatJ2000: Double, pbs: Double, marsMeanAnomaly: Double) -> (Double) { // Determine the equation of center
    let A = (10.691 + (3*pow(10, -7))*deltatJ2000)
    let B = Int((10.691 + (3*pow(10, -7))*deltatJ2000) / 360)*360
    let leadingConstant = A + Double(B)
    let v_M = (leadingConstant)*sin(.pi / 180.0 * (marsMeanAnomaly)) +
        0.623*sin(.pi / 180.0 * (2*marsMeanAnomaly)) + 0.050*sin(.pi / 180.0 * (3*marsMeanAnomaly)) +
        0.005*sin(.pi / 180.0 * (4*marsMeanAnomaly)) + 0.0005*sin(.pi / 180.0 * (5*marsMeanAnomaly)) +
    pbs
    return (v_M)
}

func aerocentSolarLong(angleFictionMeanSun: Double, v_M: Double) -> (Double) {
    let l_s = (angleFictionMeanSun + v_M) - Double(Int((angleFictionMeanSun + v_M) / 360)*360)
    return (l_s)
}

func martianEquationOfTime(l_s: Double, v_M: Double) -> (Double) {
    let eot = 2.861*sin(.pi / 180.0 * (2*l_s)) - 0.071*sin(.pi / 180.0 * (4*l_s)) +
        0.002*sin(.pi / 180.0 * (6*l_s)) - v_M
    return (eot)
}



func marsCoordinatedTime(julianDateTT: Double) -> (Double) {
    let mct = (24.0 * (((julianDateTT - 2451549.5) / 1.0274912517) + 44769.0 - 0.0009626))
    let mctFinal = mct.truncatingRemainder(dividingBy: 24.0)
    return (mctFinal)
}

func localMeanSolarTime(mct: Double, deg: Double) -> (Double)  { // Solar time for any longtitude west of the prime meridian
    let lmst = mct - deg*(1.0 / 15.0)
    return (lmst)
}


func marsDistance(ma: Double) -> (Double) {
    let helioDistance = 1.5236*(1.00436 - 0.09309*cos(.pi / 180.0 * (ma))
        - 0.00436*cos(.pi / 180.0 * (2*ma)) - 0.00031*cos(.pi / 180.0 * (3*ma)))
    return (helioDistance)
}

//-------------------------\\
// Configure Clock Display \\
//-------------------------\\
func clockTime(mct: Double) -> Array<String> {


    let mctStr = String(mct)
    var strHours = mctStr.components(separatedBy: ".")
    var mctHours  = strHours[0]
    var strMin = String(60 * Double("." + strHours[1])!).components(separatedBy: ".")
    var mctMin = strMin[0]
    var strSec = String(60 * Double("." + strMin[1])!).components(separatedBy: ".")
    var mctSec = strSec[0]
    let sec = mctSec.characters.count
    let min =  mctMin.characters.count
    let hour = mctHours.characters.count
    if 1 <= sec && sec < 2 {
        mctSec = "0" + mctSec
    } else if mctSec.characters.count < 1 {
        mctSec = "00"
    }
    if 1 <= min && min < 2 {
        mctMin = "0" + mctMin
    } else if mctMin.characters.count < 1 {
        mctMin = "00"
    }
    if 1 <= hour && hour < 2 {
        mctHours = "0" + mctHours
    } else if hour < 1 {
        mctHours = "00"
    }

    let mctClockTime = [mctHours , mctMin ,  mctSec]
    return (mctClockTime)
}
}

2 个答案:

答案 0 :(得分:2)

您的计时器工作正常。您的标签每秒都会更新,但它总是会更新为相同的值。

问题是您在创建date对象时只计算MarsTime次。

解决这个问题的一种方法是让date成为计算属性,每次读取时都会返回当前时间:

var date: TimeInterval { return Date().timeIntervalSince1970 }

答案 1 :(得分:-1)

试试这个:

timer = Timer.scheduledTimer(timeInterval: 1, target: self,   selector: (#selector(ViewController.updateTimeLabel)), userInfo: nil, repeats: true)