在Swift中运行Pace计算器

时间:2015-07-15 06:13:15

标签: swift xcode6 calculator

我是斯威夫特的全新手。我在Playground中创建了一个跑步计算器作为实验,它运行得很好,但我很难弄清楚如何将它连接到UI。

特别是我目前正在努力的事情:

  • 将文本字符串转换为Int标签
  • 进行计算的函数是否在IBAction中?

游乐场代码

import UIKit

func PaceCalculator (minutes:Double, seconds:Double, distance:Double) -> Double{
    return ((minutes*60) + seconds) / distance
}

var paceInSeconds = PaceCalculator(28, 26, 10.1)
var paceInMinutes = paceInSeconds / 60
var roundedMinutes = Double(floor(paceInMinutes))
var decimalSeconds = paceInMinutes - roundedMinutes
var intPace = Int(floor(roundedMinutes))
var seconds = Int(floor(decimalSeconds * 60))

println("Your average pace is \(intPace):\(seconds)/km")

不完整的Swift代码

import UIKit

class ViewController: UIViewController {

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

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

    @IBAction func calculatePaceButton(sender: UIButton) {

    }

    @IBOutlet weak var minutesTextField: UITextField!
    @IBOutlet weak var distanceTextField: UITextField!
    @IBOutlet weak var paceLabel: UILabel!

}

故事板

enter image description here

免责声明:我也没有编程经验。所以要对行话和解释保持温和。)

2 个答案:

答案 0 :(得分:2)

希望这对你有用:

您没有将secondsTextField添加为IBOutlet,请检查一下。

class ViewController: UIViewController {

    @IBOutlet weak var minutesTextField: UITextField!
    @IBOutlet weak var distanceTextField: UITextField!
    @IBOutlet weak var paceLabel: UILabel!
    @IBOutlet weak var secondsTextField: UITextField!
    @IBAction func calculatePaceButton(sender: UIButton) {
        var paceInSeconds = PaceCalculator((minutesTextField.text as NSString).doubleValue, seconds: (secondsTextField.text as NSString).doubleValue, distance: (distanceTextField.text as NSString).doubleValue)
        var paceInMinutes = paceInSeconds / 60.0
        var roundedMinutes = Double(floor(paceInMinutes))
        var decimalSeconds = paceInMinutes - roundedMinutes
        var intPace = Int(floor(roundedMinutes))
        var seconds = Int(floor(decimalSeconds * 60))
        paceLabel.text = "\(intPace)"
    }

    func PaceCalculator (minutes:Double, seconds:Double, distance:Double) -> Double{
        return ((minutes*60) + seconds) / distance
    }
}

答案 1 :(得分:0)

在这里,我想分享我的计算速度的解决方案。计算基于时间和位置的输入,这些输入更通用且更有用。

image/jpg

typealias ShortFullTupleStrings = (short: String, full: String) class PaceCalculator { private static func relatedTimeString( for value: TimeInterval) -> ShortFullTupleStrings? { let fm = DateComponentsFormatter() switch abs(value) { case 0 ..< 24*3600: // within one day fm.allowedUnits = [.year, .day, .hour, .minute, .second] case 24*3600 ..< 24*3600*10: // within 1-10 days fm.allowedUnits = [.year, .day, .hour, .minute] case 24*3600*10 ..< 24*3600*365: // within 10-365 days fm.allowedUnits = [.year, .day, .hour] default: // within 365-1000 days fm.allowedUnits = [.year, .day] } fm.unitsStyle = .short let short = fm.string(from: value) fm.unitsStyle = .full let full = fm.string(from: value) if let short = short, let full = full { return (short, full) } else { return nil } } static var isMetric: Bool { let locale = NSLocale.current let metricSystem = locale.usesMetricSystem return metricSystem } static func paceFrom( _ dt1: Date, to dt2: Date, distanceFrom loc1: CLLocation, to loc2: CLLocation) -> ShortFullTupleStrings? { let timeInterval = dt2.timeIntervalSince(dt1) let dist = loc2.distance(from: loc1) let pace: ShortFullTupleStrings? if !dist.isZero { let paceV: TimeInterval if isMetric { paceV = timeInterval / (dist / 1000.0) } else { paceV = timeInterval / (dist / 1609.344) } pace = relatedTimeString(for: paceV) } else { pace = nil } return pace } } 是一个帮助函数,用于获取#yrs,#days,#hrs,#min,#sec以及#years,...的完整形式的简短形式的时间字符串,取决于没有零值。例如,6分钟,5秒钟的简短形式,或6分钟,5秒钟的完整形式。

通过这种方式,relatedTimeString更通用,并支持本地化和可访问性。