Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. //
  2. // ViewController.swift
  3. // HoldButtonTest
  4. //
  5. // Created by CBS MOBIL on 17.01.2017.
  6. // Copyright © 2017 CBS MOBİL. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class ViewController: UIViewController {
  12.  
  13. @IBOutlet var button: UIButton!
  14. @IBOutlet var lblNumber: UILabel!
  15.  
  16. var timer: Timer!
  17. var speedAmmo = 50
  18.  
  19. @IBAction func buttonDown(sender: AnyObject) {
  20. singleFire()
  21. timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector:#selector(rapidFire), userInfo: nil, repeats: true)
  22. }
  23.  
  24. @IBAction func buttonUp(sender: AnyObject) {
  25. timer.invalidate()
  26. }
  27.  
  28. func singleFire() {
  29. if speedAmmo > 0 {
  30. speedAmmo -= 1
  31. print("bang!")
  32. let value : Int = Int(lblNumber.text!)!+1
  33. lblNumber.text = String(describing: value)
  34. } else {
  35. print("out of speed ammo, dude!")
  36. timer.invalidate()
  37. }
  38. }
  39.  
  40. func rapidFire() {
  41. if speedAmmo > 0 {
  42. speedAmmo -= 1
  43. print("bang!")
  44. let value : Int = Int(lblNumber.text!)!+1
  45. lblNumber.text = String(describing: value)
  46. } else {
  47. print("out of speed ammo, dude!")
  48. timer.invalidate()
  49. }
  50. }
  51.  
  52. override func viewDidLoad() {
  53. super.viewDidLoad()
  54.  
  55. button.addTarget(self, action:#selector(buttonDown(sender:)), for: .touchDown)
  56. button.addTarget(self, action:#selector(buttonUp(sender:)), for: [.touchUpInside, .touchUpOutside])
  57. }
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement