0

I'm new to Swift and I'm trying to make timer(in the label) that starts with a long press on the button. At the same time I want to change the long press button image when press the long press button. I leave button, I want the button revert back.

What might be wrong?

@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"

}
MarryJoe
  • 3
  • 4

3 Answers3

5

You can get TouchUpInside and TouchUpOutside also TouchDown action of button don't use the gesture.

class ViewController: UIViewController {
@IBOutlet weak var lbl: UILabel!
@IBOutlet weak var myBtn: UIButton!
var timer: NSTimer!

override func viewDidLoad() {
      super.viewDidLoad()
      myBtn.addTarget(self, action: "buttonDown:", forControlEvents: .TouchDown)
      myBtn.addTarget(self, action: "buttonUp:", forControlEvents: [.TouchUpInside, .TouchUpOutside])
    }
func buttonDown(sender: AnyObject) {
    singleFire()
    timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "rapidFire", userInfo: nil, repeats: true)
}

func buttonUp(sender: AnyObject) {
    timer.invalidate()
    count = 0
    label.text = "00:00:00"
}

func singleFire() {
    myBtn.setImage(UIImage(named: "xxx.png"), for: .normal)
}

func rapidFire() {
    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)
}

}
Salman Ghumsani
  • 3,647
  • 2
  • 21
  • 34
1

You should implement button events instead of using the gesture recognisers here. The button events i talk about are TouchDown and TouchUpInside. TouchDown will tell you when a button is being pressed and touchupinside will tell you when the user lifted their finger from button.

So you will change the button image and start your timer on touchdown event. Then you will revert back on touchupinside event.

https://developer.apple.com/documentation/uikit/uicontrolevents

Rishabh
  • 465
  • 5
  • 14
0

Try this,

set first the image of the button depending on its state.

    self.arrowIcon.setImage(UIImage(named: "normalImage.png"), for: .normal)
    self.arrowIcon.setImage(UIImage(named: "pressedImage.png"), for: .highlighted)
Kiester
  • 147
  • 1
  • 7