gpt4 book ai didi

swift - 未执行 bpm 代码的点击按钮

转载 作者:行者123 更新时间:2023-11-28 13:43:21 24 4
gpt4 key购买 nike

我正在尝试创建一个简单的 Controller ,它会在用户每次单击按钮时为您提供每分钟的节拍。我的代码编译成功,但按钮操作中的代码由于某种原因没有被调用。对我做错了什么有什么想法吗?

import Foundation
import UIKit

class taptempo: UIViewController {


private var timeOutInterval = 5.0
private var minTaps = 3
private var taps: [NSDate] = []

var calculatedbpm = 0

var timeOut = 5
var minimumTaps = 3

@IBOutlet weak var tapnumberlabel: UILabel!
@IBOutlet weak var tapnumberbutton: UIButton!

override func viewDidLoad(){
super.viewDidLoad()

self.tapnumberlabel.text = "\(calculatedbpm)"

}

@IBAction func tapnumberbutaction(_ sender: Any) {

func addTap() -> Int? {
let thisTap = NSDate()
if let lastTap = taps.last {
if thisTap.timeIntervalSince(lastTap as Date) > timeOutInterval {
taps.removeAll()
}
}
taps.append(thisTap)
guard taps.count >= minTaps else { return nil }
guard let firstTap = taps.first else { return 0 }
let avgIntervals = thisTap.timeIntervalSince(firstTap as Date) / Double(taps.count - 1)
calculatedbpm = Int((60.0 / avgIntervals))
self.tapnumberlabel.text = "\(calculatedbpm)"
print(calculatedbpm)
return calculatedbpm

// print("func not working")
}
}
}

最佳答案

您需要在函数体之外定义一个计算 bmp 的函数。在按钮操作中只需调用它并更新 ui。

@IBAction func tapnumberbutaction(_ sender: Any) {
self.tapnumberlabel.text = "\(addTap() ?? 0)" // conditional unwrap of returned value
}

func addTap() -> Int? {
let thisTap = NSDate()
if let lastTap = taps.last {
if thisTap.timeIntervalSince(lastTap as Date) > timeOutInterval {
taps.removeAll()
}
}
taps.append(thisTap)
guard taps.count >= minTaps else { return nil }
guard let firstTap = taps.first else { return 0 }
let avgIntervals = thisTap.timeIntervalSince(firstTap as Date) / Double(taps.count - 1)
calculatedbpm = Int((60.0 / avgIntervals))
print(calculatedbpm)
return calculatedbpm
}

关于swift - 未执行 bpm 代码的点击按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55743461/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com