运行 Swift 中的配速计算器
Running Pace Calculator in Swift
我是 Swift 的新手。我在 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!
}
故事板
免责声明:我也没有编程经验。所以对行话和解释要温和。)
希望这对你有用:
您没有将 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
}
}
在这里我想分享我计算步速的解决方案。计算基于时间和地点的输入,更通用和有用。
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
}
}
relatedTimeString
是一个辅助函数,用于获取短格式的时间字符串 # yrs, # days, # hrs, # min, # sec 和完整格式的 # years, .. ., 取决于 none 零值。例如,简写形式为 6 分 5 秒,完整形式为 6 分 5 秒。
通过这种方式,func
更加通用并支持本地化和辅助功能。
我是 Swift 的新手。我在 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!
}
故事板
免责声明:我也没有编程经验。所以对行话和解释要温和。)
希望这对你有用:
您没有将 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
}
}
在这里我想分享我计算步速的解决方案。计算基于时间和地点的输入,更通用和有用。
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
}
}
relatedTimeString
是一个辅助函数,用于获取短格式的时间字符串 # yrs, # days, # hrs, # min, # sec 和完整格式的 # years, .. ., 取决于 none 零值。例如,简写形式为 6 分 5 秒,完整形式为 6 分 5 秒。
通过这种方式,func
更加通用并支持本地化和辅助功能。