您可以使用NSTimer創建自定義按鈕來檢測按鈕上的按鈕。我沒有測試它,但我想它可以幫助你:
class CustomButton: UIButton {
let updateInterval = 0.1
var timer: NSTimer?
var isUserPressing = false
var updateBlock: (() -> Void)?
convenience init() {
self.init(frame: CGRect.zero)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
startTimer()
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
stopTimer()
}
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
stopTimer()
}
private func startTimer() {
isUserPressing = true
timer = NSTimer(timeInterval: updateInterval, target: self, selector: "timerUpdated", userInfo: nil, repeats: true)
NSRunLoop.mainRunLoop().addTimer(self.timer!, forMode: NSDefaultRunLoopMode)
}
private func stopTimer() {
isUserPressing = false
timer?.invalidate()
timer = nil
timerUpdated() // to detect taps
}
private func timerUpdated() {
updateBlock?()
}
}
你可以使用這樣的:
let button = CustomButton()
button.updateBlock = {
// call your update functions
}
酷,我喜歡它。我會馬上開始寫。謝謝! –
很高興聽到 –
**注意:** NSTimer'不會啓動,除非您在 –