Swift2 Swift UILongPress手势识别器

Swift2 Swift UILongPress手势识别器,swift2,uilongpressgesturerecogni,Swift2,Uilongpressgesturerecogni,我已经为长按按钮创建了一个作业 但如何设置长按按钮并在标签上添加数字 像1+1+1+1+1 import UIKit class ViewController: UIViewController { var Step : Int = 0 var timer = NSTimer() @IBOutlet weak var CounterLabel: UILabel! @IBAction func longPressButton(sender: UILongPressGestureRecog

我已经为长按按钮创建了一个作业 但如何设置长按按钮并在标签上添加数字 像1+1+1+1+1

import UIKit

class ViewController: UIViewController {

var Step : Int = 0
var timer = NSTimer()

@IBOutlet weak var CounterLabel: UILabel!


@IBAction func longPressButton(sender: UILongPressGestureRecognizer) {
    print("longPressFunc")
}

func addNumberToLabel(){
    Step = Step + 1
    updateLabel()
}
func updateLabel(){
    //Step = Step + 1
    CounterLabel.text = String(Step)
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.longPressFunc(_:)))
    longPressRecognizer.allowableMovement = 10
    //longPressRecognizer.minimumPressDuration = 1.0
    self.view.addGestureRecognizer(longPressRecognizer)



}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func longPressFunc(sendor: UILongPressGestureRecognizer){

    //timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(ViewController.addNumberToLabel), userInfo: nil, repeats: true)
    updateLabel()
}
}

首先需要将长按乐趣拖动到故事板

导入UIKit

类ViewController:UIViewController{

var step : Int = 0

@IBOutlet weak var CounterLabel: UILabel!

@IBAction func tapButton(sender: AnyObject) {
    step = step + 1
    updateLabel()
}

@IBAction func longPressButton(sender: UILongPressGestureRecognizer) {
    if sender.state == .Changed {
        step = step + 1
        updateLabel()
    }
}
@IBAction func resetButton(sender: AnyObject) {
    step = 0
    updateLabel()
}

func updateLabel() {
    CounterLabel.text = String(step)
}


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    step = 0
    updateLabel()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}