0

我已经为长按按钮创建了一个工作,但是如何设置长按按钮并将数字添加到标签,如 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()
}
}
4

1 回答 1

-1

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

导入 UIKit

类视图控制器: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.
}

}

于 2016-05-24T17:10:29.063 回答