Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // 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])
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement