【发布时间】:2017-08-02 07:23:43
【问题描述】:
我是 Swift 新手,我正在尝试制作从长按按钮开始的计时器(在标签中)。同时我想在长按按钮时更改长按按钮图像。我离开按钮,我希望按钮恢复原状。
可能出了什么问题?
@IBOutlet weak var myBtn: UIButton!
func initGesture()
{
{ let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(longTap(_:)))
myBtn.addGestureRecognizer(longGesture)
}
}
func TimerAction()
{
Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(longTap), userInfo: nil, repeats: false)
myBtn.setImage(UIImage(named: "xxx.png"), for: .normal)
}
@IBOutlet weak var lbl: UILabel!
func start()
{
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: (#selector(ViewController2.updateTime as (ViewController2) -> () -> ())), userInfo: nil, repeats: true)
}
func updateTimer () {
count += 1
let hours = Int(count) / 3600
let minutes = Int(count) / 60 % 60
let seconds = Int(count) % 60
label.text = String(format: "%02i:%02i:%02i",hours,minutes,seconds)
}
func reset()
{
timer.invalidate()
count = 0
label.text = "00:00:00"
}
【问题讨论】:
-
你的代码在做什么?
-
我正在尝试做一些我写的事情。我在长按手势识别时敲了myBtn。我哪里错了? @MohammadBashirSidani
-
发布你的 longTap 方法
-
您正在调用 updateTime 方法(在您的代码中不存在),而不是在 start 方法中调用 updateTimer 方法。然后将您的图像设置为@Kiester 解决方案。
标签: ios iphone swift swift3 uilongpressgesturerecogni