Skip to content

Instantly share code, notes, and snippets.

@fozoglu
Created January 17, 2017 08:50
Show Gist options
  • Save fozoglu/5003b77131c0e10ae24a44e43913ddb9 to your computer and use it in GitHub Desktop.
Save fozoglu/5003b77131c0e10ae24a44e43913ddb9 to your computer and use it in GitHub Desktop.
Butona basılı tutma ile tetiklenen bir örnek
//
// ViewController.swift
// HoldButtonTest
//
// Created by CBS MOBIL on 17.01.2017.
// Copyright © 2017 CBS MOBİL. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet var button: UIButton!
@IBOutlet var lblNumber: UILabel!
var timer: Timer!
var speedAmmo = 50
@IBAction func buttonDown(sender: AnyObject) {
singleFire()
timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector:#selector(rapidFire), userInfo: nil, repeats: true)
}
@IBAction func buttonUp(sender: AnyObject) {
timer.invalidate()
}
func singleFire() {
if speedAmmo > 0 {
speedAmmo -= 1
print("bang!")
let value : Int = Int(lblNumber.text!)!+1
lblNumber.text = String(describing: value)
} else {
print("out of speed ammo, dude!")
timer.invalidate()
}
}
func rapidFire() {
if speedAmmo > 0 {
speedAmmo -= 1
print("bang!")
let value : Int = Int(lblNumber.text!)!+1
lblNumber.text = String(describing: value)
} else {
print("out of speed ammo, dude!")
timer.invalidate()
}
}
override func viewDidLoad() {
super.viewDidLoad()
button.addTarget(self, action:#selector(buttonDown(sender:)), for: .touchDown)
button.addTarget(self, action:#selector(buttonUp(sender:)), for: [.touchUpInside, .touchUpOutside])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment