【问题标题】:Tap button for bpm code not being executed未执行 bpm 代码的点击按钮
【发布时间】:2019-04-18 09:50:05
【问题描述】:

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

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")
        }
    }
}

【问题讨论】:

  • 函数内部的函数不一定被调用。并且不要在 Swift 3+ 中使用 NSDate。并以大写字母开头的类。
  • 正如@vadian 暗示的那样,尽管您的操作已被触发,但您从未真正调用过addTap()

标签: swift uibutton nstimeinterval


【解决方案1】:

您需要在函数体之外定义一个计算 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
}

【讨论】:

  • 感谢您的帮助!
【解决方案2】:

在按钮操作之外编写 addTap() 函数。然后从按钮的 Action 中调用 addTap() 函数。

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))
        return calculatedbpm

       // print("func is working")
    }

 @IBAction func tapnumberbutaction(_ sender: Any) {

     self.tapnumberlabel.text = String(addTap())                             
  }

【讨论】:

    猜你喜欢
    • 2014-02-24
    • 1970-01-01
    • 1970-01-01
    • 2020-09-30
    • 2013-05-18
    • 1970-01-01
    • 2020-07-02
    • 2021-03-24
    • 1970-01-01
    相关资源
    最近更新 更多